Documentation
¶
Overview ¶
Package loader parses an AsyncAPI 3.x YAML document into a typed in-memory representation. M1 only models the slice of the spec the tracer needs: enough to find each message's payload $ref and resolve it to a filesystem path. Channels, parameters, bindings, and operations are modelled as opaque-enough placeholders so they round-trip through YAML without losing data; M2/M3 widen the typed surface as the lowerer (pkg/codegen/lower) starts using more fields.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUnsupportedRef = errors.New("unsupported $ref shape")
ErrUnsupportedRef is returned when a $ref points outside the slice of AsyncAPI we currently model. M1 only resolves payload $refs that point to JSON Schema files on disk; AsyncAPI-internal $refs (e.g. `#/components/messages/X`) are deferred to M2.
Functions ¶
This section is empty.
Types ¶
type Channel ¶
type Channel struct {
Address string `yaml:"address"`
Title string `yaml:"title,omitempty"`
Description string `yaml:"description,omitempty"`
Parameters map[string]*Parameter `yaml:"parameters,omitempty"`
Bindings map[string]any `yaml:"bindings,omitempty"`
Messages map[string]*Message `yaml:"messages,omitempty"`
}
type Components ¶
type Components struct {
Schemas map[string]any `yaml:"schemas,omitempty"`
Messages map[string]*Message `yaml:"messages,omitempty"`
}
Components is the AsyncAPI 3.x `components` object. Models the subset aapi-codegen consumes: shared JSON Schema types (`components.schemas`) and shared message wrappers (`components.messages`). Other components (parameters, operationBindings, ...) get added when fixtures need them.
type Document ¶
type Document struct {
AsyncAPI string `yaml:"asyncapi"`
Info Info `yaml:"info"`
DefaultContentType string `yaml:"defaultContentType,omitempty"`
Channels map[string]*Channel `yaml:"channels,omitempty"`
Operations map[string]*Operation `yaml:"operations,omitempty"`
Components *Components `yaml:"components,omitempty"`
// XAAPICodegen carries aapi-codegen-specific configuration declared
// inside the spec via the `x-aapi-codegen` extension. The extension
// is the canonical place to put cross-tree $ref → import mappings
// (plan §6 / Q1) — colocates spec content with the tooling
// directives that interpret it. CLI / config-YAML still work as
// overrides for migration, but the spec is the source of truth.
XAAPICodegen *XExtension `yaml:"x-aapi-codegen,omitempty"`
// SourcePath is the absolute filesystem path the document was loaded
// from. Set by Load(); used by RefResolver to resolve relative $refs.
SourcePath string `yaml:"-"`
}
Document is the root of an AsyncAPI 3.x spec.
func Load ¶
Load parses the AsyncAPI document at specPath. The returned Document has SourcePath populated so payload $refs can be resolved against it.
func (*Document) Materialize ¶
Materialize prepares a document with inline payloads and/or `components.schemas` for go-jsonschema consumption by writing synthetic .schema.json files into tmpDir. After this call, every message that previously had an inline payload has a Payload.Ref pointing at one of these synthetic files — so the rest of the pipeline (PayloadSchemaPaths, the lowerer's title-reading, go-jsonschema's DoFile) can process them without knowing whether the original spec used inline or external payloads.
Layout (mirrors the AsyncAPI spec's own hierarchy so collisions between an inline channel message and a components.messages entry sharing a key are structural, not name-mangled):
tmpDir/
components/
<SchemaName>.schema.json # one per components.schemas entry
messages/
<MsgName>.schema.json # one per components.messages payload
messages/
<MsgKey>.schema.json # one per channel-level inline message
`#/components/schemas/X` refs in any payload are rewritten to relative file paths so go-jsonschema's filesystem-based resolver picks them up. The relative path depends on where the referring file lives — components/messages/Foo.schema.json refs `../X.schema.json`, messages/Foo.schema.json refs `../components/X.schema.json`, components/Foo.schema.json refs sibling `X.schema.json`.
Caller is responsible for cleaning up tmpDir (typically via os.RemoveAll in a defer next to os.MkdirTemp).
func (*Document) PayloadSchemaPaths ¶
PayloadSchemaPaths walks every channel.messages.*.payload.$ref in doc, resolves each ref against the spec file's directory, dedupes, and returns absolute filesystem paths. Refs that contain a fragment (e.g. `foo.json#/X`) have the fragment stripped — we hand whole files to go-jsonschema.
Internal AsyncAPI refs (`#/components/...`) are not yet supported and surface as ErrUnsupportedRef.
func (*Document) ResolveMessageRefs ¶
ResolveMessageRefs walks every channel-level message slot and follows any `$ref` pointing into `components.messages`, inlining the referenced component into the slot. After this call, every channel.messages[X] is a fully-populated Message struct; CanonicalName is set on slots that were resolved from a component, so the materializer can dedupe shared messages (multiple channels referencing the same component produce one synthetic schema file and therefore one Go type).
Idempotent: calling twice is a no-op because Ref is cleared after inlining and a slot with empty Ref is skipped.
Operation-level message refs (`operations.X.messages[].$ref`) support both forms after this resolver runs:
- channel-scoped: `#/channels/<chan>/messages/<key>` (passes through)
- component-scoped: `#/components/messages/<Name>`
Component-scoped refs are rewritten to channel-scoped form by finding the unique channel-level slot whose CanonicalName matches. The operation's own `channel.$ref` constrains which channel to look in, so a component referenced from multiple channels resolves unambiguously per operation.
type Message ¶
type Message struct {
// Ref is set when this message slot is a `$ref` to
// `#/components/messages/X` rather than an inline declaration. The
// resolver (see ResolveMessageRefs) follows the ref, copies the
// component's fields into this struct, and sets CanonicalName.
// Spec authors can't mix Ref with inline fields — the resolver
// rejects that.
Ref string `yaml:"$ref,omitempty"`
Name string `yaml:"name,omitempty"`
Title string `yaml:"title,omitempty"`
Summary string `yaml:"summary,omitempty"`
ContentType string `yaml:"contentType,omitempty"`
Payload Payload `yaml:"payload"`
// CanonicalName is set by the resolver to the
// `components.messages.<Name>` key when this message was resolved
// from a component ref. The materializer uses it as the synthetic-
// file basename so multiple channel-level references to the same
// component dedupe to one synthetic schema (one Go type). Empty
// for inline messages — the materializer falls back to the
// channel-level message key in that case.
CanonicalName string `yaml:"-"`
}
type Operation ¶
type Operation struct {
Action string `yaml:"action"` // "send" | "receive"
Channel OperationChannelRef `yaml:"channel"`
Title string `yaml:"title,omitempty"`
Summary string `yaml:"summary,omitempty"`
Messages []OperationMessageRef `yaml:"messages,omitempty"`
}
type OperationChannelRef ¶
type OperationChannelRef struct {
Ref string `yaml:"$ref"`
}
OperationChannelRef is the typed shape of `operations.X.channel.$ref`.
type OperationMessageRef ¶
type OperationMessageRef struct {
Ref string `yaml:"$ref"`
}
OperationMessageRef is the typed shape of items in `operations.X.messages`.
type Payload ¶
type Payload struct {
Ref string `yaml:"$ref,omitempty"`
// Inline schema fields (type/properties/etc.) get parsed into this map
// for now. Promoted to typed fields when M2 needs them.
Inline map[string]any `yaml:",inline"`
}
Payload is either an inline schema or a $ref to an external one. M1 only handles the $ref form (every reference spec uses it). When Ref is empty the lowerer can fall back to interpreting the rest of the YAML as an inline schema; M1 doesn't need that path.
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. Declared either in the spec's x-aapi-codegen extension (canonical) or in the optional aapi-codegen.config.yaml (legacy / migration override). Aliasing is mandatory because two canonical schemas commonly share a last path segment (`v1`/`v1`) and Go's import-alias derivation would otherwise collide.
This type duplicates the codegen.SchemaPackageMapping shape on purpose — the loader can't import codegen without a dependency cycle. Generate() converts loader.SchemaPackageMapping → codegen.SchemaPackageMapping at the boundary.
type XExtension ¶
type XExtension struct {
SchemaPackages []SchemaPackageMapping `yaml:"schema-packages,omitempty"`
// OmitValidation opts out of generated UnmarshalJSON methods for
// this spec. Useful when consumers hand-write their own
// UnmarshalJSON (e.g. wire-compat shims for legacy PascalCase
// publishers). Either source — spec extension or config — can opt
// out; the merge is OR semantics (Generate's mergeOmitValidation).
OmitValidation bool `yaml:"omit-validation,omitempty"`
// OmitPublishers suppresses the Publisher struct + Send<Msg> methods
// even when the spec has `action: send` operations. Use when a spec
// is consumer-facing only (the producer lives elsewhere; this spec is
// a reference copy). v0.2+.
OmitPublishers bool `yaml:"omit-publishers,omitempty"`
// OmitSubscribers suppresses the Subscriber struct + Subscribe<Msg>
// methods + <Msg>Handler interfaces even when the spec has
// `action: receive` operations. Symmetric to OmitPublishers. v0.2+.
OmitSubscribers bool `yaml:"omit-subscribers,omitempty"`
}
XExtension is the typed shape of the spec-level `x-aapi-codegen` extension. Add fields as new declarations move into the spec.