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 ¶
- Variables
- func Generate(specPath string, cfg Config) (string, error)
- func RenderParameterEnums(spec *ir.Spec) (string, error)
- func RenderPublisher(spec *ir.Spec) (string, error)
- func RenderSubscriber(spec *ir.Spec) (string, error)
- type Config
- type Configuration
- type MessagePackageMapping
- type SchemaPackageMapping
Constants ¶
This section is empty.
Variables ¶
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 ¶
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
RenderParameterEnums emits one `type <T> string` + `const ( ... )` block per channel-parameter enum the spec declared, plus one `type <T> string` + `var <t>Pattern = regexp.MustCompile(...)` + `New<T>` + `Must<T>` block per pattern-validated parameter (v0.4.1+).
Returns "" when the spec has no parameter types — caller skips emission. The block intentionally precedes the publisher / subscriber sections so generated Send / Subscribe signatures can reference the types without forward declarations.
Naming: GoTypeName comes from the lowerer (pascalized parameter name). Enum constants are <TypeName><Pascalized(value)>. Pattern types ship a regex var (private, named `<lowercasedType>Pattern`), a returning constructor (`New<TypeName>` — error on mismatch), and a panicking convenience (`Must<TypeName>` — for compile-time-known constants).
func RenderPublisher ¶
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.
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
// MessagePackages drives cross-file MESSAGE $ref → import behaviour.
// When a message slot is a $ref into another spec file, the lowerer
// looks up the file in this mapping; the message-name segment of
// the $ref becomes the Go type name in the mapped package. Generated
// Subscriber/Publisher signatures reference `<alias>.<TypeName>`
// from the imported package — no payload schema duplication. v0.5+.
MessagePackages []MessagePackageMapping
// 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"`
MessagePackages []MessagePackageMapping `yaml:"message-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 MessagePackageMapping ¶ added in v0.5.0
type MessagePackageMapping struct {
File string `yaml:"file"`
Package string `yaml:"package"`
Alias string `yaml:"alias"`
}
MessagePackageMapping ties a referenced AsyncAPI spec file (consumed via cross-file message $ref) to the Go package the producer's already-generated message types live in. See loader.MessagePackageMapping for the field semantics — this type is the codegen-internal mirror (loader can't import codegen without a cycle; Generate converts at the boundary). v0.5+.
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.
Source Files
¶
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. |