Documentation
¶
Index ¶
- Constants
- func BuildLabelMap[T ~int](labels []string) map[string]T
- func FromJSON[T ~int](labels []string, b []byte) (T, error)
- func FromYAML[T ~int](labels []string, unmarshal func(any) error) (T, error)
- func IsValidIndex[T ~int](labels []string, index T) bool
- func SafeGetLabel[T ~int](labels []string, index T, defaultLabel string) string
- func SafeGetLabelWithError[T ~int](labels []string, index T) (string, error)
- func StringToIndex[T ~int](labels []string, target string) (T, bool)
- func ToJSON[T ~int](labels []string, v T) ([]byte, error)
- func ToYAML[T ~int](labels []string, v T) (any, error)
- func ValidateIndex[T ~int](labels []string, index T) error
- type CacheBuilder
Constants ¶
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
const LookupThreshold = DefaultLookupThreshold
LookupThreshold defines when to use map-based lookup vs linear search
Variables ¶
This section is empty.
Functions ¶
func BuildLabelMap ¶
BuildLabelMap creates a map for string-to-index lookup. Used when the map will be reused multiple times.
func IsValidIndex ¶
IsValidIndex checks if an index is within bounds (returns bool instead of error)
func SafeGetLabel ¶
SafeGetLabel returns the label for an index, or a default value if invalid
func SafeGetLabelWithError ¶
SafeGetLabelWithError returns the label for an index, or an error if invalid
func StringToIndex ¶
StringToIndex performs optimized string-to-index lookup. Uses map-based lookup for large slices, linear search for small ones.
func ValidateIndex ¶
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