Documentation
¶
Index ¶
- Constants
- func ReflectValidate(value any, opts ...Option) error
- func ReflectValues(reflectTypeOrStructType any) []reflect.Value
- func ReflectValuesOfStructField(field reflect.StructField) ([]reflect.Value, error)
- func Register[T any](enums ...T) (unregister func())
- func Validate[T any](v T) error
- func ValidateStruct(v any) error
- func ValidateStructField(field reflect.StructField, value reflect.Value) error
- func Values[T any]() []T
- type Option
Examples ¶
Constants ¶
View Source
const ErrInvalid errorkit.Error = "The value does not match the enumerator specification"
View Source
const ImplementationError errorkit.Error = "ImplementationError"
Variables ¶
This section is empty.
Functions ¶
func ReflectValidate ¶ added in v0.290.0
ReflectValidate checks whether a given value exists within its predefined set of valid enum options. If the given type has no enum constraint, error is never exepcted.
func ReflectValues ¶ added in v0.262.0
ReflectValues will return the enumerables for - reflect.Type -> the type specific values - reflect.StructField -> the struct field type specific values
Example ¶
package main
import (
"reflect"
"go.llib.dev/frameless/pkg/enum"
)
func main() {
type T string
const (
V1 T = "C1"
V2 T = "C2"
V3 T = "C3"
)
enum.Register[T](V1, V2, V3)
enum.ReflectValues(reflect.TypeOf((*T)(nil)).Elem()) // []{"C1", "C2", "C3"}
}
func ReflectValuesOfStructField ¶ added in v0.274.0
func ReflectValuesOfStructField(field reflect.StructField) ([]reflect.Value, error)
Deprecate: use ReflectValues instead
func Validate ¶ added in v0.173.0
Validate will check if the given value is a registered enum member. It is not a recursive operation, only check the given value.
Example ¶
package main
import (
"go.llib.dev/frameless/pkg/enum"
)
func main() {
type T string
const (
V1 T = "C1"
V2 T = "C2"
V3 T = "C3"
)
enum.Register[T](V1, V2, V3)
_ = enum.Validate(V1) // nil
_ = enum.Validate(V2) // nil
_ = enum.Validate(V3) // nil
_ = enum.Validate[T](T("C4")) // enum.Err
}
func ValidateStruct ¶
Example (Float) ¶
package main
import (
"go.llib.dev/frameless/pkg/enum"
)
func main() {
type ExampleStruct struct {
V float64 `enum:"2.5;4.2;"`
}
_ = enum.ValidateStruct(ExampleStruct{V: 4.2}) // no error
_ = enum.ValidateStruct(ExampleStruct{V: 24.42}) // has error
}
Example (Int) ¶
package main
import (
"go.llib.dev/frameless/pkg/enum"
)
func main() {
type ExampleStruct struct {
V int `enum:"2,4,8,16,42,"`
}
_ = enum.ValidateStruct(ExampleStruct{V: 42}) // no error
_ = enum.ValidateStruct(ExampleStruct{V: 24}) // has error
}
Example (Slice) ¶
package main
import (
"go.llib.dev/frameless/pkg/enum"
)
func main() {
type ExampleStruct struct {
V []string `enum:"FOO|BAR|BAZ|"`
}
_ = enum.ValidateStruct(ExampleStruct{V: []string{"FOO", "BAR", "BAZ"}}) // no error
_ = enum.ValidateStruct(ExampleStruct{V: []string{"FOO", "BAB", "BAZ"}}) // has error because of BAB
}
Example (String) ¶
package main
import (
"go.llib.dev/frameless/pkg/enum"
)
func main() {
type ExampleStruct struct {
V string `enum:"A;B;C;"`
}
_ = enum.ValidateStruct(ExampleStruct{V: "A"}) // no error
_ = enum.ValidateStruct(ExampleStruct{V: "D"}) // has error
}
func ValidateStructField ¶ added in v0.278.0
func ValidateStructField(field reflect.StructField, value reflect.Value) error
Types ¶
type Option ¶ added in v0.290.0
func ReflectType ¶ added in v0.290.0
ReflectType allows you to inject the expected enum type as a reflect.Type
Click to show internal directories.
Click to hide internal directories.