Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transforms): Add Topic Formatting Capability to MQTTSecretSender #872

Merged

Conversation

AlexCuse
Copy link
Contributor

Allow user to initialize MQTTSecretSender with a formatting function
that builds a final publish topic from the configured topic and the
input parameters to the MQTTSend function. Also change default behavior
to allow for placeholders for context values to be used in configured
topic, eg:

"topic/{context-key}" will publish to
"topic/current-value-at-context-key-for-this-pipeline-execution" on each call

Signed-off-by: Alex Ullrich [email protected]

PR Checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Currently MQTTSender publishes directly to the topic string configured.

Issue Number:
#871

What is the new behavior?

MQTTSender will replace {my-key} with the value found in the context at my-key by default. It will also be possible to configure a custom formatting function.

Does this PR introduce a breaking change?

  • Yes
  • No

Are there any new imports or modules? If so, what are they used for and why?

Are there any specific instructions or things that should be known prior to reviewing?

Other information

@AlexCuse AlexCuse force-pushed the mqttsender-topic-formatting branch from 52a3866 to 317530a Compare May 28, 2021 22:42
@AlexCuse AlexCuse changed the title feat(transform): Add Topic Formatting Capability to MQTTSecretSender feat(transforms): Add Topic Formatting Capability to MQTTSecretSender May 28, 2021
Allow user to initialize MQTTSecretSender with a formatting function
that builds a final publish topic from the configured topic and the
input parameters to the MQTTSend function.  Also change default behavior
to allow for placeholders for context values to be used in configured
topic, eg:

"topic/{context-key}" will publish to
"topic/current-value-at-context-key-for-this-pipeline-execution" on each call

Signed-off-by: Alex Ullrich <[email protected]>
@AlexCuse AlexCuse force-pushed the mqttsender-topic-formatting branch from 317530a to bb3ec87 Compare May 28, 2021 22:43
@AlexCuse AlexCuse requested a review from lenny-goodell May 28, 2021 22:43
@codecov-commenter
Copy link

Codecov Report

Merging #872 (bb3ec87) into master (c799dd1) will increase coverage by 0.01%.
The diff coverage is 61.11%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #872      +/-   ##
==========================================
+ Coverage   62.33%   62.35%   +0.01%     
==========================================
  Files          31       31              
  Lines        2031     2048      +17     
==========================================
+ Hits         1266     1277      +11     
- Misses        675      679       +4     
- Partials       90       92       +2     
Impacted Files Coverage Δ
pkg/transforms/mqttsecret.go 30.23% <61.11%> (+8.49%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c799dd1...bb3ec87. Read the comment docs.

Copy link
Member

@lenny-goodell lenny-goodell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

publishTopic, err = sender.topicFormatter(publishTopic, ctx, data)
} else {
for k, v := range ctx.GetAllValues() {
publishTopic = strings.Replace(publishTopic, fmt.Sprintf("{%s}", k), v, -1)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would using text/template make sense here instead of the replace?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this approach does have an advantage, since text/template wasn't working well with special key names such as those containing a "$" character.

Copy link
Contributor Author

@AlexCuse AlexCuse May 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it would be best to keep the complexities of text/template (like you mention) out of the default behavior. It would require more complex expressions to render against the context map as well, and wouldn't provide a way for us to catch "replacement not found" errors that I can see. Have a look here:

https://play.golang.org/p/QwUSeJnLaTh

That said I did add the ability to specify your own topic format function as well, so if you wanted to do something with template that fits your use case it should be easy enough to plug in.

Copy link

@fakhir fakhir May 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a good idea to include the custom formatting function.

A simpler template format and finding replacement errors can be managed with text/template as here:
https://play.golang.org/p/QUJb5aA0IbF

btw, looking at your example, I realized that the index format which you used also does allow referencing names which special characters (e.g. {{ index . "$device" }}).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a end user perspective, I like the simplicity of the base-topic/{profilename}/device/{devicename}/state syntax.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, yes it is simpler from the user's perspective. I'll update my implementation in the MQTT trigger to use Alex's method.

@lenny-goodell lenny-goodell linked an issue Jun 1, 2021 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SDK: Dynamic Topic Formatting for MQTTSecretSender
4 participants