ADR 0035: Enums travel by name, and string-backed enums
Status: Accepted (2026-10-25). Amends 0025. Breaking: the JSON form of every enum changes.
Context
Section titled “Context”enum Status { Active, Suspended } lowers to type Status int with iota
constants and a generated String() (ADR 0025). That representation is right
inside a program, but it has four problems at the edges, where backend code
lives.
- Clients can crash an exhaustive
when.encoding/jsonencodes the number ("status":1) and decodes any number. A client sending{"status": 7}gets aStatus(7)without error, and the next exhaustivewhenpanics:lyth: no when branch for this Status. - Stored data depends on declaration order. Reordering entries, or inserting one in the middle, silently changes what stored and transmitted numbers mean.
- Text columns do not round-trip. pgx (v5.11) encodes a named integer type
as an integer. For a text column it falls back to
fmt.Stringerand writes"Paid", then cannot scan"Paid"back:cannot scan text (OID 25) in text format into *main.Status. pgx never consultsencoding.TextMarshaler. Making one enum work with JSON, pgx, anddatabase/sqltook six hand-written methods (about 27 lines) in the review.String()is generated and cannot be redefined (E0102), so a lower-case wire spelling needs yet another method. - No enum can match an existing contract. An enum cannot match a
specification that fixes the wire values (
"in_progress","PAYMENT_FAILED"). Go code usestype Status stringand constants for this (the most common enum shape in Go APIs), which Lyth cannot declare today.
Other languages default to names on the wire: Rust’s serde, Kotlin’s
serialization and Jackson, Swift’s Codable with raw values, and protobuf’s
JSON mapping.
Decision
Section titled “Decision”Plain enums: names on the wire
Section titled “Plain enums: names on the wire”enum Status { Active, Suspended, PendingReview } keeps its Go
representation: type Status int, iota constants named StatusActive, and
String() returning the entry name. The zero value is still the first entry.
Lyth additionally generates:
// MarshalText returns the wire name of the Status entry.func (s Status) MarshalText() ([]byte, error) { switch s { case StatusActive: return []byte("active"), nil case StatusSuspended: return []byte("suspended"), nil case StatusPendingReview: return []byte("pendingReview"), nil } return nil, fmt.Errorf("invalid Status %d", int(s))}
// UnmarshalText sets s to the entry with the given wire name.func (s *Status) UnmarshalText(text []byte) error { switch string(text) { case "active": *s = StatusActive case "suspended": *s = StatusSuspended case "pendingReview": *s = StatusPendingReview default: return fmt.Errorf("invalid Status %q", text) } return nil}- Wire name. The wire name of an entry is its name under the automatic
jsonkey rule (ADR 0036):Active→active,PendingReview→pendingReview. JSON keys and enum values then share one case convention. - Unknown values are rejected.
UnmarshalTextrejects any other text, so decoding untrusted input can no longer produce a value outside the set.whenstays sound, and an invalid request becomes a decode error (400) instead of a panic (500). - Who uses the methods.
encoding/json(v1 and v2),flag.TextVar, slog’s handlers, and most text formats use them. In logs, slog then showsactive, whilefmtand string templates keep usingString()(Active). - Overriding. An enum may declare
MarshalText/UnmarshalTextitself; Lyth then generates neither. It declares both or neither (E0216 otherwise). Text methods always put text on the wire, so the escape hatch for keeping numbers in JSON isMarshalJSON/UnmarshalJSON, whichencoding/jsonprefers to the generated pair. - Wire names are unique. Two entries whose wire names coincide (
IdandIDboth giveid) are E0102: rename one, or declare the pair. - Databases. Plain enums store their number in integer columns, as today. Text columns use a string-backed enum.
String-backed enums: Go’s type T string with exhaustiveness
Section titled “String-backed enums: Go’s type T string with exhaustiveness”enum Plan: string { Free = "free", Pro = "pro", Enterprise = "enterprise",}import "fmt"
type Plan string
const ( PlanFree Plan = "free" PlanPro Plan = "pro" PlanEnterprise Plan = "enterprise")
// String returns the value of the Plan entry.func (p Plan) String() string { return string(p)}
// UnmarshalText sets p to the Plan entry with the given value.func (p *Plan) UnmarshalText(text []byte) error { switch v := Plan(text); v { case PlanFree, PlanPro, PlanEnterprise: *p = v return nil } return fmt.Errorf("invalid Plan %q", text)}- Lowering. It lowers to Go’s idiom:
type Plan string, typed constantsPlanFree Plan = "free", aString()that returns the value, and anUnmarshalTextthat accepts exactly the declared values. - What works.
encoding/jsonencodes the value. pgx anddatabase/sqlwrite and scan it as a string with no extra methods. Exhaustivewhen, entry names insidewhen,==,Plan.Pro, andstring(p)work as for plain enums. - Values. Each entry needs a value, a string literal (E0216), and the
values must be distinct (E0102). A plain enum’s entry with a value is
E0601, with a hint naming the string-backed form.
stringis the only backing type; numeric wire codes use a defined type and constants (ADR 0037), which are not a closed set. - Zero value.
""is not an entry unless one is declared with"". Without one, the type has no zero value in Lyth: a field of this type must be given at construction, andp: Planwithout an initializer is E0405, like a non-null pointer (types.md §6). Reflection can still produce""(a JSON field left out decodes to it), andwhenthen panics as for any value outside the set. Input types should declare such fieldsPlan?, whichomitzeroand validators treat as optional.
Alternatives
Section titled “Alternatives”- Verbatim entry names on the wire (
"PendingReview"). This matchesString(), Rust, Kotlin, and protobuf’s JSON mapping, and log output stays identical to the wire. It is rejected because JSON keys are lower-camel in Lyth (ADR 0013, 0022), and PascalCase values next to lower-camel keys look foreign in most APIs. This is the one real choice in this ADR; accepting verbatim names changes only the wire-name rule above. - Make every enum string-backed. One representation, and database-ready,
but the zero value is no longer an entry. Every plain enum field would
become required, and
int(c)/Color(n)conversions disappear. - Generate
Scan/Valueas well, so plain enums use text columns. This importsdatabase/sql/driverinto every package with an enum and adds two methods each; string-backed enums give the same result through the type itself. - Keep numbers and validate in
UnmarshalJSON. It fixes the crash but keeps order-dependent wire values and still cannot match a fixed contract.
Consequences
Section titled “Consequences”- Wire format. The JSON form of every plain enum changes from a number to a name. Nothing else changes in Lyth code; Go callers see two more methods.
- Generated code. Each plain enum gets about 25 more lines of Go. There is no run-time cost unless the methods are called, and they cost what hand-written ones would.
- Exhaustiveness at the edge. Exhaustive
whenbecomes safe on decoded input, which was the point of exhaustiveness for backend code. - Validation. validator’s
requiredstill cannot tell a missing plain enum from its first entry. The guide recommendsStatus?for optional input, or a first entry such asUnknownwhen “missing” must be detected.
Migration
Section titled “Migration”- No rewrite needed. No source rewrite is needed: existing enum
declarations keep compiling. The change is in what they put on the wire, so
it is a release-note item. The guide’s version table gains a row: “Enums
encode as names in JSON; declare
MarshalText/UnmarshalTextto keep numbers”. - New compiler error. For one release, an enum that already declares only one of the pair is an error naming the other.
Implementation
Section titled “Implementation”- Generated methods.
enumDeclininternal/lower/lower.gogenerates the methods besideString();fmtis imported only when they are generated. The checker decides whether to generate them once every method is declared (finishEnumsininternal/check/decl.go) and adds them to the shadow type, soencoding.TextMarshalerand interface checks see them. - Parser.
enum Name: string { Entry = "value", ... }. - Checker.
resolveEnumchecks values and wire names; a string-backed enum without a""entry is not zeroable. - Tests.
run/enumwire(JSON v1 and v2 round trips, rejected numbers and names, map keys, slog,MarshalJSONkeeping numbers),run/enums(the name in JSON),gen/enums,gen/stringenum,check/enumwire_neg(half-declared pair, duplicate values and wire names, missing and non-literal values),check/enumzero_neg, andexamples/todoapi/internal/todo/enumcolumn_test.lyth, which writes and scans a string-backed enum through pgx whenDATABASE_URLis set.