Skip to content

Commit

Permalink
add pattern support for mqtt message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Tenischev committed Nov 21, 2023
1 parent 439558c commit c2d565e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
14 changes: 13 additions & 1 deletion filters/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,16 @@ filter.currentTime = () => (new Date(Date.now())).toISOString();
function replaceAll(originalStr, replacePattern, replaceString) {
return originalStr.replaceAll(replacePattern, replaceString)
}
filter.replaceAll = replaceAll;
filter.replaceAll = replaceAll;

function toTopicString(channelName, hasParameters, parameters) {
if (hasParameters) {
let topicName = replaceAll(channelName, ".", "\.")
Object.keys(parameters).forEach(value => topicName = topicName.replace("{" + value + "}", ".*"))
return topicName
} else {
return channelName
}
}

filter.toTopicString = toTopicString
10 changes: 5 additions & 5 deletions template/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ mqtt:
connection: {{params.connectionTimeout}}
{% if server.binding('mqtt') and server.binding('mqtt').keepAlive | isDefined %}keepAlive: {{server.binding('mqtt').keepAlive}}{% endif %}
topic:
{% for channelName, channel in asyncapi.channels() %}{% if channel.hasPublish() %}
{{channel.publish().id() | camelCase}}: {{channelName}}
{% elif channel.hasSubscribe() %}
{{channel.subscribe().id() | camelCase}}: {{channelName}}
{% endif %}{% endfor %}
{%- for channelName, channel in asyncapi.channels() %}{% if channel.hasPublish() %}
{{channel.publish().id() | camelCase}}: {{channelName | toTopicString(channel.hasParameters(), channel.parameters()) }}
{%- elif channel.hasSubscribe() %}
{{channel.subscribe().id() | camelCase}}: {{channelName | toTopicString(channel.hasParameters(), channel.parameters())}}
{%- endif %}{% endfor %}
{% endif %}{% endfor %}

{%- if asyncapi | isProtocol('kafka') %}
Expand Down

0 comments on commit c2d565e

Please sign in to comment.