Documentation
¶
Overview ¶
Package enum provides a simple enum implementation.
Index ¶
- type Enum
- type Item
- type Pair
- type Registry
- func (r *Registry[T]) Add(items ...*Item[T])
- func (r *Registry[T]) Contains(value T) bool
- func (r *Registry[T]) Filter(predicate func(*Item[T]) bool) []*Item[T]
- func (r *Registry[T]) GetByName(name string) (*Item[T], bool)
- func (r *Registry[T]) GetByValue(value T) (*Item[T], bool)
- func (r *Registry[T]) Items() []*Item[T]
- func (r *Registry[T]) Range(fn func(*Item[T]) bool)
- func (r *Registry[T]) Remove(value T) bool
- func (r *Registry[T]) Size() int
- func (r *Registry[T]) SortedItems(less func(*Item[T], *Item[T]) bool) []*Item[T]
- func (r *Registry[T]) Update(value T, newName string) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Enum ¶
type Enum[T comparable] interface { Value() T String() string Name() string Valid(checker ...func(T) bool) bool }
Enum defines a common enum interface.
type Item ¶
type Item[T comparable] struct { // contains filtered or unexported fields }
Item defines a common enum item struct implement Enum interface.
func NewItem ¶
func NewItem[T comparable](value T, name string) *Item[T]
NewItem creates a new enum item.
Example ¶
item1 := NewItem(Active, "Active") item2 := NewItem(Inactive, "Inactive") fmt.Println(item1.Name(), item1.Value()) fmt.Println(item2.Name(), item2.Value())
Output: Active 1 Inactive 2
func NewItemsFromPairs ¶
func NewItemsFromPairs[T comparable](pairs ...Pair[T]) []*Item[T]
NewItemsFromPairs creates enum items from a slice of Pair structs.
Example ¶
items := NewItemsFromPairs(
Pair[Status]{Value: Active, Name: "Active"},
Pair[Status]{Value: Inactive, Name: "Inactive"},
)
fmt.Println(items[0].Name(), items[0].Value())
fmt.Println(items[1].Name(), items[1].Value())
Output: Active 1 Inactive 2
func (*Item[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
Example ¶
item := NewItem(Active, "Active") data, _ := item.MarshalJSON() fmt.Println(string(data)) var unmarshaledItem Item[Status] _ = unmarshaledItem.UnmarshalJSON(data) fmt.Println(unmarshaledItem.Name(), unmarshaledItem.Value())
Output: {"name":"Active","value":1} Active 1
func (*Item[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
func (*Item[T]) Valid ¶
Valid checks if the enum item is valid. If a custom check function is provided, it will be used to validate the value.
Example ¶
item := NewItem(Active, "Active") fmt.Println(item.Valid()) invalidItem := NewItem(Unknown, "") fmt.Println(invalidItem.Valid())
Output: true false
type Pair ¶
type Pair[T comparable] struct { Value T Name string }
Pair represents a value-name pair for creating enum items
type Registry ¶
type Registry[T comparable] struct { // contains filtered or unexported fields }
Registry defines a common enum registry struct.
func NewRegistry ¶
func NewRegistry[T comparable](items ...*Item[T]) *Registry[T]
NewRegistry creates a new enum registry.
func (*Registry[T]) Add ¶
Add adds enum items to the registry.
Example ¶
registry := NewRegistry[Status]()
item1 := NewItem(Active, "Active")
item2 := NewItem(Inactive, "Inactive")
registry.Add(item1, item2)
if item, found := registry.GetByValue(Active); found {
fmt.Println("Found by value:", item.Name())
}
if item, found := registry.GetByName("Inactive"); found {
fmt.Println("Found by name:", item.Value())
}
Output: Found by value: Active Found by name: 2
func (*Registry[T]) Contains ¶
Contains checks if an enum item with the given value exists in the registry.
Example ¶
registry := NewRegistry[Status]() item := NewItem(Active, "Active") registry.Add(item) fmt.Println(registry.Contains(Active)) fmt.Println(registry.Contains(Inactive))
Output: true false
func (*Registry[T]) Filter ¶
Filter returns a slice of enum items that satisfy the given predicate function.
Example ¶
registry := NewRegistry[Status]()
item1 := NewItem(Active, "Active")
item2 := NewItem(Inactive, "Inactive")
registry.Add(item1, item2)
activeItems := registry.Filter(func(item *Item[Status]) bool {
return item.Value() == Active
})
for _, item := range activeItems {
fmt.Println(item.Name(), item.Value())
}
Output: Active 1
func (*Registry[T]) GetByValue ¶
GetByValue retrieves an enum item by its value.
func (*Registry[T]) Items ¶
Items returns a slice of all enum items in the registry.
Example ¶
registry := NewRegistry[Status]()
item1 := NewItem(Active, "Active")
item2 := NewItem(Inactive, "Inactive")
registry.Add(item1, item2)
for _, item := range registry.Items() {
fmt.Println(item.Name(), item.Value())
}
Output: Active 1 Inactive 2
func (*Registry[T]) Range ¶
Range iterates over all enum items in the registry and applies the given function.
Example ¶
registry := NewRegistry[Status]()
item1 := NewItem(Active, "Active")
item2 := NewItem(Inactive, "Inactive")
registry.Add(item1, item2)
registry.Range(func(item *Item[Status]) bool {
fmt.Println(item.Name(), item.Value())
return true // continue iteration
})
Output: Active 1 Inactive 2
func (*Registry[T]) Remove ¶
Remove removes an enum item from the registry by its value.
Example ¶
registry := NewRegistry[Status]()
item1 := NewItem(Active, "Active")
registry.Add(item1)
fmt.Println("Size before removal:", registry.Size())
removed := registry.Remove(Active)
fmt.Println("Removed:", removed)
fmt.Println("Size after removal:", registry.Size())
Output: Size before removal: 1 Removed: true Size after removal: 0
func (*Registry[T]) Size ¶
Size returns the number of enum items in the registry.
Example ¶
registry := NewRegistry[Status]()
fmt.Println("Initial size:", registry.Size())
item1 := NewItem(Active, "Active")
item2 := NewItem(Inactive, "Inactive")
registry.Add(item1, item2)
fmt.Println("Size after adding items:", registry.Size())
registry.Remove(Active)
fmt.Println("Size after removing an item:", registry.Size())
Output: Initial size: 0 Size after adding items: 2 Size after removing an item: 1
func (*Registry[T]) SortedItems ¶
SortedItems returns a slice of all enum items sorted by the given less function.
Example ¶
registry := NewRegistry[Status]()
item1 := NewItem(Inactive, "Inactive")
item2 := NewItem(Active, "Active")
registry.Add(item1, item2)
for _, item := range registry.SortedItems(func(i1, i2 *Item[Status]) bool {
return i1.value < i2.value
}) {
fmt.Println(item.Name(), item.Value())
}
Output: Active 1 Inactive 2
func (*Registry[T]) Update ¶
Update updates the name of an enum item in the registry by its value.
Example ¶
registry := NewRegistry[Status]()
item1 := NewItem(Active, "Active")
registry.Add(item1)
updated := registry.Update(Active, "Activated")
fmt.Println("Updated:", updated)
if item, found := registry.GetByValue(Active); found {
fmt.Println("New name:", item.Name())
}
Output: Updated: true New name: Activated