Documentation ¶
Overview ¶
Package enumnames provides efficient mapping between integer enum values and string names.
Index ¶
- type IntegerEnum
- type Map
- func (enumMap Map[Enum]) ContainsKey(key Enum) bool
- func (enumMap Map[Enum]) ContainsName(name string) bool
- func (enumMap Map[Enum]) GetKey(name string) (key Enum, ok bool)
- func (enumMap Map[Enum]) GetName(key Enum) (name string, ok bool)
- func (enumMap Map[Enum]) GetNameOrFallback(key Enum, fallback string) (name string)
- func (enumMap Map[Enum]) Keys() []Enum
- func (enumMap Map[Enum]) MarshalToNameJSON(key Enum) ([]byte, error)
- func (enumMap Map[Enum]) Names() []string
- func (enumMap Map[Enum]) Size() int
- func (enumMap Map[Enum]) String() string
- func (enumMap Map[Enum]) UnmarshalFromNameJSON(nameJSON []byte, dest *Enum) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IntegerEnum ¶
type Map ¶
type Map[Enum IntegerEnum] struct { // contains filtered or unexported fields }
Map is an immutable mapping of integer enum values to string names. It must be instantiated with NewMap.
func NewMap ¶
func NewMap[Enum IntegerEnum](enumNames map[Enum]string) Map[Enum]
NewMap transforms the given enum-value-to-name map into a more efficient representation, which also provides utility methods for getting and marshaling enum names and values.
Panics if:
- the range of integer enum keys in the map is not contiguous
- there are duplicate names in the map
func (Map[Enum]) ContainsKey ¶ added in v0.2.0
ContainsKey checks if the given enum key exists in the map.
func (Map[Enum]) ContainsName ¶
ContainsName checks if any enum key maps to the given name.
func (Map[Enum]) GetKey ¶ added in v0.2.0
GetKey returns the enum key mapped to the given name, or ok=false if no mapping is found.
func (Map[Enum]) GetName ¶
GetName returns the mapped name for the given enum key, or ok=false if no mapping is found.
func (Map[Enum]) GetNameOrFallback ¶
GetNameOrFallback returns the mapped name for the given enum key, or the fallback if no mapping is found.
func (Map[Enum]) Keys ¶ added in v0.2.0
func (enumMap Map[Enum]) Keys() []Enum
Keys returns a slice of all enum keys in the map, sorted by their integer value. Mutating it will not affect the map.
func (Map[Enum]) MarshalToNameJSON ¶
MarshalToNameJSON marshals the given enum key to its mapped name. It errors if the key is not mapped.
func (Map[Enum]) Names ¶
Names returns a slice of all enum names in the map, sorted by the integer value of their keys. Mutating it will not affect the map.
func (Map[Enum]) String ¶
String returns a string representation of the map, mapping integer enum keys to their names.
func (Map[Enum]) UnmarshalFromNameJSON ¶
UnmarshalFromNameJSON unmarshals the given enum name JSON to string, and sets dest to the enum key mapped to the name. It errors if string unmarshaling fails, or if the unmarshaled enum name is not mapped.