Documentation
¶
Overview ¶
Package asyncapi3 emits AsyncAPI 3.0.0 documents from Go code.
Declare servers, channels, and operations as Go values, reflect payload schemas from the structs that cross the wire with SchemaFromType, and marshal the Document to JSON or YAML.
Marshalled output is deterministic — maps marshal key-sorted and struct field order is fixed — so regenerating an unchanged document is byte-identical. That property is what makes a committed document gateable in CI; see Check.
The document model is a curated subset of the specification. Spec objects and protocol bindings it does not model yet attach as raw JSON through the Bindings fields, so an unmodelled binding never blocks you.
Index ¶
- Constants
- func Check(generated []byte, path string) error
- func MergeSchemas(dst, src map[string]json.RawMessage) error
- func SchemaFromType(v any, opts ...ReflectOption) (json.RawMessage, map[string]json.RawMessage, error)
- func ValidateBytes(document []byte) error
- type Channel
- type Components
- type Contact
- type Document
- type ExternalDocs
- type Info
- type License
- type Message
- type Operation
- type OperationReply
- type Parameter
- type RefObj
- type ReflectOption
- type Server
- type Tag
Constants ¶
const ( ActionSend = "send" ActionReceive = "receive" )
Operation actions, as defined by the specification: an application either sends a message to a channel or receives one from it. Both are expressed from the point of view of the application described by the document.
const Version = "3.0.0"
Version is the AsyncAPI specification version this package emits.
Variables ¶
This section is empty.
Functions ¶
func Check ¶
Check compares freshly generated document bytes against the committed artifact at path, so contract-drift gating runs as one tested code path locally and in CI instead of shell logic in a workflow.
func MergeSchemas ¶
func MergeSchemas(dst, src map[string]json.RawMessage) error
MergeSchemas folds src into dst, failing on same-name definitions with different content — a silent overwrite would corrupt one of the schemas.
func SchemaFromType ¶
func SchemaFromType(v any, opts ...ReflectOption) (json.RawMessage, map[string]json.RawMessage, error)
SchemaFromType reflects v into a JSON Schema, returning the payload schema for the value itself — a "$ref" into "#/components/schemas/" — and the definitions it references, ready to assign to Components.Schemas.
Property names come from json struct tags. Schema keywords are read from the tags jsonschema-go understands, including required:"true", so the wire contract is declared on the struct rather than restated here.
func ValidateBytes ¶
ValidateBytes checks an already-marshalled AsyncAPI document against the embedded official AsyncAPI 3.0.0 meta-schema.
Types ¶
type Channel ¶
type Channel struct {
Address string `json:"address,omitempty"`
Title string `json:"title,omitempty"`
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
Messages map[string]Message `json:"messages,omitempty"`
Parameters map[string]Parameter `json:"parameters,omitempty"`
Servers []RefObj `json:"servers,omitempty"`
Tags []Tag `json:"tags,omitempty"`
Bindings json.RawMessage `json:"bindings,omitempty"`
}
Channel is an addressable path messages flow through. An Address may embed parameters in braces — "/ports/{unlocode}" — each of which must have a matching entry in Parameters.
type Components ¶
type Components struct {
Schemas map[string]json.RawMessage `json:"schemas,omitempty"`
Messages map[string]Message `json:"messages,omitempty"`
Parameters map[string]Parameter `json:"parameters,omitempty"`
Servers map[string]Server `json:"servers,omitempty"`
Channels map[string]Channel `json:"channels,omitempty"`
}
Components holds reusable definitions referenced from elsewhere in the document. Schemas is where SchemaFromType definitions belong; entries are referenced as "#/components/schemas/Name".
type Contact ¶
type Contact struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
Email string `json:"email,omitempty"`
}
Contact identifies who to reach about the API.
type Document ¶
type Document struct {
AsyncAPI string `json:"asyncapi"`
ID string `json:"id,omitempty"`
Info Info `json:"info"`
DefaultContentType string `json:"defaultContentType,omitempty"`
Servers map[string]Server `json:"servers,omitempty"`
Channels map[string]Channel `json:"channels,omitempty"`
Operations map[string]Operation `json:"operations,omitempty"`
Components *Components `json:"components,omitempty"`
}
Document is the root of an AsyncAPI document. Build one with New so the specification version is set correctly.
type ExternalDocs ¶
type ExternalDocs struct {
Description string `json:"description,omitempty"`
URL string `json:"url"`
}
ExternalDocs points at documentation held outside the document.
type Info ¶
type Info struct {
Title string `json:"title"`
Version string `json:"version"`
Description string `json:"description,omitempty"`
TermsOfService string `json:"termsOfService,omitempty"`
Contact *Contact `json:"contact,omitempty"`
License *License `json:"license,omitempty"`
Tags []Tag `json:"tags,omitempty"`
ExternalDocs *ExternalDocs `json:"externalDocs,omitempty"`
}
Info carries metadata about the API. Title and Version are required by the specification.
type Message ¶
type Message struct {
Ref string `json:"$ref,omitempty"`
Name string `json:"name,omitempty"`
Title string `json:"title,omitempty"`
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
ContentType string `json:"contentType,omitempty"`
Headers json.RawMessage `json:"headers,omitempty"`
Payload json.RawMessage `json:"payload,omitempty"`
Examples json.RawMessage `json:"examples,omitempty"`
Tags []Tag `json:"tags,omitempty"`
Bindings json.RawMessage `json:"bindings,omitempty"`
}
Message describes one kind of message on a channel. Set Ref alone to reference a message defined under Components; otherwise Payload carries the schema, typically produced by SchemaFromType.
type Operation ¶
type Operation struct {
Action string `json:"action,omitempty"`
Channel RefObj `json:"channel,omitzero"`
Title string `json:"title,omitempty"`
Summary string `json:"summary,omitempty"`
Description string `json:"description,omitempty"`
Messages []RefObj `json:"messages,omitempty"`
Reply *OperationReply `json:"reply,omitempty"`
Tags []Tag `json:"tags,omitempty"`
Bindings json.RawMessage `json:"bindings,omitempty"`
}
Operation is an action the application performs on a channel: ActionSend or ActionReceive. Set Reply to describe a request/reply exchange.
type OperationReply ¶
type OperationReply struct {
Channel RefObj `json:"channel,omitzero"`
Messages []RefObj `json:"messages,omitempty"`
}
OperationReply describes the response half of a request/reply exchange, and the channel it arrives on — which may be the request channel itself.
type Parameter ¶
type Parameter struct {
Description string `json:"description,omitempty"`
Enum []string `json:"enum,omitempty"`
Default string `json:"default,omitempty"`
Examples []string `json:"examples,omitempty"`
Location string `json:"location,omitempty"`
}
Parameter describes one substitution in a channel address.
type RefObj ¶
type RefObj struct {
Ref string `json:"$ref,omitempty"`
}
RefObj is a JSON Reference to another part of the document. Build one with Ref.
type ReflectOption ¶
type ReflectOption func(*reflectConfig)
ReflectOption configures SchemaFromType.
func RequireAll ¶
func RequireAll() ReflectOption
RequireAll marks every property of every reflected definition as required. Correct only for payloads whose Go structs never use omitempty — Go then marshals every field on every message.
type Server ¶
type Server struct {
Host string `json:"host"`
Protocol string `json:"protocol"`
Pathname string `json:"pathname,omitempty"`
Description string `json:"description,omitempty"`
Title string `json:"title,omitempty"`
Summary string `json:"summary,omitempty"`
Tags []Tag `json:"tags,omitempty"`
Bindings json.RawMessage `json:"bindings,omitempty"`
}
Server describes where the API is reachable. Host and Protocol are required by the specification; Bindings carries protocol-specific configuration as raw JSON.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
requestreply
command
Command requestreply emits an AsyncAPI 3.0 document for an in-band authentication handshake: the server asks for a token, the client answers on the same channel.
|
Command requestreply emits an AsyncAPI 3.0 document for an in-band authentication handshake: the server asks for a token, the client answers on the same channel. |
|
websocket
command
Command websocket emits an AsyncAPI 3.0 document for a server-to-client WebSocket broadcast, with the payload schema reflected from the Go struct that is actually written to the socket.
|
Command websocket emits an AsyncAPI 3.0 document for a server-to-client WebSocket broadcast, with the payload schema reflected from the Go struct that is actually written to the socket. |