Documentation
¶
Index ¶
- Variables
- func EnumNames[T ~string](enums ...T) []string
- func EnumToStringSlice[T ~string](enums ...T) []string
- func MarshalJSON[T ~string](value T, validValues ...T) ([]byte, error)
- func MarshalJSONForInterface[T EnumType](value T, typeName string) ([]byte, error)
- func ParseEnum[T ~string](str string, defaultValue T, validValues ...T) T
- func UnmarshalJSON[T ~string](dest *T, data []byte, typeName string, defaultValue T, validValues ...T) error
- func UnmarshalJSONForInterface[T EnumType](dest *T, data []byte, typeName string) error
- func UnmarshalJSONFromStrings[T ~string](dest *T, data []byte, typeName string, defaultValue T, validStrings []string) error
- func ValidateEnum[T ~string](value T, validValues ...T) bool
- type EnumType
- type StringEnum
- type ValidatableEnum
Constants ¶
This section is empty.
Variables ¶
var ( ErrEnumValueInvalid = errors.New("enum value is not in valid list") ErrInvalidEnumValue = errors.New("invalid enum value") )
Static errors for enum validation.
Functions ¶
func EnumToStringSlice ¶
EnumToStringSlice converts enum values to string slice.
func MarshalJSON ¶
MarshalJSON provides generic JSON marshaling for enum types. Usage: implement MarshalJSON on your enum type like this:
func (e MyEnum) MarshalJSON() ([]byte, error) {
return enum.MarshalJSON(e, MyEnumValue1, MyEnumValue2)
}
func MarshalJSONForInterface ¶
MarshalJSONForInterface marshals JSON using the EnumType interface. This is useful for enums that implement their own IsValid method.
func UnmarshalJSON ¶
func UnmarshalJSONForInterface ¶
UnmarshalJSONForInterface unmarshals JSON using the EnumType interface. This is useful for enums that implement their own IsValid method.
func UnmarshalJSONFromStrings ¶
func UnmarshalJSONFromStrings[T ~string]( dest *T, data []byte, typeName string, defaultValue T, validStrings []string, ) error
UnmarshalJSONFromStrings unmarshals JSON using a string slice for validation.
func ValidateEnum ¶
ValidateEnum checks if an enum value is in the list of valid values.
Types ¶
type StringEnum ¶
type StringEnum string
StringEnum is a generic type for string-based enums.
func (StringEnum) String ¶
func (se StringEnum) String() string
String returns the string value of the enum.
type ValidatableEnum ¶
type ValidatableEnum interface {
IsValid() bool
}
ValidatableEnum interface for enums that can validate themselves.