Documentation ΒΆ
Index ΒΆ
- func Parse[M iMember[V], V Equaler[V]](e Enum[M, V], value V) *M
- type Builder
- type Enum
- func (e Enum[M, V]) Choice(seed int64) *M
- func (e Enum[M, V]) Contains(member M) bool
- func (e Enum[M, V]) Empty() bool
- func (e Enum[M, V]) GoString() string
- func (e Enum[M, V]) Index(member M) int
- func (e Enum[M, V]) Len() int
- func (e Enum[M, V]) Members() []M
- func (e Enum[M, V]) Parse(value V) *M
- func (e Enum[M, V]) String() string
- func (Enum[M, V]) TypeName() string
- func (e Enum[M, V]) Value(member M) V
- func (e Enum[M, V]) Values() []V
- type Equaler
- type Member
Examples ΒΆ
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func Parse ΒΆ added in v1.4.0
Parse is like Enum.Parse but finds the member for the value using Equaler comparator.
Example ΒΆ
package main import ( "fmt" "strings" "github.com/orsinium-labs/enum" ) type FoldedString string // Equal implements [enum.Equaler]. // // Compare strings ignoring the case. func (s FoldedString) Equal(other FoldedString) bool { return strings.EqualFold(string(s), string(other)) } func main() { type Color enum.Member[FoldedString] var ( Red = Color{"red"} Green = Color{"green"} Blue = Color{"blue"} Colors = enum.New(Red, Green, Blue) ) parsed := enum.Parse(Colors, "RED") fmt.Printf("%#v\n", parsed) }
Output: &enum_test.Color{Value:"red"}
Types ΒΆ
type Builder ΒΆ added in v1.3.0
type Builder[M iMember[V], V comparable] struct { // contains filtered or unexported fields }
Builder is a constructor for an Enum.
Use Builder.Add to add new members to the future enum and then call Builder.Enum to create a new Enum with all added members.
Builder is useful for when you have lots of enum members, and new ones are added over time, as the project grows. In such scenario, it's easy to forget to add in the Enum a newly created Member. The builder is designed to prevent that.
func NewBuilder ΒΆ added in v1.3.0
func NewBuilder[V comparable, M iMember[V]]() Builder[M, V]
NewBuilder creates a new Builder, a constructor for an Enum.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/enum" ) func main() { type Color enum.Member[string] var ( b = enum.NewBuilder[string, Color]() Red = b.Add(Color{"red"}) Green = b.Add(Color{"green"}) Blue = b.Add(Color{"blue"}) Colors = b.Enum() ) fmt.Println( Colors.Contains(Red), Colors.Contains(Green), Colors.Contains(Blue), ) }
Output: true true true
type Enum ΒΆ
type Enum[M iMember[V], V comparable] struct { // contains filtered or unexported fields }
Enum is a collection of enum members.
Use New to construct a new Enum from a list of members.
func New ΒΆ
func New[V comparable, M iMember[V]](members ...M) Enum[M, V]
New constructs a new Enum wrapping the given enum members.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/enum" ) func main() { type Color enum.Member[string] var ( Red = Color{"red"} Green = Color{"green"} Blue = Color{"blue"} Colors = enum.New(Red, Green, Blue) ) fmt.Printf("%#v\n", Colors) }
Output: enum.New(enum_test.Color{"red"}, enum_test.Color{"green"}, enum_test.Color{"blue"})
func (Enum[M, V]) Choice ΒΆ added in v1.2.0
Choice returns a randomly selected member of the enum.
A random seed can be given (or be 0 to use time.Now().UnixNano() as the seed). nil is returned only if the Enum contains no members.
func (Enum[M, V]) Contains ΒΆ
Contains returns true if the enum has the given member.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/enum" ) func main() { type Color enum.Member[string] var ( Red = Color{"red"} Green = Color{"green"} Blue = Color{"blue"} Colors = enum.New(Red, Green, Blue) ) contains := Colors.Contains(Red) fmt.Println(contains) }
Output: true
func (Enum[M, V]) Empty ΒΆ
Empty returns true if the enum doesn't have any members.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/enum" ) func main() { type Color enum.Member[string] var ( Red = Color{"red"} Green = Color{"green"} Blue = Color{"blue"} Colors = enum.New(Red, Green, Blue) ) empty := Colors.Empty() fmt.Println(empty) }
Output: false
func (Enum[M, V]) GoString ΒΆ
GoString implements fmt.GoStringer interface.
When you print a member using "%#v" format, it will show the enum representation as a valid Go syntax.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/enum" ) func main() { type Color enum.Member[string] var ( Red = Color{"red"} Green = Color{"green"} Blue = Color{"blue"} Colors = enum.New(Red, Green, Blue) ) fmt.Printf("%#v\n", Colors) }
Output: enum.New(enum_test.Color{"red"}, enum_test.Color{"green"}, enum_test.Color{"blue"})
func (Enum[M, V]) Index ΒΆ
Index returns the index of the given member in the enum.
If the given member is not in the enum, it panics. Use Enum.Contains first if you don't know for sure if the member belongs to the enum.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/enum" ) func main() { type Color enum.Member[string] var ( Red = Color{"red"} Green = Color{"green"} Blue = Color{"blue"} Colors = enum.New(Red, Green, Blue) ) index := Colors.Index(Green) fmt.Println(index) }
Output: 1
func (Enum[M, V]) Len ΒΆ
Len returns how many members the enum has.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/enum" ) func main() { type Color enum.Member[string] var ( Red = Color{"red"} Green = Color{"green"} Blue = Color{"blue"} Colors = enum.New(Red, Green, Blue) ) length := Colors.Len() fmt.Println(length) }
Output: 3
func (Enum[M, V]) Members ΒΆ
func (e Enum[M, V]) Members() []M
Members returns a slice of the members in the enum.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/enum" ) func main() { type Color enum.Member[string] var ( Red = Color{"red"} Green = Color{"green"} Blue = Color{"blue"} Colors = enum.New(Red, Green, Blue) ) members := Colors.Members() fmt.Println(members) }
Output: [{red} {green} {blue}]
func (Enum[M, V]) Parse ΒΆ
func (e Enum[M, V]) Parse(value V) *M
Parse converts a raw value into a member of the enum.
If none of the enum members has the given value, nil is returned.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/enum" ) func main() { type Color enum.Member[string] var ( Red = Color{"red"} Green = Color{"green"} Blue = Color{"blue"} Colors = enum.New(Red, Green, Blue) ) parsed := Colors.Parse("red") fmt.Printf("%#v\n", parsed) }
Output: &enum_test.Color{Value:"red"}
func (Enum[M, V]) String ΒΆ
String implements fmt.Stringer interface.
It returns a comma-separated list of values of the enum members.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/enum" ) func main() { type Color enum.Member[string] var ( Red = Color{"red"} Green = Color{"green"} Blue = Color{"blue"} Colors = enum.New(Red, Green, Blue) ) fmt.Println(Colors) }
Output: red, green, blue
func (Enum[M, V]) TypeName ΒΆ
TypeName is a string representation of the wrapped type.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/enum" ) func main() { type Color enum.Member[string] var ( Red = Color{"red"} Green = Color{"green"} Blue = Color{"blue"} Colors = enum.New(Red, Green, Blue) ) tname := Colors.TypeName() fmt.Println(tname) }
Output: string
func (Enum[M, V]) Value ΒΆ
func (e Enum[M, V]) Value(member M) V
Value returns the wrapped value of the given enum member.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/enum" ) func main() { type Color enum.Member[string] var ( Red = Color{"red"} Green = Color{"green"} Blue = Color{"blue"} Colors = enum.New(Red, Green, Blue) ) value := Colors.Value(Green) fmt.Println(value) }
Output: green
func (Enum[M, V]) Values ΒΆ
func (e Enum[M, V]) Values() []V
Values returns a slice of values of all members of the enum.
Example ΒΆ
package main import ( "fmt" "github.com/orsinium-labs/enum" ) func main() { type Color enum.Member[string] var ( Red = Color{"red"} Green = Color{"green"} Blue = Color{"blue"} Colors = enum.New(Red, Green, Blue) ) values := Colors.Values() fmt.Println(values) }
Output: [red green blue]
type Equaler ΒΆ added in v1.4.0
type Equaler[V comparable] interface { Equal(other V) bool comparable }
Equaler check if the two values of the same type are equal.
type Member ΒΆ
type Member[T comparable] struct { Value T }
Member is an enum member, a specific value bound to a variable.