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 or Builder.AddValue 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
func NewStrictBuilder ¶ added in v1.5.0
func NewStrictBuilder[V comparable, M iMember[V]]() Builder[M, V]
NewStrictBuilder creates a new Builder, a constructor for an Enum, that panics if two members share the same value.
func (*Builder[M, V]) Add ¶ added in v1.3.0
func (b *Builder[M, V]) Add(m M) M
Add registers a new Member in the builder.
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.