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: Allow struct field tags for influxdb with a marshaller #389

Closed
wants to merge 9 commits into from

Conversation

zob456
Copy link

@zob456 zob456 commented Oct 17, 2023

Closes #

Proposed Changes

Briefly describe your proposed changes:
My suggested change creates a function in api called MarshalStructToWritePoint that will take in a value of a custom type & return either a *write.Point correctly formatted or an error

The company I work for works with Influx & I've had to write a custom marshaller for just this reason. I also found some other threads on stackoverflow & other places asking if this was on the road map / covered by another package.

I haven't contributed to a go package like this before so I hope I am in-line with best practices here.

Any feedback is greatly appreciated even if my PR is not accepted!

Thanks!

Checklist

  • CHANGELOG.md updated
  • Rebased/mergeable
  • A test has been added if appropriate
  • Tests pass
  • Commit messages are in semantic format
  • Sign CLA (if not already signed)

- Adds marshal_struct_to_write_point.go
- Adds marshal_struct_to_write_point_test.go
…o tag-marshal

feat: adds MarshalStructToWritePoint function allowing users to pass a typed argument & get a *write.Point back
- Adds MarshalStructToWritePoint function in marshal_struct_to_write_point.go
- Adds marshal_struct_to_write_point_test.go
@zob456 zob456 changed the title Tag marshal Allow struct field tags for influxdb with a marshaller Oct 17, 2023
@codecov-commenter
Copy link

Codecov Report

Attention: 21 lines in your changes are missing coverage. Please review.

Comparison is base (fd16abc) 92.72% compared to head (6b78fbc) 92.05%.
Report is 1 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #389      +/-   ##
==========================================
- Coverage   92.72%   92.05%   -0.68%     
==========================================
  Files          23       24       +1     
  Lines        2242     2317      +75     
==========================================
+ Hits         2079     2133      +54     
- Misses        123      139      +16     
- Partials       40       45       +5     
Files Coverage Δ
api/marshal_struct_to_write_point.go 72.00% <72.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@powersj
Copy link
Contributor

powersj commented Oct 18, 2023

Thanks for the PR! I'll add this to our list to review and discuss the feature addition next week.

Thanks again!

@powersj powersj changed the title Allow struct field tags for influxdb with a marshaller feat: Allow struct field tags for influxdb with a marshaller Oct 18, 2023
@powersj
Copy link
Contributor

powersj commented Oct 24, 2023

@zob456,

Thanks for the PR. I think we would be interested in landing this. One idea we thought about is it would be nice to get a unmarshaller as well, so you could do the full loop.

I'll add this to the backlog to get you a full, proper review.

@zob456
Copy link
Author

zob456 commented Oct 24, 2023

@powersj great point! I hadn't thought of an Unmarshaller but that would be very helpful!

I'll look out for the backlog issue!

Thanks again!

@zob456
Copy link
Author

zob456 commented Nov 15, 2023

@powersj wanted to check back in & see if this is tracked somewhere as an issue so I can work on the additional functionality & open a proper PR?

@powersj
Copy link
Contributor

powersj commented Nov 16, 2023

Hi @zob456,

Ah I have the review of this PR tracked in the backlog for someone to provide a proper review. I do not have a public issue for the unmsarshaller, but feel free to put something up. Hopefully that answers your question.

Thanks

Copy link
Contributor

@sranka sranka left a comment

Choose a reason for hiding this comment

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

Thank you for your PR. There are a few nits in the implementation, but it looks good. The primary question is: "Should this be part of the client library?".

With the functionality introduced herein, the client library would internally

  1. convert a struct to write.Point with a help of influxdb tag on struct fields (scope of this PR)
  2. convert Point to lp.Metric
  3. encode lp metric to a protocol line and write protocol line(s)

I think that the conversion to a Point instance introduced herein is redundant ... the client library should better introduce a new WriteData function on WriteAPI as it was planned for a v3 version of this library. I would also prefer the approach taken in

func (p *PointsWriter) WriteData(points ...interface{}) {
, including annotations names and structure.

I think that this PR can introduce a good utility function outside the client library, but it should rather not become a new enhancement of the library.

import (
"errors"
"github.com/influxdata/influxdb-client-go/v2/api/write"
"log"
Copy link
Contributor

Choose a reason for hiding this comment

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

Pls use "github.com/influxdata/influxdb-client-go/v2/internal/log" as the rest of the client code does.

Comment on lines +64 to +68
// Tags is exported in case this is a type a user wants to use in their code
type Tags map[string]string

// Fields is exported in case this is a type a user wants to use in their code
type Fields map[string]interface{}
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no need to export this, and if there is no need, we should better not. If it would have to be exported, it would be already done to support the Point struct.

}
}

func checkEitherTagOrMeasurement(influxTag string) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

a nit: this fn performs a slightly different thing to what the name suggests ... it should be better named checkFieldTag WDYT?

@sranka
Copy link
Contributor

sranka commented Nov 28, 2023

I would be fine with the tags and the overall approach that already went through review in a v3 client in

func (p *PointsWriter) WriteData(points ...interface{}) {
.

@sranka sranka closed this Nov 28, 2023
@bednar bednar added this to the 2.14.5 milestone Nov 28, 2023
@bednar bednar added the wontfix This will not be worked on label Nov 28, 2023
@sranka
Copy link
Contributor

sranka commented Nov 29, 2023

#394 adds the same functionality using struct tags that were planned for v3 version of this library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants