internal

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// InvalidLabel is the default label used for invalid enum values
	InvalidLabel = "Invalid"

	// DefaultLookupThreshold defines when to switch from linear to map-based lookup
	DefaultLookupThreshold = 10
)

Constants used throughout the enum package

View Source
const LookupThreshold = DefaultLookupThreshold

LookupThreshold defines when to use map-based lookup vs linear search

Variables

This section is empty.

Functions

func BuildLabelMap

func BuildLabelMap[T ~int](labels []string) map[string]T

BuildLabelMap creates a map for string-to-index lookup. Used when the map will be reused multiple times.

func FromJSON

func FromJSON[T ~int](labels []string, b []byte) (T, error)

FromJSON deserializes JSON into an enum value.

func FromYAML

func FromYAML[T ~int](labels []string, unmarshal func(any) error) (T, error)

FromYAML deserializes YAML into an enum value.

func IsValidIndex

func IsValidIndex[T ~int](labels []string, index T) bool

IsValidIndex checks if an index is within bounds (returns bool instead of error)

func SafeGetLabel

func SafeGetLabel[T ~int](labels []string, index T, defaultLabel string) string

SafeGetLabel returns the label for an index, or a default value if invalid

func SafeGetLabelWithError

func SafeGetLabelWithError[T ~int](labels []string, index T) (string, error)

SafeGetLabelWithError returns the label for an index, or an error if invalid

func StringToIndex

func StringToIndex[T ~int](labels []string, target string) (T, bool)

StringToIndex performs optimized string-to-index lookup. Uses map-based lookup for large slices, linear search for small ones.

func ToJSON

func ToJSON[T ~int](labels []string, v T) ([]byte, error)

ToJSON serializes an enum value into JSON.

func ToYAML

func ToYAML[T ~int](labels []string, v T) (any, error)

func ValidateIndex

func ValidateIndex[T ~int](labels []string, index T) error

ValidateIndex checks if an index is within bounds for the given labels

Types

type CacheBuilder

type CacheBuilder[T ~int] struct {
	// contains filtered or unexported fields
}

CacheBuilder helps build cached data structures for enum optimization

func NewCacheBuilder

func NewCacheBuilder[T ~int](labels []string) *CacheBuilder[T]

NewCacheBuilder creates a new cache builder

func (*CacheBuilder[T]) BuildAllValues

func (cb *CacheBuilder[T]) BuildAllValues() []T

BuildAllValues creates a pre-computed slice of all enum values

func (*CacheBuilder[T]) BuildLookupMap

func (cb *CacheBuilder[T]) BuildLookupMap() map[string]T

BuildLookupMap creates a lookup map for string-to-value conversion

func (*CacheBuilder[T]) ShouldUseCachedLookup

func (cb *CacheBuilder[T]) ShouldUseCachedLookup() bool

ShouldUseCachedLookup determines if a cached lookup map should be used based on the number of labels and expected usage patterns

Jump to

Keyboard shortcuts

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