enum

package
v0.251.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2024 License: Apache-2.0 Imports: 7 Imported by: 1

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 Register

func Register[T any](enums ...T) (unregister func())

func Validate added in v0.173.0

func Validate[T any](v T) error

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

func ValidateStruct(v any) error
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 Values added in v0.172.0

func Values[T any]() []T

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL