Documentation
¶
Overview ¶
Package enums validates string-backed enums and adapts them to JSON and database/sql.
An enum that declares "" as a member treats "" as its unset value: Scan reads SQL NULL as "", Unmarshal reads JSON null as "", and Value and Marshal write "" as the empty string. A non-pointer enum that does not declare "" rejects "", SQL NULL and JSON null; a *Enum field takes NULL and null as nil without calling Scan or Unmarshal.
Index ¶
- func Marshal[Enum ~string](value Enum, allowed map[Enum]struct{}) ([]byte, error)
- func Scan[Enum ~string](src any, out *Enum, allowed map[Enum]struct{}) error
- func Set[Enum ~string](vals []Enum) map[Enum]struct{}
- func Unmarshal[Enum ~string](b []byte, out *Enum, allowed map[Enum]struct{}) error
- func Validate[Enum ~string](value Enum, allowed map[Enum]struct{}) error
- func Value[Enum ~string](value Enum, allowed map[Enum]struct{}) (driver.Value, error)
- func Values(t reflect.Type) ([]string, bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Scan ¶
Scan implements the database/sql Scanner contract for string-backed enums. A SQL NULL (src == nil) scans as the zero value "" and is accepted only if allowed contains "".
func Set ¶
func Set[Enum ~string](vals []Enum) map[Enum]struct{}
Set builds the allowed-values set for an enum type and records the values by type for OpenAPI generation. Calling it more than once for the same type (for example a full set and a request-only subset) records the union in first-seen order, so the published enum component always lists every value the type can carry. Narrow a single field with the `enum:"a,b"` struct tag.
Types ¶
This section is empty.