Guide: Enums, Union Types & Sum Types¶
This guide walks through the examples/enum-union-sum example.
Concept: Type Modeling — Enums, Unions, Sum Types
examples/enum-union-sum¶
Demonstrates all three Go type-modeling patterns side-by-side, with commentary on when to use each codec primitive:
-
Iota enum (string wire) —
type OrderStatus intwithiotaconstants. Bridged to string viaMapCodecSafe+validate.OneOf. Schema:{enum: ["pending", "approved", ...]}. -
Iota enum (integer wire) — Same named type, bridged to
intviaMapCodecSafe+validate.RangeInt. Schema:{minimum: 0, maximum: 3}. Use when integer representation matters on the wire (binary protocols, DB columns). -
Open union (
codex.Any()) — For dynamic config values or JSON blobs where the type set is open. Passes through unchanged with empty schema{}. -
Sum type (
TaggedUnion) —PaymentStatussealed interface withPending,Completed{TxID},Failed{Reason}. Discriminated by"status"field. Schema:{oneOf:[...], discriminator:{propertyName:"status"}}. -
Binary sum type (
Either2) — A product field that is either a plain SKU string OR an inline ProductRef object. Left branch tried first; right branch as fallback. Schema:{oneOf:[string, object]}.