diff --git a/pkg/asyncapi/v3/channel.go b/pkg/asyncapi/v3/channel.go index beddfc0..7e27d0d 100644 --- a/pkg/asyncapi/v3/channel.go +++ b/pkg/asyncapi/v3/channel.go @@ -27,7 +27,7 @@ type Channel struct { Title string `json:"title"` Summary string `json:"summary"` Description string `json:"description"` - Servers []*Server `json:"servers"` + Servers []*Server `json:"servers"` // Reference only Parameters map[string]*Parameter `json:"parameters"` Tags []*Tag `json:"tags"` ExternalDocs *ExternalDocumentation `json:"externalDocs"` @@ -58,11 +58,6 @@ func (ch *Channel) generateMetadata(parentName, name string) error { } } - // Generate servers metadata - for i, srv := range ch.Servers { - srv.generateMetadata(ch.Name, "", &i) - } - // Generate parameters metadata for name, param := range ch.Parameters { param.generateMetadata(ch.Name, name) diff --git a/pkg/asyncapi/v3/components.go b/pkg/asyncapi/v3/components.go index 4149d66..ddee4ed 100644 --- a/pkg/asyncapi/v3/components.go +++ b/pkg/asyncapi/v3/components.go @@ -103,7 +103,7 @@ func (c *Components) setDependencies(spec Specification) error { func (c *Components) generateMetadataFromMaps() error { // Generate metadata for servers for name, entity := range c.Servers { - entity.generateMetadata("", name, nil) + entity.generateMetadata("", name) } // Generate metadata for channels diff --git a/pkg/asyncapi/v3/server.go b/pkg/asyncapi/v3/server.go index 38c59e6..c2f53b0 100644 --- a/pkg/asyncapi/v3/server.go +++ b/pkg/asyncapi/v3/server.go @@ -27,14 +27,14 @@ type Server struct { } // generateMetadata generates metadata for the Server. -func (srv *Server) generateMetadata(parentName, name string, number *int) { +func (srv *Server) generateMetadata(parentName, name string) { // Prevent modification if nil if srv == nil { return } // Set name - srv.Name = generateFullName(parentName, name, "Server", number) + srv.Name = generateFullName(parentName, name, "Server", nil) // Generate variables metadata for n, s := range srv.Variables { diff --git a/pkg/asyncapi/v3/specification.go b/pkg/asyncapi/v3/specification.go index d46e2ec..62d878d 100644 --- a/pkg/asyncapi/v3/specification.go +++ b/pkg/asyncapi/v3/specification.go @@ -28,7 +28,7 @@ type Specification struct { Version string `json:"asyncapi"` ID string `json:"id"` Info Info `json:"info"` - Servers []*Server `json:"servers"` + Servers map[string]*Server `json:"servers"` DefaultContentType string `json:"defaultContentType"` Channels map[string]*Channel `json:"channels"` Operations map[string]*Operation `json:"operations"` @@ -105,8 +105,8 @@ func (s *Specification) generateMetadata() error { } // Generate servers metadata - for i, srv := range s.Servers { - srv.generateMetadata("", "Server", &i) + for name, srv := range s.Servers { + srv.generateMetadata("", name) } // Generate metadata for channels diff --git a/test/v3/issues/209/asyncapi.gen.go b/test/v3/issues/209/asyncapi.gen.go new file mode 100644 index 0000000..8f72d87 --- /dev/null +++ b/test/v3/issues/209/asyncapi.gen.go @@ -0,0 +1,68 @@ +// Package "issue209" provides primitives to interact with the AsyncAPI specification. +// +// Code generated by github.com/lerenn/asyncapi-codegen version (devel) DO NOT EDIT. +package issue209 + +import ( + "fmt" + + "github.com/lerenn/asyncapi-codegen/pkg/extensions" +) + +// AsyncAPIVersion is the version of the used AsyncAPI document +const AsyncAPIVersion = "0.1.0" + +// controller is the controller that will be used to communicate with the broker +// It will be used internally by AppController and UserController +type controller struct { + // broker is the broker controller that will be used to communicate + broker extensions.BrokerController + // subscriptions is a map of all subscriptions + subscriptions map[string]extensions.BrokerChannelSubscription + // logger is the logger that will be used² to log operations on controller + logger extensions.Logger + // middlewares are the middlewares that will be executed when sending or + // receiving messages + middlewares []extensions.Middleware + // handler to handle errors from consumers and middlewares + errorHandler extensions.ErrorHandler +} + +// ControllerOption is the type of the options that can be passed +// when creating a new Controller +type ControllerOption func(controller *controller) + +// WithLogger attaches a logger to the controller +func WithLogger(logger extensions.Logger) ControllerOption { + return func(controller *controller) { + controller.logger = logger + } +} + +// WithMiddlewares attaches middlewares that will be executed when sending or receiving messages +func WithMiddlewares(middlewares ...extensions.Middleware) ControllerOption { + return func(controller *controller) { + controller.middlewares = middlewares + } +} + +// WithErrorHandler attaches a errorhandler to handle errors from subscriber functions +func WithErrorHandler(handler extensions.ErrorHandler) ControllerOption { + return func(controller *controller) { + controller.errorHandler = handler + } +} + +type MessageWithCorrelationID interface { + CorrelationID() string + SetCorrelationID(id string) +} + +type Error struct { + Channel string + Err error +} + +func (e *Error) Error() string { + return fmt.Sprintf("channel %q: err %v", e.Channel, e.Err) +} diff --git a/test/v3/issues/209/asyncapi.yaml b/test/v3/issues/209/asyncapi.yaml new file mode 100644 index 0000000..b9746bf --- /dev/null +++ b/test/v3/issues/209/asyncapi.yaml @@ -0,0 +1,15 @@ +asyncapi: 3.0.0 +info: + title: Hello world application + version: '0.1.0' +servers: + local: + host: localhost:9092 + protocol: kafka + protocolVersion: '3.5' + description: Local Kafka broker + test: + host: test.k8s.cluster.local:9092 + protocol: kafka + protocolVersion: '3.5' + description: Test environment K8S Kafka cluster \ No newline at end of file diff --git a/test/v3/issues/209/generate.go b/test/v3/issues/209/generate.go new file mode 100644 index 0000000..a32cb21 --- /dev/null +++ b/test/v3/issues/209/generate.go @@ -0,0 +1,5 @@ +//go:generate go run ../../../../cmd/asyncapi-codegen -g types -p issue209 -i ./asyncapi.yaml -o ./asyncapi.gen.go + +package issue209 + +// This is just to test that the generation is correct