codegen

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 20 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package codegen orchestrates AsyncAPI → Go generation.

Pipeline:

  • Load the AsyncAPI spec.
  • Resolve every message payload's $ref to a JSON Schema file.
  • Invoke go-jsonschema in-process to emit payload Go types (cross-tree $refs become Go imports per Config.SchemaPackages).
  • Lower the spec to IR; render typed publishers per `action: send` operation.
  • Combine, inject imports, gofmt, write one Go file.

Wire-compat (accepting legacy PascalCase JSON alongside the canonical camelCase) is intentionally out of scope: aapi-codegen does not emit UnmarshalJSON shims. Consumers that need it hand-write a small `compat.go` sibling to the generated file and retire it once the legacy publishers are gone — the kind of transitional debt that doesn't belong in generated code.

Index

Constants

This section is empty.

Variables

View Source
var DefaultInitialisms = []string{"ID", "URL"}

DefaultInitialisms is the initialism list applied when Config.Initialisms is empty. Mirrors oapi-codegen's pattern: defaults are baked in; user-supplied entries are additive.

Functions

func Generate

func Generate(specPath string, cfg Config) (string, error)

Generate runs the pipeline end-to-end and writes one Go file at cfg.Output. Returns the absolute output path on success.

func RenderParameterEnums added in v0.4.0

func RenderParameterEnums(spec *ir.Spec) (string, error)

RenderParameterEnums emits one `type <T> string` + `const ( ... )` block per channel-parameter enum the spec declared. Returns "" when the spec has none — caller skips emission. The block intentionally precedes the publisher / subscriber sections so generated Send / Subscribe signatures can reference the enum types without forward declarations (Go allows it either way, but ordering helps readers).

Naming: GoTypeName comes from the lowerer (pascalized parameter name); constants are <TypeName><Pascalized(value)>. Values that contain runes invalid in Go identifiers are sanitised to '_'.

func RenderPublisher

func RenderPublisher(spec *ir.Spec) (string, error)

RenderPublisher emits the publisher Go source for every `action: send` operation in spec. Returns "" when the spec has no send operations — caller should skip emission entirely and not pull in context/json/fmt imports.

func RenderSubscriber added in v0.2.0

func RenderSubscriber(spec *ir.Spec) (string, error)

RenderSubscriber emits the subscriber Go source for every `action: receive` operation. Returns "" when the spec has none — caller should skip emission and not pull in context/json/fmt/errors.

Types

type Config

type Config struct {
	Package string
	Output  string
	// Initialisms supplements DefaultInitialisms when configuring
	// go-jsonschema's --capitalization rules. Empty means defaults only.
	Initialisms []string
	// SchemaPackages drives cross-tree $ref → import behaviour. For each
	// entry, go-jsonschema emits `import alias "package"` at the use
	// site instead of inlining the schema's types.
	SchemaPackages []SchemaPackageMapping
	// OmitValidation suppresses go-jsonschema's generated UnmarshalJSON
	// methods (required-field checks, strict additionalProperties,
	// format/enum/numeric-bound validators, etc.). False (the default)
	// means validation is included; true is the opt-out for consumers
	// that hand-write their own UnmarshalJSON, e.g. for wire-compat
	// (camelCase ↔ legacy PascalCase): two UnmarshalJSON methods on the
	// same type would be a compile error, so wire-compat consumers must
	// opt out here to define their own.
	OmitValidation bool
	// OmitPublishers / OmitSubscribers skip the corresponding generated
	// section even when the spec has matching `action:` operations.
	// OR-merged with the spec extension equivalents. v0.2+.
	OmitPublishers  bool
	OmitSubscribers bool
}

Config is the internal codegen surface. The user-facing YAML Configuration translates into this via Configuration.ToCodegenConfig.

type Configuration

type Configuration struct {
	Package        string                 `yaml:"package"`
	Output         string                 `yaml:"output"`
	SchemaPackages []SchemaPackageMapping `yaml:"schema-packages"`
	// Capitalization mirrors go-jsonschema's --capitalization flag list.
	// When empty, DefaultInitialisms is applied.
	Capitalization []string `yaml:"capitalization"`
	// OmitValidation opts out of go-jsonschema's generated UnmarshalJSON
	// methods. Default false (validation included). Set true only when
	// consumers will hand-write their own UnmarshalJSON (e.g. for the
	// camelCase ↔ legacy PascalCase wire-compat pattern); a generated
	// UnmarshalJSON and a hand-written one on the same type is a
	// compile error.
	OmitValidation bool `yaml:"omit-validation"`
}

Configuration is the YAML-driven config aapi-codegen reads via the CLI's -config flag. Mirrors oapi-codegen's pattern: top-level package + output, plus an aapi-codegen-specific schema-packages block that absorbs the cross-tree $ref mapping today's per-schema-dir generate.sh files express as repeated --schema-package CLI args. See plan §6.

Fields not set in YAML default to zero; CLI flags supersede YAML values when both are present (the precedence is established by MergeFlags).

func LoadConfiguration

func LoadConfiguration(path string) (*Configuration, error)

LoadConfiguration parses a YAML config file. Returns an error rather than silently filling in defaults — a typo in the YAML key should surface, not be papered over.

func (*Configuration) MergeFlags

func (c *Configuration) MergeFlags(pkg, output string, capitalization []string)

MergeFlags overlays CLI-flag values onto a config loaded from YAML. A non-empty flag value wins; this lets the CLI run a config file as the base while still supporting one-off overrides without editing YAML.

func (*Configuration) ToCodegenConfig

func (c *Configuration) ToCodegenConfig() Config

ToCodegenConfig translates a Configuration into the codegen.Config the internal pipeline consumes. Kept as a translation step so the YAML schema can evolve independently of the internal struct's fields.

type SchemaPackageMapping

type SchemaPackageMapping struct {
	ID      string `yaml:"id"`
	Package string `yaml:"package"`
	Alias   string `yaml:"alias"`
}

SchemaPackageMapping ties a JSON-Schema $id to the Go package its types live in. Aliasing is mandatory because two canonical schemas commonly share a last path segment (`v1`/`v1`) and Go's import-alias derivation would otherwise collide.

Directories

Path Synopsis
Package ir is the binding-agnostic intermediate representation that sits between the AsyncAPI loader and the Go-source templates.
Package ir is the binding-agnostic intermediate representation that sits between the AsyncAPI loader and the Go-source templates.
Package loader parses an AsyncAPI 3.x YAML document into a typed in-memory representation.
Package loader parses an AsyncAPI 3.x YAML document into a typed in-memory representation.
Package lower turns a parsed AsyncAPI document (loader.Document) into the binding-agnostic IR (ir.Spec) the publisher template iterates over.
Package lower turns a parsed AsyncAPI document (loader.Document) into the binding-agnostic IR (ir.Spec) the publisher template iterates over.
Package schema models the slice of JSON Schema 2020-12 that the compat-shim emitter cares about.
Package schema models the slice of JSON Schema 2020-12 that the compat-shim emitter cares about.

Jump to

Keyboard shortcuts

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