KO
|
EN
gitlite — search
Search
#javascript
#php
#nodejs
#python
#angularjs
#hacktoberfest
#linux
#blog
#gulp
#ruby
#css
#r
go-asyncapi
★ 90
Open GitHub ↗
AsyncAPI spec from Go code
Download README (.md)
Explore Similar Repositories
mat-video
:
:tv: mat-video is an Angular 8/9+ video player using Material!
apollo-mocked-provider
:
Automatically mock GraphQL data with a mocked ApolloProvider
pdn-ddsfiletype-plus
:
A Paint.NET filetype plugin that adds support for some of the DDS formats introduced in DirectX 10 and later.
GmodDotNet
:
Cross-platform .NET Module/Plugin platform for Garry's Mod powered by .NET.
ImageCropper.Forms
:
Xamarin.Forms plugin to crop and rotate photos.
// repository documentation
Was this content helpful?
★ 0
(0 ratings)
Select Rating:
★
★
★
★
★
Submit Feedback
Recent Feedback
×
Download README
Do you want to download the
README.md
file for
go-asyncapi
?
Download (.md)
# AsyncAPI Generator for Go [](https://github.com/swaggest/go-asyncapi/actions?query=branch%3Amaster+workflow%3Atest-unit) [](https://codecov.io/gh/swaggest/go-asyncapi) [](https://godoc.org/github.com/swaggest/go-asyncapi)   This library helps to create [AsyncAPI](https://www.asyncapi.com/) spec from your Go message structures. Supported AsyncAPI versions: * `v2.4.0` * `v2.1.0` * `v2.0.0` * `v1.2.0` ## Example ```go package asyncapi_test import ( "fmt" "os" "time" "github.com/swaggest/go-asyncapi/reflector/asyncapi-2.4.0" "github.com/swaggest/go-asyncapi/spec-2.4.0" ) func main() { type SubItem struct { Key string `json:"key" description:"Item key"` Values []int64 `json:"values" uniqueItems:"true" description:"List of item values"` } type MyMessage struct { Name string `path:"name" description:"Name"` CreatedAt time.Time `json:"createdAt" description:"Creation time"` Items []SubItem `json:"items" description:"List of items"` } type MyAnotherMessage struct { TraceID string `header:"X-Trace-ID" description:"Tracing header" required:"true"` Item SubItem `json:"item" description:"Some item"` } asyncAPI := spec.AsyncAPI{} asyncAPI.Info.Version = "1.2.3" asyncAPI.Info.Title = "My Lovely Messaging API" asyncAPI.AddServer("live", spec.Server{ URL: "api.{country}.lovely.com:5672", Description: "Production instance.", ProtocolVersion: "0.9.1", Protocol: "amqp", Variables: map[string]spec.ServerVariable{ "country": { Enum: []string{"RU", "US", "DE", "FR"}, Default: "US", Description: "Country code.", }, }, }) reflector := asyncapi.Reflector{} reflector.Schema = &asyncAPI mustNotFail := func(err error) { if err != nil { panic(err.Error()) } } mustNotFail(reflector.AddChannel(asyncapi.ChannelInfo{ Name: "one.{name}.two", BaseChannelItem: &spec.ChannelItem{ Bindings: &spec.ChannelBindingsObject{ Amqp: &spec.AmqpChannel{ Is: spec.AmqpChannelIsRoutingKey, Exchange: &spec.AmqpChannelExchange{ Name: "some-exchange", }, }, }, }, Publish: &asyncapi.MessageSample{ MessageEntity: spec.MessageEntity{ Description: "This is a sample schema.", Summary: "Sample publisher", }, MessageSample: new(MyMessage), }, })) mustNotFail(reflector.AddChannel(asyncapi.ChannelInfo{ Name: "another.one", Subscribe: &asyncapi.MessageSample{ MessageEntity: spec.MessageEntity{ Description: "This is another sample schema.", Summary: "Sample consumer", }, MessageSample: new(MyAnotherMessage), }, })) yaml, err := reflector.Schema.MarshalYAML() mustNotFail(err) fmt.Println(string(yaml)) mustNotFail(os.WriteFile("sample.yaml", yaml, 0o600)) // output: // asyncapi: 2.4.0 // info: // title: My Lovely Messaging API // version: 1.2.3 // servers: // live: // url: api.{country}.lovely.com:5672 // description: Production instance. // protocol: amqp // protocolVersion: 0.9.1 // variables: // country: // enum: // - RU // - US // - DE // - FR // default: US // description: Country code. // channels: // another.one: // subscribe: // message: // $ref: '#/components/messages/Asyncapi240TestMyAnotherMessage' // one.{name}.two: // parameters: // name: // schema: // description: Name // type: string // publish: // message: // $ref: '#/components/messages/Asyncapi240TestMyMessage' // bindings: // amqp: // bindingVersion: 0.2.0 // is: routingKey // exchange: // name: some-exchange // components: // schemas: // Asyncapi240TestMyAnotherMessage: // properties: // item: // $ref: '#/components/schemas/Asyncapi240TestSubItem' // description: Some item // type: object // Asyncapi240TestMyMessage: // properties: // createdAt: // description: Creation time // format: date-time // type: string // items: // description: List of items // items: // $ref: '#/components/schemas/Asyncapi240TestSubItem' // type: // - array // - "null" // type: object // Asyncapi240TestSubItem: // properties: // key: // description: Item key // type: string // values: // description: List of item values // items: // type: integer // type: // - array // - "null" // uniqueItems: true // type: object // messages: // Asyncapi240TestMyAnotherMessage: // headers: // properties: // X-Trace-ID: // description: Tracing header // type: string // required: // - X-Trace-ID // type: object // payload: // $ref: '#/components/schemas/Asyncapi240TestMyAnotherMessage' // summary: Sample consumer // description: This is another sample schema. // Asyncapi240TestMyMessage: // payload: // $ref: '#/components/schemas/Asyncapi240TestMyMessage' // summary: Sample publisher // description: This is a sample schema. } ```