Documentation
¶
Overview ¶
Package enum provides a standard enum interface for creating a list of enums. Enums are indexed, iterable, and namespaced.
Flags or flag based enums are still best handled by [Iota]
Basic Enum ¶
Declare an Enum type by embedding Member in a struct.
type Color struct {
enum.Member
}
This enum type can now be used to create an Enum list or namespace.
type Colors struct {
Red Color
Green Color
Blue Color
}
The enums can now be initialized by passing your struct to the Define function.
Colors := enum.Define(Colors{})
Enhanced Enum ¶
Because an enum is just a struct, we can build enhanced enums that carry additional data.
type Vehicle struct {
enum.Member
Tires int
Passengers int
CarbonPerKilometer int
}
These are identical to our basic enum in every other way. Unlike the name and index of enums, the additional data is technically mutable. By convention enhanced enum data should be treated as immutable, though mutation may be desirable in some cases.
Create an Enum list or namespace.
type Vehicles struct {
Car Vehicle
Bus Vehicle
Bicycle Vehicle
}
The additional data can then be populated with their non-zero values when passing the struct declaration to Define.
Vehicle := enum.Define(Vehicles{
Car: Vehicle{
Tires: 4,
Passengers: 5,
CarbonPerKilometer: 400,
},
Bus: Vehicle{
Tires: 6,
Passengers: 50,
CarbonPerKilometer: 800,
},
Bicycle: Vehicle{
Tires: 2,
Passengers: 1,
CarbonPerKilometer: 0,
},
})
Because package enum uses reflection to initialize, it may be advisable to declare your list globally and pass it to Define in the init function
Example ¶
package main
import "fmt"
type Fruit struct {
Member
}
var fruits = struct {
Apple Fruit
Banana Fruit
Coconut Fruit
Grapes Fruit
}{}
func main() {
fruits = Define(fruits)
fmt.Printf("I like %ss, %ss, and %s", fruits.Banana, fruits.Coconut, fruits.Grapes)
}
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrUninitialized = errors.New("Enum is Zero Value") ErrEnumNotFound = errors.New("Enum not found") )
Functions ¶
func All ¶ added in v0.2.0
All provides allocation-free iteration over all enum members. Yields Member
No yield for any internal error
func Define ¶
func Define[T any](schema T) T
Define registers the struct enum namespace and uses reflection over the struct fields to initialize each enum member
Types ¶
type Enum ¶
type Enum interface {
Index() int
Name() string
String() string
// contains filtered or unexported methods
}
Enum is a package sealed interface to identify the enum type
type Member ¶
type Member struct {
// contains filtered or unexported fields
}
Member is embedded in a struct to mark the struct type as an enum
The zero value of Member is a nil definition signifying the enum is not initialized
func ByIndex ¶ added in v0.2.0
ByIndex returns the enum member by the index os its position in the enum list
func ByName ¶ added in v0.2.0
ByName returns the enum member by name. Member zero value with false is returned if member name does not return initialized enum member
func Values ¶ added in v0.2.0
Values returns a defensive copy of the definition's slice of Values
an empty Member slice is returned for any internal error
func (Member) MarshalText ¶
MarshalText marshals the enum member name