enum

package
v2.3.8 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package enum provides a simple enum implementation.

Index

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

func (it *Item[T]) MarshalJSON() ([]byte, error)

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]) Name

func (it *Item[T]) Name() string

Name returns the name of the enum item.

func (*Item[T]) String

func (it *Item[T]) String() string

String returns the string representation of the enum item.

func (*Item[T]) UnmarshalJSON

func (it *Item[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Item[T]) Valid

func (it *Item[T]) Valid(checker ...func(T) bool) bool

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

func (*Item[T]) Value

func (it *Item[T]) Value() T

Value returns the value of the enum item.

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

func (r *Registry[T]) Add(items ...*Item[T])

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

func (r *Registry[T]) Contains(value T) bool

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

func (r *Registry[T]) Filter(predicate func(*Item[T]) bool) []*Item[T]

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]) GetByName

func (r *Registry[T]) GetByName(name string) (*Item[T], bool)

GetByName retrieves an enum item by its name.

func (*Registry[T]) GetByValue

func (r *Registry[T]) GetByValue(value T) (*Item[T], bool)

GetByValue retrieves an enum item by its value.

func (*Registry[T]) Items

func (r *Registry[T]) Items() []*Item[T]

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

func (r *Registry[T]) Range(fn func(*Item[T]) bool)

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

func (r *Registry[T]) Remove(value T) bool

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

func (r *Registry[T]) Size() int

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

func (r *Registry[T]) SortedItems(less func(*Item[T], *Item[T]) bool) []*Item[T]

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

func (r *Registry[T]) Update(value T, newName string) bool

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

Jump to

Keyboard shortcuts

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