2020-12-29 10:45:49 +00:00
|
|
|
package callflag
|
2020-03-19 15:52:37 +00:00
|
|
|
|
2021-07-05 15:23:16 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// CallFlag represents a call flag.
|
2020-03-19 15:52:37 +00:00
|
|
|
type CallFlag byte
|
|
|
|
|
|
|
|
// Default flags.
|
|
|
|
const (
|
2020-12-11 07:34:01 +00:00
|
|
|
ReadStates CallFlag = 1 << iota
|
|
|
|
WriteStates
|
2020-03-19 15:52:37 +00:00
|
|
|
AllowCall
|
|
|
|
AllowNotify
|
2020-12-11 07:34:01 +00:00
|
|
|
|
|
|
|
States = ReadStates | WriteStates
|
|
|
|
ReadOnly = ReadStates | AllowCall
|
|
|
|
All = States | AllowCall | AllowNotify
|
2020-06-10 14:24:48 +00:00
|
|
|
NoneFlag CallFlag = 0
|
2020-03-19 15:52:37 +00:00
|
|
|
)
|
2020-06-10 12:50:51 +00:00
|
|
|
|
2021-07-05 15:23:16 +00:00
|
|
|
var flagString = map[CallFlag]string{
|
|
|
|
ReadStates: "ReadStates",
|
|
|
|
WriteStates: "WriteStates",
|
|
|
|
AllowCall: "AllowCall",
|
|
|
|
AllowNotify: "AllowNotify",
|
|
|
|
States: "States",
|
|
|
|
ReadOnly: "ReadOnly",
|
|
|
|
All: "All",
|
|
|
|
NoneFlag: "None",
|
|
|
|
}
|
|
|
|
|
|
|
|
// basicFlags are all flags except All and None. It's used to stringify CallFlag
|
2022-04-20 18:30:09 +00:00
|
|
|
// where its bits are matched against these values from the values with sets of bits
|
|
|
|
// to simple flags, which is important to produce proper string representation
|
2021-07-05 15:23:16 +00:00
|
|
|
// matching C# Enum handling.
|
|
|
|
var basicFlags = []CallFlag{ReadOnly, States, ReadStates, WriteStates, AllowCall, AllowNotify}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// FromString parses an input string and returns a corresponding CallFlag.
|
2021-07-05 15:23:16 +00:00
|
|
|
func FromString(s string) (CallFlag, error) {
|
|
|
|
flags := strings.Split(s, ",")
|
|
|
|
if len(flags) == 0 {
|
|
|
|
return NoneFlag, errors.New("empty flags")
|
|
|
|
}
|
|
|
|
if len(flags) == 1 {
|
|
|
|
for f, str := range flagString {
|
|
|
|
if s == str {
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NoneFlag, errors.New("unknown flag")
|
|
|
|
}
|
|
|
|
|
|
|
|
var res CallFlag
|
|
|
|
|
|
|
|
for _, flag := range flags {
|
|
|
|
var knownFlag bool
|
|
|
|
|
|
|
|
flag = strings.TrimSpace(flag)
|
|
|
|
for _, f := range basicFlags {
|
|
|
|
if flag == flagString[f] {
|
|
|
|
res |= f
|
|
|
|
knownFlag = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !knownFlag {
|
|
|
|
return NoneFlag, errors.New("unknown/inappropriate flag")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2020-06-10 12:50:51 +00:00
|
|
|
// Has returns true iff all bits set in cf are also set in f.
|
|
|
|
func (f CallFlag) Has(cf CallFlag) bool {
|
|
|
|
return f&cf == cf
|
|
|
|
}
|
2021-07-05 15:23:16 +00:00
|
|
|
|
|
|
|
// String implements Stringer interface.
|
|
|
|
func (f CallFlag) String() string {
|
|
|
|
if flagString[f] != "" {
|
|
|
|
return flagString[f]
|
|
|
|
}
|
|
|
|
|
|
|
|
var res string
|
|
|
|
|
|
|
|
for _, flag := range basicFlags {
|
|
|
|
if f.Has(flag) {
|
|
|
|
if len(res) != 0 {
|
|
|
|
res += ", "
|
|
|
|
}
|
|
|
|
res += flagString[flag]
|
|
|
|
f &= ^flag // Some "States" shouldn't be combined with "ReadStates".
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface.
|
2021-07-05 15:23:16 +00:00
|
|
|
func (f CallFlag) MarshalJSON() ([]byte, error) {
|
|
|
|
return []byte(`"` + f.String() + `"`), nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
2021-07-05 15:23:16 +00:00
|
|
|
func (f *CallFlag) UnmarshalJSON(data []byte) error {
|
|
|
|
var js string
|
|
|
|
if err := json.Unmarshal(data, &js); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
flag, err := FromString(js)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*f = flag
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-11 10:30:15 +00:00
|
|
|
|
|
|
|
// MarshalYAML implements the YAML marshaler interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (f CallFlag) MarshalYAML() (any, error) {
|
2022-02-11 10:30:15 +00:00
|
|
|
return f.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalYAML implements the YAML unmarshaler interface.
|
2023-04-03 10:34:24 +00:00
|
|
|
func (f *CallFlag) UnmarshalYAML(unmarshal func(any) error) error {
|
2022-02-11 10:30:15 +00:00
|
|
|
var s string
|
|
|
|
|
|
|
|
err := unmarshal(&s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*f, err = FromString(s)
|
|
|
|
return err
|
|
|
|
}
|