Package asyncapi provides a suite of tools for working with AsyncAPI specifications, making it easier to parse, format, manipulate, and generate code from these specs.
It is the counterpart of MarkRosemaker/openapi for event-driven APIs and follows the same design, so both packages can be used side by side.
The primary goals of this package are:
- Parsing AsyncAPI specifications into a structured format.
- Validating the specifications strictly against the rules of the specification.
- Formatting the parsed specifications, including sorting maps and merging duplicate content.
- Adding information programmatically to the specifications.
- Marshalling the modified specifications back into their original format.
- Utilizing the parsed specification for code generation.
- Comprehensive parsing of AsyncAPI 3.1.0 specifications, in JSON as well as in YAML.
- Strict validation against the rules of the specification: required fields, enumerations, key and address patterns, absolute URLs, runtime expressions, and the rules that span several objects, e.g. that the messages of an operation "MUST contain a subset of the messages defined in the channel referenced in this operation". Every error names the exact location of the problem, e.g.
channels["userSignedup"].messages["userSignedUp"].contentType: mime: expected slash after first token. - Marshalling back to JSON and to YAML, to a file, to a writer or to a byte slice.
- Reference resolution of every referencable object, including references that point to other references, e.g. an operation that refers to a message of a channel which in turn refers to a message of the components object.
- Order preservation: maps keep the order in which their keys were defined, so writing a specification back doesn't reshuffle it.
- Multi format schemas: schemas in other formats (Avro, Protobuf, RAML, ...) are kept as they are, AsyncAPI schemas are parsed, including boolean schemas and multiple types.
- Bindings of all 20 protocols are preserved as they were given, so nothing is lost when a specification is written back.
- Documented in line with the specification: every object, field and rule quotes the official documentation and links to the section it comes from.
package main
import (
"fmt"
"github.com/MarkRosemaker/asyncapi"
)
func main() {
doc, err := asyncapi.LoadFromFile("path/to/asyncapi.json") // or asyncapi.yaml
if err != nil {
fmt.Println("Error parsing spec:", err)
return
}
if err := doc.Validate(); err != nil {
fmt.Println("Error validating spec:", err)
return
}
// sort the keys of the servers, channels, operations and components in alphabetical order
doc.SortMaps()
// write an improved version of your spec, as JSON or as YAML
if err := doc.WriteToFile("path/to/asyncapi.json"); err != nil {
fmt.Println("Error writing to file:", err)
return
}
}- Go Reference: API documentation.
Contributions are welcome — please open an issue or a pull request on GitHub.
This project is licensed under the Apache 2.0 License.