Help us shape the future of Go! Take the 2026 Go Developer Survey

enum

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package enum describes generated named string and integer values without erasing their Go type. Transport manifests are an explicit serialization boundary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Case

type Case[E Scalar] struct {
	Name     string
	Value    E
	LabelKey i18n.MessageKey
}

Case retains a declared constant's Go name and concrete enum value.

type Definition

type Definition struct {
	PackagePath string     `json:"package"`
	Name        string     `json:"name"`
	Cases       []WireCase `json:"cases"`
}

Definition is the normalized serialized enum boundary for contract emitters. Values retain exact JSON numbers instead of passing through float64.

type Descriptor

type Descriptor[E Scalar] struct {
	// contains filtered or unexported fields
}

Descriptor is an immutable enum declaration. Its zero value is invalid. Copies share one validation result, so retained descriptors validate once.

func Describe

func Describe[E Scalar](packagePath, name string, cases ...Case[E]) Descriptor[E]

Describe is a declaration boundary used by generated code. It copies cases; registries and custom declarations must Validate before accepting a descriptor. Retain the result to reuse its cached validation across label lookups.

func (Descriptor[E]) Cases

func (d Descriptor[E]) Cases() []Case[E]

Cases returns a copy of the enum case slice.

func (Descriptor[E]) Contains

func (d Descriptor[E]) Contains(value E) bool

Contains reports whether the value matches a declared case.

func (Descriptor[E]) Definition

func (d Descriptor[E]) Definition() (Definition, error)

Definition validates the descriptor and builds its wire definition with JSON-encoded case values.

func (Descriptor[E]) Label

func (d Descriptor[E]) Label(ctx context.Context, catalog *i18n.Catalog, locale i18n.LocaleID, value E) (string, error)

Label resolves the enum value's label through the supplied locale catalog.

func (Descriptor[E]) LabelDefinitions

func (d Descriptor[E]) LabelDefinitions() ([]i18n.MessageDefinition, error)

LabelDefinitions contributes parameter-free declarations without another enum value list. Unlabeled cases are omitted and shared keys contribute once, in first-case order. Callers may combine these with typed message definitions when constructing one catalog.

func (Descriptor[E]) LabelKey

func (d Descriptor[E]) LabelKey(value E) (i18n.MessageKey, error)

LabelKey uses the same case list as enum membership and client metadata. Descriptor validation is cached, so a retained descriptor does not repeat it.

func (Descriptor[E]) Name

func (d Descriptor[E]) Name() string

Name returns the enum type name.

func (Descriptor[E]) PackagePath

func (d Descriptor[E]) PackagePath() string

PackagePath returns the enum package path.

func (Descriptor[E]) Validate

func (d Descriptor[E]) Validate() error

Validate checks descriptor names, values and their actual JSON representation. It does not infer public API exposure from a persistence model. The result is computed once per Describe call; custom enum marshalers run only that once.

type Scalar

type Scalar interface {
	~string | ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

Scalar is the set of supported enum underlying types. Platform pointers and floating-point values are not enum values.

type WireCase

type WireCase struct {
	Name     string          `json:"name"`
	Value    json.RawMessage `json:"value"`
	LabelKey i18n.MessageKey `json:"label_key,omitempty"`
}

WireCase describes an enum case's name, JSON value, and optional label key.

Jump to

Keyboard shortcuts

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