enums

package
v1.21.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 5 Imported by: 3

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal[Enum ~string](value Enum, allowed map[Enum]struct{}) ([]byte, error)

func Scan

func Scan[Enum ~string](src any, out *Enum, allowed map[Enum]struct{}) error

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{}

func Unmarshal

func Unmarshal[Enum ~string](b []byte, out *Enum, allowed map[Enum]struct{}) error

func Validate

func Validate[Enum ~string](value Enum, allowed map[Enum]struct{}) error

func Value

func Value[Enum ~string](value Enum, allowed map[Enum]struct{}) (driver.Value, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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