Skip to content

Conversation

@doits
Copy link

@doits doits commented Apr 27, 2025

Fix #20501

Erster Versuch, erst mal nur im MQTT-Plugin.

Achtung: Das erste Mal, dass ich Go-Code umbaue bzw. in Go programmiere. Anfängerfehler vorprogrammiert, was vielleicht auch die nachfolgenden Fragen zeigen.

Fragen:

  • Wie kann man das grundsätzlich testen, ohne dass gleich ein echter MQTT-Server im Hintergrund laufen muss?
  • Kann man das mit weniger Änderungen testen, ohne das MQTT-Plugin nur wegen des Tests so zu refactoren?
  • Aktuell wird die neue FormatPayload-Funktion getestet (die gibt es eigentlich nur, damit man sie im Test nutzen kann 😅 ), aber eigentlich wäre es besser zu testen, was am Ende zu m.client.Publish gesendet wird – also was letztendlich "rausgeht". Sehe aktuell nicht, wie man das m.client.Publish im test mocken könnte, damit man dort das payload rausfischen kann.

Ich denke ich brauche etwas Hilfe/Denkanstöße von erfahrenen Programmieren, damit ich hier sauber weiterkomme. Gebt gerne kritisches Feedback und/oder ein paar Hinweise, wie ihr es angehen könntet.

Summary by Sourcery

Introduce payload formatting using jq in the MQTT plugin and refactor publishing logic.

New Features:

  • Allow formatting MQTT message payloads using jq expressions before publishing.

Enhancements:

  • Refactor MQTT publishing logic into reusable FormatPayload and Publish methods.

Tests:

  • Add a unit test for the new jq payload formatting functionality.

@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Apr 27, 2025

Reviewer's Guide by Sourcery

This pull request introduces payload formatting using jq for the MQTT plugin. It refactors the existing setter functions to use a new Publish helper function, which in turn calls a new FormatPayload function to process the payload using a jq pipeline if configured. A new test is added to verify the FormatPayload functionality.

No diagrams generated as the changes look simple and do not need a visual representation.

File-Level Changes

Change Details Files
Introduce new helper functions for formatting and publishing payloads.
  • Add FormatPayload function to process the payload using a jq pipeline.
  • Add Publish function to format and publish a value to the topic.
plugin/mqtt.go
Refactor setter functions to use the new Publish helper.
  • Update IntSetter to call m.Publish.
  • Update FloatSetter to call m.Publish.
  • Update BoolSetter to call m.Publish.
  • Update StringSetter to call m.Publish.
plugin/mqtt.go
Add a new test for the FormatPayload function.
  • Add TestFormatPayload to verify payload processing with a jq expression.
plugin/mqtt_test.go

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

return "", err
}

if m.pipeline != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Wäre es nicht einfacher und weniger diff gewesen, diesen Schritt in setFormattedValue mit einzubauen? Es muss ohnehin auch analog für http, script etc erfolgen.

@andig andig added the enhancement New feature or request label May 1, 2025
@github-actions github-actions bot added the stale Outdated and ready to close label May 8, 2025
@andig andig closed this May 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request stale Outdated and ready to close

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Plugins: Anpassung des Payloads per JQ

2 participants