Code generation

  1. Install
  2. The definition file
  3. What you get
  4. Declaring a state machine
  5. Errors the generator catches

enumx generate reads a definition file, parses its AST, and writes a sibling *_enumx_gen.go containing the named type, typed constants, a struct accessor, and — if you declared any — a state machine.

Install

go install github.com/bakhod1r/enumx/cmd/enumx@latest

The definition file

Create enums.go with the //go:build ignore directive so it is excluded from normal builds:

//go:build ignore

//go:generate enumx generate enums.go

package main

import "github.com/bakhod1r/enumx"

// Auto type inference: the type name is derived from the variable (Status → StatusType)
var Status = enumx.Define("pending", "active", "blocked")

// Explicit naming: an uppercase, space-free first argument names the type
var Roles = enumx.Define("Role", "admin", "user", "guest")

enumx.Define never runs. It exists only so the definition file compiles and so the generator has an AST to read.

Because an uppercase first argument is treated as a type name, enumx.Define("Active", "Inactive") declares a type called Active with one value. Use lowercase values, or name the type explicitly.

Then:

go generate ./...

What you get

For var Status = enumx.Define("pending", "active", "blocked"):

type StatusType string

const (
    StatusTypePending StatusType = "pending"
    StatusTypeActive  StatusType = "active"
    StatusTypeBlocked StatusType = "blocked"
)

type StatusEnum struct {
    Pending StatusType
    Active  StatusType
    Blocked StatusType
    *enumx.Def[StatusType]
}

var Status = &StatusEnum{ /* ... */ }

func (v StatusType) String() string  { /* ... */ }
func (v StatusType) IsValid() bool   { /* ... */ }

The generated init() also calls enumx.Register, so every package-level generic — enumx.Parse, enumx.Values, enumx.Set, enumx.NullEnum, aliases, metadata — works with the type immediately.

Use it:

Status.Active               // StatusType("active")
Status.Has(Status.Active)   // true
Status.Values()             // [pending active blocked]

val, err := enumx.ParseString(Status.Def, r.FormValue("status"))

Declaring a state machine

enumx.Transitions declares the legal edges of an enum defined in the same file. Each edge is written from>to, and one source may list several targets separated by commas:

var Order = enumx.Define("pending", "paid", "shipped", "cancelled")

var _ = enumx.Transitions("Order",
    "pending>paid,cancelled",
    "paid>shipped,cancelled",
)

The generator registers the transitions and adds three methods to the type:

state := Order.Pending

state.NextStates()                      // [paid cancelled]
state.CanTransitionTo(Order.Shipped)    // false

next, err := state.To(Order.Paid)       // returns the new state, or an error

To returns the current state unchanged when the transition is illegal, so a rejected move can never corrupt your value. The generated file also carries a stateDiagram-v2 comment you can paste into any Mermaid renderer.

Every state named in a transition is checked against the enum’s values at generation time — a typo is a build error, not an edge that silently never fires.

Errors the generator catches

Rather than emitting code that fails to compile later, generation fails immediately on:

  • a value that cannot become a Go identifier ("", "-", or a keyword);
  • two values that collapse to the same field name ("in-progress" and "in_progress");
  • the same value declared twice;
  • two enums that would generate the same type name;
  • a non-literal argument to Define or Transitions;
  • a transition referencing an unknown enum or an unknown state.