 Documentation
      ¶
      Documentation
      ¶
    
    
  
    
  
    Index ¶
Examples ¶
Constants ¶
      View Source
      
  
const ErrInvalid errorkit.Error = "The value does not match the enumerator specification"
    Variables ¶
This section is empty.
Functions ¶
func ReflectValues ¶ added in v0.262.0
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 Validate ¶ added in v0.173.0
Validate will check if the given value is a registered enum member.
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
}
Types ¶
This section is empty.
 Click to show internal directories. 
   Click to hide internal directories.