Documentation
¶
Overview ¶
Package enum turns the usual `type Foo string` + const + hand-written switch boilerplate into a reusable, generics-based registry.
It offers two styles over one shared core:
- StrictEnum: a bare ~string type with one-line delegate methods that validate at the boundary (ScanString, ValueString, MarshalJSONString, UnmarshalJSONString).
- AutoEnum: the Member[T] wrapper with zero per-type code and explicit validation via the Enum[T] container.
The set of allowed values is a registry (Set[T]) built once via a constructor, never via reflect.
Index ¶
- Variables
- func MarshalJSONInt[T Signed](set *Set[T], value T) ([]byte, error)
- func MarshalJSONString[T ~string](set *Set[T], value T) ([]byte, error)
- func ScanInt[T Signed](set *Set[T], dst *T, src any) error
- func ScanString[T ~string](set *Set[T], dst *T, src any) error
- func UnmarshalJSONInt[T Signed](set *Set[T], dst *T, data []byte) error
- func UnmarshalJSONString[T ~string](set *Set[T], dst *T, data []byte) error
- func ValueInt[T Signed](set *Set[T], value T) (driver.Value, error)
- func ValueString[T ~string](set *Set[T], value T) (driver.Value, error)
- type Enum
- type Member
- type Set
- type Signed
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidValue is returned when a value is not a registered member of an enum. ErrInvalidValue = errors.New("enum: invalid value") // ErrNullValue is returned when a NULL/nil source is scanned into a non-pointer enum. ErrNullValue = errors.New("enum: null value") // ErrUnsupportedType is returned when a source type cannot be converted to the enum value. ErrUnsupportedType = errors.New("enum: unsupported type") )
Functions ¶
func MarshalJSONInt ¶
MarshalJSONInt implements json.Marshaler for a signed integer enum type and validates membership before encoding the value as a JSON number. It marshals the base int64, never the enum-typed value, to avoid recursing into the delegate.
func MarshalJSONString ¶
MarshalJSONString implements json.Marshaler for a ~string enum type and validates membership before encoding the value as a JSON string.
func ScanInt ¶
ScanInt implements sql.Scanner for a signed integer enum type and validates membership. It accepts an int64 source (the canonical driver representation) or a textual []byte/string; nil yields ErrNullValue and any other type yields ErrUnsupportedType.
func ScanString ¶
ScanString implements sql.Scanner for a ~string enum type and validates membership. It accepts a string or []byte source; nil yields ErrNullValue and any other type yields ErrUnsupportedType.
func UnmarshalJSONInt ¶
UnmarshalJSONInt implements json.Unmarshaler for a signed integer enum type. It decodes a JSON number into an int64 and validates membership before storing it into dst. Non-integer JSON numbers (e.g. 2.5) are rejected by the decoder.
func UnmarshalJSONString ¶
UnmarshalJSONString implements json.Unmarshaler for a ~string enum type. It decodes a JSON string and validates membership before storing the value into dst.
Types ¶
type Enum ¶
type Enum[T comparable] struct { // contains filtered or unexported fields }
Enum is the registry (container) of allowed Members. It is the explicit-validation counterpart to the non-validating Member methods.
func New ¶
func New[T comparable](members ...Member[T]) *Enum[T]
New builds an Enum from the given members, preserving first-seen order.
type Member ¶
type Member[T comparable] struct { // contains filtered or unexported fields }
Member is a generic enum member wrapping a single comparable value of type T.
It implements sql.Scanner, driver.Valuer, json.Marshaler and json.Unmarshaler once for every enum, so member types need zero per-type code. By Go's type system these methods cannot reach the specific Enum container, so they do NOT validate membership. Validate explicitly via Enum.Parse / Enum.Contains.
func Of ¶
func Of[T comparable](value T) Member[T]
Of constructs a Member holding value. T is normally inferred from value.
func (Member[T]) MarshalJSON ¶
MarshalJSON encodes the underlying value as JSON. It is reflect-free: encoding/json handles T directly. Membership is not validated here.
func (*Member[T]) Scan ¶
Scan implements sql.Scanner via the reflect scalar bridge. A nil source yields ErrNullValue. Membership is not validated here.
func (*Member[T]) UnmarshalJSON ¶
UnmarshalJSON decodes JSON into the underlying value. It is reflect-free and does not validate membership.
type Set ¶
type Set[T comparable] struct { // contains filtered or unexported fields }
Set is a reflect-free registry of allowed enum values for any comparable type T.
It is the core shared by both enum styles (StrictEnum and AutoEnum): a membership check plus deterministic enumeration. The registry is built once via NewSet and is read-only afterwards, so it is safe for concurrent reads.
func NewSet ¶
func NewSet[T comparable](values ...T) *Set[T]
NewSet builds a Set from the given values. Duplicates are stored once and the first-seen insertion order is preserved for Values, All and String.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
autoenum
command
Command autoenum demonstrates the AutoEnum style of go-enums: zero per-type code via the Member wrapper, with explicit validation through the Enum container.
|
Command autoenum demonstrates the AutoEnum style of go-enums: zero per-type code via the Member wrapper, with explicit validation through the Enum container. |
|
intenum
command
Command intenum demonstrates the StrictEnum style over a signed integer type: a bare int whose one-line delegate methods validate every value at the boundary.
|
Command intenum demonstrates the StrictEnum style over a signed integer type: a bare int whose one-line delegate methods validate every value at the boundary. |
|
strictenum
command
Command strictenum demonstrates the StrictEnum style of go-enums: a bare string type whose one-line delegate methods validate every value at the boundary.
|
Command strictenum demonstrates the StrictEnum style of go-enums: a bare string type whose one-line delegate methods validate every value at the boundary. |