internal

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2025 License: MIT Imports: 4 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 FromBinary added in v0.1.1

func FromBinary[T ~int](labels []string, data []byte) (T, error)

FromBinary deserializes binary into an enum value (for encoding.BinaryUnmarshaler).

func FromJSON

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

FromJSON deserializes JSON into an enum value.

func FromSQLValue added in v0.1.1

func FromSQLValue[T ~int](labels []string, src any) (T, error)

FromSQLValue deserializes an SQL value into an enum value (for sql.Scanner).

func FromText added in v0.1.1

func FromText[T ~int](labels []string, text []byte) (T, error)

FromText deserializes text into an enum value (for encoding.TextUnmarshaler).

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 ToBinary added in v0.1.1

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

ToBinary serializes an enum value into binary (for encoding.BinaryMarshaler).

func ToJSON

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

ToJSON serializes an enum value into JSON.

func ToSQLValue added in v0.1.1

func ToSQLValue[T ~int](labels []string, v T) (driver.Value, error)

ToSQLValue serializes an enum value for SQL storage (for driver.Valuer).

func ToText added in v0.1.1

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

ToText serializes an enum value into text (for encoding.TextMarshaler).

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

type ErrBinaryDataTooShort added in v0.1.1

type ErrBinaryDataTooShort struct {
	Expected int
	Actual   int
}

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

type ErrBinaryDataTruncated struct {
	Expected int
	Actual   int
}

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

type ErrInvalidEnumValue struct {
	Value       string
	ValidValues []string
}

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

type ErrLabelTooLong struct {
	Length    int
	MaxLength int
}

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.

Jump to

Keyboard shortcuts

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