This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
enum is a Go code generator that creates type-safe, marshalable enum implementations from simple type definitions. It generates idiomatic Go code with zero runtime dependencies and supports JSON, SQL, MongoDB BSON, and YAML marshaling through optional flags.
# Run all tests (excludes examples)
go test ./...
# Run tests with race detection and coverage
go test -race -cover ./...
# Run a specific test
go test -run TestRuntimeIntegration ./internal/generator
# Run integration tests (includes MongoDB container tests)
go test ./internal/generator -v -run TestRuntimeIntegration
# Build the enum generator
go build
# Install globally
go install github.com/go-pkgz/enum@latest# Run linter (golangci-lint v2.0.2)
golangci-lint run
# Format code
gofmt -s -w .
goimports -w .# Generate using go:generate directives
go generate ./...
# Direct invocation examples
go run github.com/go-pkgz/enum@latest -type status -lower
go run github.com/go-pkgz/enum@latest -type status -lower -sql -bson -yaml- main.go - CLI entry point that parses flags and invokes the generator
- internal/generator/generator.go - Core generator logic:
- Parses Go AST to find enum constants
- Evaluates constant values including iota and binary expressions
- Generates code from template with conditional blocks for features
- internal/generator/enum.go.tmpl - Go template for generated code with conditional sections for SQL/BSON/YAML
- Type name must be lowercase (private) - Enforced to prevent confusion with public types
- Constants must be prefixed with type name - Ensures clear namespacing (e.g.,
statusActivefor typestatus) - Generated public types are capitalized - Follows Go conventions (private
status→ publicStatus) - Zero runtime dependencies - Generated code uses only stdlib unless optional features are enabled
- Conditional feature generation - SQL/BSON/YAML code only generated when flags are set to avoid forcing dependencies
- JSON: Via
encoding.TextMarshaler/TextUnmarshaler(always generated) - SQL (
-sqlflag): Implementsdatabase/sql/driver.Valuerandsql.Scanner- Smart NULL handling: uses zero value if available, errors otherwise
- BSON (
-bsonflag): ImplementsMarshalBSONValue/UnmarshalBSONValuefor MongoDB- Stores as string values, not documents
- YAML (
-yamlflag): Implementsyaml.Marshaler/yaml.Unmarshalerfor gopkg.in/yaml.v3
- Unit tests (
generator_test.go): Test parsing, generation, edge cases - Integration tests (
integration_test.go):TestRuntimeIntegration: Full pipeline test that builds binary, generates code, runs tests with real databases- Uses testcontainers for MongoDB integration testing
- SQLite for SQL testing (in-memory)
- Test data in
testdata/integration/:enum_test.go: Real database tests run by runtime integrationstatus.go,priority.go: Sample enums for testing
- Parse Go source files to find type definition
- Extract constants prefixed with type name
- Evaluate constant values:
- Handle iota increments
- Evaluate binary expressions (e.g.,
iota + 1,1 << iota) - Support explicit values
- Generate code with:
- String() method
- Parse/Must functions
- Marshal/Unmarshal methods based on flags
- Iterator for Go 1.23+ range-over-func
- Optional GetByID function (requires unique values)
- Enum type must be lowercase - Generator validates and rejects uppercase type names
- Constants must start with type name - e.g., for type
status, constants must bestatusXxx - Unique IDs required for -getter flag - Generator fails if duplicate values exist when getter is requested
- Declaration order preserved - Enums maintain source order, not alphabetical
- Type fidelity - Generated code preserves underlying type (uint8, int32, etc.)
GitHub Actions workflow (.github/workflows/ci.yml):
- Runs on all pushes and PRs
- Tests with Go 1.24
- Runs tests with race detection and coverage
- Runs golangci-lint
- Submits coverage to Coveralls
- Tests examples separately
The integration tests use a unique two-stage approach:
-
TestRuntimeIntegrationinintegration_test.go:- Builds the enum binary from source
- Creates a temporary package with enum definitions
- Runs the built binary to generate enum code
- Creates a temporary
go.modwith test dependencies - Copies test file from
testdata/integration/enum_test.go - Runs the generated tests in isolation
-
Actual database tests in
testdata/integration/enum_test.go:TestGeneratedEnumWithMongoDB: Usesgithub.com/go-pkgz/testutilsto spin up MongoDB 7 containerTestGeneratedEnumWithSQL: Uses in-memory SQLite for SQL testing- Tests real marshal/unmarshal operations, not just code generation
- Test files in testdata are NOT compiled by Go - They're copied and run in temp directory
- MongoDB container via testutils:
mongoContainer := containers.NewMongoTestContainer(ctx, t, 7) defer mongoContainer.Close(ctx) coll := mongoContainer.Collection("test_db")
- Verifies storage format - Confirms enums stored as strings in MongoDB, not empty documents
- Full round-trip testing - Writes enum to database, reads back, verifies correct unmarshaling
# Run full integration test (builds binary, generates code, tests with real databases)
go test ./internal/generator -v -run TestRuntimeIntegration
# Skip integration tests in short mode
go test -short ./...
# Clean test cache before running to ensure fresh MongoDB container
go clean -testcache && go test ./internal/generator -v -run TestRuntimeIntegrationIntegration tests require:
- Docker for MongoDB containers (via testcontainers)
- Network access for go mod tidy in temp package
- Write access to temp directory for generated code
TestMongoDBIntegrationin mainintegration_test.goonly verifies code generation, not actual MongoDB- Real MongoDB testing happens via
TestRuntimeIntegration→TestGeneratedEnumWithMongoDB - Tests verify both success and error paths (NULL handling, invalid values)
- Uses
requirefor critical assertions,assertfor non-critical ones