Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -916,8 +916,33 @@ operations:

generates `SendAsSendEventsOperationForUserCreated` and
`...ForUserDeleted`, each sending its own message type. Operations with a single
message are unchanged. Reception of multiple message types on a single channel
is not covered yet (the subscribe side still handles the first message only).
message are unchanged.

On the **receive** side, a subscribe function is likewise generated per message,
named after the operation and the message, with one callback per message type:

```yaml
operations:
receiveEvents:
action: receive
channel: { $ref: '#/channels/events' }
```

generates `SubscribeToReceiveEventsOperationForUserCreated(ctx, fn)` and
`...ForUserDeleted(ctx, fn)` (plus the matching `UnsubscribeFrom...For...`
functions and per-message methods on the subscriber interface). A single broker
subscription is opened per channel and multiplexed to the registered handlers.

Each received message is discriminated with a **try-each-until-valid** strategy:
it is unmarshalled and validated against every expected message schema (using
[`go-playground/validator`](https://github.com/go-playground/validator), so this
dependency must be available in your module) and dispatched to the first one
that matches. This is heuristic and can be ambiguous when several message
schemas overlap — it works best when the messages have distinguishing required
or `enum`/`const` fields. A message that matches none of the expected types is
nacked and surfaced to the [error handler](#errorhandler) as
`extensions.ErrNoMatchingMessage`. Operations with a single message are
unchanged.

### Channel parameters from message location (AsyncAPI v3)

Expand Down
13 changes: 13 additions & 0 deletions pkg/codegen/generators/v3/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ type ActionOperations struct {
Send map[string]*asyncapi.Operation
}

// HasMultiMessageReceive reports whether at least one receive operation carries
// more than one message, and therefore needs the multiplexed dispatcher
// machinery (issue #333).
func (ao ActionOperations) HasMultiMessageReceive() bool {
for _, op := range ao.Receive {
o := op.Follow()
if len(o.GetMessages()) > 1 && o.Reply == nil && o.ReplyOf == nil {
return true
}
}
return false
}

// NewActionOperations will create a struct with operations based on their action.
func NewActionOperations(side generators.Side, spec asyncapi.Specification) ActionOperations {
var ao ActionOperations
Expand Down
11 changes: 8 additions & 3 deletions pkg/codegen/generators/v3/controller_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ import (
// internal controller struct and its options) used by both the application and
// user controllers. It is generated once, only when a controller side is
// generated, so that the `types` output stays free of controller boilerplate.
type ControllerBaseGenerator struct{}
type ControllerBaseGenerator struct {
// MultiMessageReceive is true when at least one generated controller side has
// a receive operation carrying several messages, which requires the
// multiplexed dispatcher machinery (issue #333).
MultiMessageReceive bool
}

// Generate will generate the shared controller base code.
func (ControllerBaseGenerator) Generate() (string, error) {
func (g ControllerBaseGenerator) Generate() (string, error) {
tmplt, err := loadTemplate(controllerBaseTemplatePath)
if err != nil {
return "", err
}

buf := new(bytes.Buffer)
if err := tmplt.Execute(buf, nil); err != nil {
if err := tmplt.Execute(buf, g); err != nil {
return "", err
}

Expand Down
24 changes: 23 additions & 1 deletion pkg/codegen/generators/v3/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,29 @@ func (g Generator) generateControllerBase() (string, error) {
return "", nil
}

return ControllerBaseGenerator{}.Generate()
return ControllerBaseGenerator{
MultiMessageReceive: g.hasMultiMessageReceive(),
}.Generate()
}

// hasMultiMessageReceive reports whether any of the controller sides that will
// be generated has a receive operation carrying several messages (issue #333).
func (g Generator) hasMultiMessageReceive() bool {
sides := make([]generators.Side, 0, 2)
if g.Options.Generate.Application {
sides = append(sides, generators.SideIsApplication)
}
if g.Options.Generate.User {
sides = append(sides, generators.SideIsUser)
}

for _, side := range sides {
if NewActionOperations(side, g.Specification).HasMultiMessageReceive() {
return true
}
}

return false
}

func (g Generator) generateApp() (string, error) {
Expand Down
Loading
Loading