enum

package
v0.0.1-alpha Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidType is returned when an invalid type is used for an enum
	// This is usually caused when a type is deserialized from a network request and we want to validate its in the Set
	ErrInvalidType = errors.New("invalid enum type")
)

Functions

This section is empty.

Types

type ID

type ID interface {
	~string | int
}

type Item

type Item[T any] struct {
	ID    T
	Label string
}

func NewItem

func NewItem[T ID](id T, label string) Item[T]

type Set

type Set[T ID] struct {
	// contains filtered or unexported fields
}

Set is a generic set of enums This enables users to create a set of enums keyed by a custom type This set provides functionality for adding, getting, and validating enums of that type within the set

func NewSet

func NewSet[T ID](items ...Item[T]) Set[T]

func NewSetFromMap

func NewSetFromMap[T ID](m map[T]string) Set[T]

func (*Set[T]) Add

func (s *Set[T]) Add(id T, label string) T

Add adds a new enum to the set

func (*Set[T]) Get

func (s *Set[T]) Get(id T) (Item[T], bool)

Get returns the enum item by its id

func (*Set[T]) GetOrError

func (s *Set[T]) GetOrError(id T) (Item[T], error)

GetOrError returns the enum item by its id or an error if the item is not found

func (*Set[T]) Has

func (s *Set[T]) Has(id T) bool

Has returns true if the enum item exists in the set

func (*Set[T]) Validate

func (s *Set[T]) Validate(id T) (err error)

Validate returns an error if the enum item is not found in the set

Jump to

Keyboard shortcuts

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