Documentation
¶
Index ¶
- Constants
- func BuildLabelMap[T ~int](labels []string) map[string]T
- func FromBinary[T ~int](labels []string, data []byte) (T, error)
- func FromJSON[T ~int](labels []string, b []byte) (T, error)
- func FromSQLValue[T ~int](labels []string, src any) (T, error)
- func FromText[T ~int](labels []string, text []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 ToBinary[T ~int](labels []string, v T) ([]byte, error)
- func ToJSON[T ~int](labels []string, v T) ([]byte, error)
- func ToSQLValue[T ~int](labels []string, v T) (driver.Value, error)
- func ToText[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
- type ErrBinaryDataTooShort
- type ErrBinaryDataTruncated
- type ErrInvalidEnumValue
- type ErrLabelTooLong
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 FromBinary ¶ added in v0.1.1
FromBinary deserializes binary into an enum value (for encoding.BinaryUnmarshaler).
func FromSQLValue ¶ added in v0.1.1
FromSQLValue deserializes an SQL value into an enum value (for sql.Scanner).
func FromText ¶ added in v0.1.1
FromText deserializes text into an enum value (for encoding.TextUnmarshaler).
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 ToBinary ¶ added in v0.1.1
ToBinary serializes an enum value into binary (for encoding.BinaryMarshaler).
func ToSQLValue ¶ added in v0.1.1
ToSQLValue serializes an enum value for SQL storage (for driver.Valuer).
func ToText ¶ added in v0.1.1
ToText serializes an enum value into text (for encoding.TextMarshaler).
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
type ErrBinaryDataTooShort ¶ added in v0.1.1
ErrBinaryDataTooShort is returned when binary data is too short to contain valid data.
func NewBinaryDataTooShortError ¶ added in v0.1.1
func NewBinaryDataTooShortError(expected, actual int) *ErrBinaryDataTooShort
NewBinaryDataTooShortError creates a new ErrBinaryDataTooShort.
func (*ErrBinaryDataTooShort) Error ¶ added in v0.1.1
func (e *ErrBinaryDataTooShort) Error() string
func (*ErrBinaryDataTooShort) Is ¶ added in v0.1.1
func (e *ErrBinaryDataTooShort) Is(target error) bool
Is implements the errors.Is interface for error comparison.
type ErrBinaryDataTruncated ¶ added in v0.1.1
ErrBinaryDataTruncated is returned when binary data is truncated.
func NewBinaryDataTruncatedError ¶ added in v0.1.1
func NewBinaryDataTruncatedError(expected, actual int) *ErrBinaryDataTruncated
NewBinaryDataTruncatedError creates a new ErrBinaryDataTruncated.
func (*ErrBinaryDataTruncated) Error ¶ added in v0.1.1
func (e *ErrBinaryDataTruncated) Error() string
func (*ErrBinaryDataTruncated) Is ¶ added in v0.1.1
func (e *ErrBinaryDataTruncated) Is(target error) bool
Is implements the errors.Is interface for error comparison.
type ErrInvalidEnumValue ¶ added in v0.1.1
ErrInvalidEnumValue is returned when trying to unmarshal an invalid enum value.
func NewInvalidEnumValueError ¶ added in v0.1.1
func NewInvalidEnumValueError(value string, validValues []string) *ErrInvalidEnumValue
NewInvalidEnumValueError creates a new ErrInvalidEnumValue.
func (*ErrInvalidEnumValue) Error ¶ added in v0.1.1
func (e *ErrInvalidEnumValue) Error() string
func (*ErrInvalidEnumValue) Is ¶ added in v0.1.1
func (e *ErrInvalidEnumValue) Is(target error) bool
Is implements the errors.Is interface for error comparison.
type ErrLabelTooLong ¶ added in v0.1.1
ErrLabelTooLong is returned when a label exceeds the maximum allowed length for binary encoding.
func NewLabelTooLongError ¶ added in v0.1.1
func NewLabelTooLongError(length, maxLength int) *ErrLabelTooLong
NewLabelTooLongError creates a new ErrLabelTooLong.
func (*ErrLabelTooLong) Error ¶ added in v0.1.1
func (e *ErrLabelTooLong) Error() string
func (*ErrLabelTooLong) Is ¶ added in v0.1.1
func (e *ErrLabelTooLong) Is(target error) bool
Is implements the errors.Is interface for error comparison.