Documentation
¶
Overview ¶
Package flagenum is a utility package which facilitates implementation of flag.Value, json.Marshaler, and json.Unmarshaler interfaces via a string-to- value mapping.
Example ¶
Example demonstrates how to use flagenum to create bindings for a custom type.
Output: -value value Set the value. Options are: bar, foo Value is: 20 JSON is: {"value":"bar"}
Index ¶
- type Enum
- func (e Enum) Choices() string
- func (e Enum) FlagSet(v interface{}, key string) error
- func (e Enum) FlagString(v interface{}) string
- func (e Enum) GetKey(value interface{}) string
- func (e Enum) GetValue(key string) (interface{}, error)
- func (e Enum) JSONMarshal(v interface{}) ([]byte, error)
- func (e Enum) JSONUnmarshal(v interface{}, data []byte) error
Examples ¶
Constants ¶
Variables ¶
Functions ¶
Types ¶
type Enum ¶
type Enum map[string]interface{}
Enum is a mapping of enumeration key strings to values that can be used as flags.
Strings can be mapped to any value type that is comparable via reflect.DeepEqual.
func (Enum) FlagSet ¶
FlagSet implements flag.Value's Set semantics. It identifies the mapped value associated with the supplied key and stores it in the supplied interface.
The interface, v, must be a valid pointer to the mapped enumeration type.
func (Enum) FlagString ¶
FlagString implements flag.Value's String semantics.
func (Enum) GetKey ¶
GetKey performs reverse lookup of the enumeration value, returning the key that corresponds to the value.
If multiple keys correspond to the same value, the result is undefined.
func (Enum) JSONMarshal ¶
JSONMarshal implements json.Marshaler's MarshalJSON semantics. It marshals the value in the supplied interface to its associated key and emits a quoted string containing that key.
The interface, v, must be a valid pointer to the mapped enumeration type.
func (Enum) JSONUnmarshal ¶
JSONUnmarshal implements json.Unmarshaler's UnmarshalJSON semantics. It parses data containing a quoted string, identifies the enumeration value associated with that string, and stores it in the supplied interface.
The interface, v, must be a valid pointer to the mapped enumeration type. a string corresponding to one of the enum's keys.