Documentation ¶
Overview ¶
Package enums provides common interfaces for enums and bit flag enums and utilities for using them
Package enums provides common interfaces for enums and bit flag enums and utilities for using them
Index ¶
- func BitFlagString[T BitFlagConstraint](i T, values []T) string
- func BitFlagStringExtended[T, E BitFlagConstraint](i T, values []T, extendedValues []E) string
- func BitIndexStringExtended[T, E BitFlagConstraint](i T, m map[T]string) string
- func Desc[T EnumConstraint](i T, descMap map[T]string) string
- func DescExtended[T, E EnumConstraint](i T, descMap map[T]string) string
- func HasFlag(i *int64, f BitFlag) bool
- func Scan[T EnumSetter](i T, value any, typeName string) error
- func SetFlag(i *int64, on bool, f ...BitFlag)
- func SetString[T EnumConstraint](i *T, s string, valueMap map[string]T, typeName string) error
- func SetStringExtended[T EnumConstraint, E EnumSetter](i *T, ie E, s string, valueMap map[string]T) error
- func SetStringLower[T EnumConstraint](i *T, s string, valueMap map[string]T, typeName string) error
- func SetStringLowerExtended[T EnumConstraint, E EnumSetter](i *T, ie E, s string, valueMap map[string]T) error
- func SetStringOr[T BitFlagConstraint, S BitFlagSetter](i S, s string, valueMap map[string]T, typeName string) error
- func SetStringOrExtended[T BitFlagConstraint, S BitFlagSetter, E BitFlagSetter](i S, ie E, s string, valueMap map[string]T) error
- func SetStringOrLower[T BitFlagConstraint, S BitFlagSetter](i S, s string, valueMap map[string]T, typeName string) error
- func SetStringOrLowerExtended[T BitFlagConstraint, S BitFlagSetter, E BitFlagSetter](i S, ie E, s string, valueMap map[string]T) error
- func String[T EnumConstraint](i T, m map[T]string) string
- func StringExtended[T, E EnumConstraint](i T, m map[T]string) string
- func UnmarshalText[T EnumSetter](i T, text []byte, typeName string) error
- func ValuesGlobalExtended[T, E EnumConstraint](values []T, extendedValues []E) []T
- type BitFlag
- type BitFlagConstraint
- type BitFlagSetter
- type Enum
- type EnumConstraint
- type EnumSetter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BitFlagString ¶ added in v0.0.8
func BitFlagString[T BitFlagConstraint](i T, values []T) string
BitFlagString returns the string representation of the given bit flag value with the given values available.
func BitFlagStringExtended ¶ added in v0.0.8
func BitFlagStringExtended[T, E BitFlagConstraint](i T, values []T, extendedValues []E) string
BitFlagStringExtended returns the string representation of the given bit flag value with the given values available, with the bit flag type extending the other given bit flag type that has the given values (extendedValues) available.
func BitIndexStringExtended ¶ added in v0.0.8
func BitIndexStringExtended[T, E BitFlagConstraint](i T, m map[T]string) string
BitIndexStringExtended returns the string representation of the given bit flag enum bit index value with the given map, with the bit flag type extending the given other bit flag type.
func Desc ¶ added in v0.0.8
func Desc[T EnumConstraint](i T, descMap map[T]string) string
Desc returns the description of the given enum value.
func DescExtended ¶ added in v0.0.8
func DescExtended[T, E EnumConstraint](i T, descMap map[T]string) string
DescExtended returns the description of the given enum value, with the enum type extending the other given enum type.
func Scan ¶ added in v0.0.8
func Scan[T EnumSetter](i T, value any, typeName string) error
Scan loads the enum from the given SQL scanner value.
func SetFlag ¶ added in v0.0.8
SetFlag sets the value of the given flags in these flags to the given value.
func SetString ¶ added in v0.0.8
func SetString[T EnumConstraint](i *T, s string, valueMap map[string]T, typeName string) error
SetString sets the given enum value from its string representation, the map from enum names to values, and the name of the enum type, which is used for the error message.
func SetStringExtended ¶ added in v0.0.8
func SetStringExtended[T EnumConstraint, E EnumSetter](i *T, ie E, s string, valueMap map[string]T) error
SetStringExtended sets the given enum value from its string representation and the map from enum names to values, with the enum type extending the other given enum type. It also takes the enum value in terms of the extended enum type (ie).
func SetStringLower ¶ added in v0.0.8
func SetStringLower[T EnumConstraint](i *T, s string, valueMap map[string]T, typeName string) error
SetStringLower sets the given enum value from its string representation, the map from enum names to values, and the name of the enum type, which is used for the error message. It also tries the lowercase version of the given string if the original version fails.
func SetStringLowerExtended ¶ added in v0.0.8
func SetStringLowerExtended[T EnumConstraint, E EnumSetter](i *T, ie E, s string, valueMap map[string]T) error
SetStringLowerExtended sets the given enum value from its string representation and the map from enum names to values, with the enum type extending the other given enum type. It also takes the enum value in terms of the extended enum type (ie). It also tries the lowercase version of the given string if the original version fails.
func SetStringOr ¶ added in v0.0.8
func SetStringOr[T BitFlagConstraint, S BitFlagSetter](i S, s string, valueMap map[string]T, typeName string) error
SetStringOr sets the given bit flag value from its string representation while preserving any bit flags already set.
func SetStringOrExtended ¶ added in v0.0.8
func SetStringOrExtended[T BitFlagConstraint, S BitFlagSetter, E BitFlagSetter](i S, ie E, s string, valueMap map[string]T) error
SetStringOrExtended sets the given bit flag value from its string representation while preserving any bit flags already set, with the enum type extending the other given enum type. It also takes the enum value in terms of the extended enum type (ie).
func SetStringOrLower ¶ added in v0.0.8
func SetStringOrLower[T BitFlagConstraint, S BitFlagSetter](i S, s string, valueMap map[string]T, typeName string) error
SetStringOrLower sets the given bit flag value from its string representation while preserving any bit flags already set. It also tries the lowercase version of each flag string if the original version fails.
func SetStringOrLowerExtended ¶ added in v0.0.8
func SetStringOrLowerExtended[T BitFlagConstraint, S BitFlagSetter, E BitFlagSetter](i S, ie E, s string, valueMap map[string]T) error
SetStringOrLowerExtended sets the given bit flag value from its string representation while preserving any bit flags already set, with the enum type extending the other given enum type. It also takes the enum value in terms of the extended enum type (ie). It also tries the lowercase version of each flag string if the original version fails.
func String ¶ added in v0.0.8
func String[T EnumConstraint](i T, m map[T]string) string
String returns the string representation of the given enum value with the given map.
func StringExtended ¶ added in v0.0.8
func StringExtended[T, E EnumConstraint](i T, m map[T]string) string
StringExtended returns the string representation of the given enum value with the given map, with the enum type extending the given other enum type.
func UnmarshalText ¶ added in v0.0.8
func UnmarshalText[T EnumSetter](i T, text []byte, typeName string) error
UnmarshalText loads the enum from the given text. It logs any error instead of returning it to prevent one modified enum from tanking an entire object loading operation.
func ValuesGlobalExtended ¶ added in v0.0.8
func ValuesGlobalExtended[T, E EnumConstraint](values []T, extendedValues []E) []T
ValuesGlobalExtended returns also possible values for the given enum type that extends the other given enum type.
Types ¶
type BitFlag ¶
type BitFlag interface { Enum // BitIndexString returns the string // representation of the bit flag if // the bit flag is a bit index value // (typically an enum constant), and // not an actual bit flag value. BitIndexString() string }
BitFlag is the interface that all bit flag enum types satisfy. Bit flag enum types support all of the operations that standard enums do, and additionally can check if they have a given bit flag. Note that HasFlag is defined on BitFlagSetter since it requires a pointer receiver for atomic operations to prevent race conditions.
type BitFlagConstraint ¶ added in v0.0.8
BitFlagConstraint is the generic type constraint that all bit flags satisfy.
type BitFlagSetter ¶
type BitFlagSetter interface { EnumSetter BitFlag // Has returns whether these flags // have the given flag set. HasFlag(f BitFlag) bool // Set sets the value of the given // flags in these flags to the given value. SetFlag(on bool, f ...BitFlag) // SetStringOr sets the bit flag from its // string representation while preserving any // bit flags already set, and returns an // error if the string is invalid. SetStringOr(s string) error }
BitFlagSetter is an expanded interface that all pointers to bit flag enum types satisfy. Pointers to bit flag enum types must satisfy all of the methods of EnumSetter and BitFlag, and must also be able to set a given bit flag.
type Enum ¶
type Enum interface { fmt.Stringer // Int64 returns the enum value as an int64. Int64() int64 // Desc returns the description of the enum value. Desc() string // Values returns all possible values this // enum type has. Values() []Enum }
Enum is the interface that all enum types satisfy. Enum types must be convertable to strings and int64s, must be able to return a description of their value, must be able to report if they are valid, and must be able to return all possible enum values for their type.
func Values ¶ added in v0.0.8
func Values[T EnumConstraint](values []T) []Enum
Values returns all possible values for the given enum type.
func ValuesExtended ¶ added in v0.0.8
func ValuesExtended[T, E EnumConstraint](values []T, extendedValues []E) []Enum
ValuesExtended returns all possible values for the given enum type that extends the other given enum type.
type EnumConstraint ¶ added in v0.0.8
EnumConstraint is the generic type constraint that all enums satisfy.
type EnumSetter ¶
type EnumSetter interface { Enum // SetString sets the enum value from its // string representation, and returns an // error if the string is invalid. SetString(s string) error // SetInt64 sets the enum value from an int64. SetInt64(i int64) }
EnumSetter is an expanded interface that all pointers to enum types satisfy. Pointers to enum types must satisfy all of the methods of Enum, and must also be settable from strings and int64s.
Directories ¶
Path | Synopsis |
---|---|
cmd
|
|
enumgen
Package main provides the actual command line implementation of the enumgen library.
|
Package main provides the actual command line implementation of the enumgen library. |
Package enumgen provides functions for generating enum methods for enum types.
|
Package enumgen provides functions for generating enum methods for enum types. |