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
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,30 @@ func (t *Settings) SetDefaults() {

Defaults are supported for scalar properties (string, boolean, integer, number).

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

When a channel parameter declares a `location` runtime expression, the generated
send functions auto-fill that parameter from the outgoing message when the
caller leaves it empty. The parameter stays overridable: if you set it
explicitly, your value is used as-is.

```yaml
channels:
user:
address: app.user.{userId}
parameters:
userId:
location: $message.payload#/userId
messages:
userSignedUp:
$ref: '#/components/messages/userSignedUp'
```

With the spec above, `SendAs...` derives `userId` from `msg.Payload.UserId`, so
the message is published on `app.user.<userId>` without passing the parameter
manually. Supported locations are `$message.payload#/...` and
`$message.header#/...`, and the targeted field must be a string.

### ErrorHandler

You can use an error handler that will be executed when processing for messages
Expand Down
36 changes: 36 additions & 0 deletions pkg/asyncapi/v3/operation.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package asyncapiv3

import "strings"

// OperationAction represents an OperationAction.
type OperationAction string

Expand Down Expand Up @@ -157,9 +159,43 @@ func (op *Operation) setDependencies(spec Specification) error {
// Generate reply
op.generateReply()

// Resolve channel parameter locations against the operation message
op.setParametersLocationDependencies()

return nil
}

// setParametersLocationDependencies resolves, for each channel parameter that
// defines a `location` runtime expression, the field of the operation message
// it points to. It ensures the field exists on the message (creating the schema
// tree if needed, like correlation IDs do) and records whether that field is
// required so the generated code knows if it is a pointer. This lets the
// generated send functions auto-fill the parameter from the message.
func (op *Operation) setParametersLocationDependencies() {
ch := op.Channel.Follow()
if ch == nil || len(ch.Parameters) == 0 {
return
}

// The location refers to the operation message; if there is none, there is
// nothing to resolve.
msg, err := op.GetMessage()
if err != nil {
return
}

for _, p := range ch.Parameters {
pf := p.Follow()
if pf.Location == "" {
continue
}

locationParent := msg.createTreeUntilLocation(pf.Location)
path := strings.Split(pf.Location, "/")
pf.LocationRequired = locationParent.IsFieldRequired(path[len(path)-1])
}
}

func (op *Operation) setReference(spec Specification) error {
if op.Reference == "" {
return nil
Expand Down
13 changes: 13 additions & 0 deletions pkg/asyncapi/v3/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,19 @@ type Parameter struct {
// Non AsyncAPI fields
Name string `json:"-"`
ReferenceTo *Parameter `json:"-"`

// LocationRequired indicates whether the field targeted by Location is a
// required field of the message (and thus generated as a non-pointer field).
LocationRequired bool `json:"-"`
}

// Follow returns the referenced parameter if there is one, otherwise the
// parameter itself.
func (p *Parameter) Follow() *Parameter {
if p.ReferenceTo != nil {
return p.ReferenceTo
}
return p
}

// generateMetadata generates metadata for the Parameter.
Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/generators/v3/templates/controller.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ func (c *{{ $.Prefix }}Controller) {{ sendFuncName $value $.Prefix }}(
{{- end}}
msg {{opToMsgTypeName $value}},
) error {
{{- template "fill-parameters-from-location" $value }}
// Set channel address
{{- if eq .Channel.Follow.Address "" }}
addr := chanAddr
Expand Down
23 changes: 23 additions & 0 deletions pkg/codegen/generators/v3/templates/message.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,26 @@ func (msg *{{namify .Name}}) SetAsResponseFrom(req MessageWithCorrelationID) {
{{- end -}}

{{- end }}

{{- /* ---------------------------------------------------------------------- */ -}}
{{- /* fill-parameters-from-location auto-fills channel parameters that define */ -}}
{{- /* a `location` runtime expression from the outgoing message, unless the */ -}}
{{- /* caller already set them. The dot is the operation. (issue #139) */ -}}
{{- define "fill-parameters-from-location" -}}
{{- if .Channel.Follow.Parameters}}
{{- range $pkey, $pvalue := .Channel.Follow.Parameters}}
{{- if $pvalue.Follow.Location}}
// Fill {{namify $pkey}} from the message ({{$pvalue.Follow.Location}}) if not set
if params.{{namify $pkey}} == "" {
{{- if $pvalue.Follow.LocationRequired}}
params.{{namify $pkey}} = msg.{{referenceToStructAttributePath $pvalue.Follow.Location}}
{{- else}}
if v := msg.{{referenceToStructAttributePath $pvalue.Follow.Location}}; v != nil {
params.{{namify $pkey}} = *v
}
{{- end}}
}
{{- end}}
{{- end}}
{{- end}}
{{- end -}}
Loading
Loading