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

[confmap] confmap honors Unmarshal methods on config embedded structs. #9635

Merged
merged 5 commits into from Mar 12, 2024

Conversation

atoulme
Copy link
Contributor

@atoulme atoulme commented Feb 25, 2024

Description:
This implements support for calling Unmarshal on embedded structs of structs being decoded.

Link to tracking Issue:
Fixes #6671

Testing:
Unit tests.

Contrib fix is open: open-telemetry/opentelemetry-collector-contrib#31406

@atoulme atoulme requested a review from a team as a code owner February 25, 2024 01:36
@atoulme atoulme force-pushed the support_embedded_structs_confmap branch 3 times, most recently from 3a7b443 to d3c51b0 Compare February 25, 2024 02:18
Copy link

codecov bot commented Feb 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.99%. Comparing base (b808e85) to head (5096427).
Report is 65 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9635      +/-   ##
==========================================
+ Coverage   90.89%   90.99%   +0.10%     
==========================================
  Files         347      349       +2     
  Lines       18324    18600     +276     
==========================================
+ Hits        16655    16925     +270     
- Misses       1344     1348       +4     
- Partials      325      327       +2     

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

@atoulme atoulme force-pushed the support_embedded_structs_confmap branch 2 times, most recently from 2f769b6 to 0fffbeb Compare February 25, 2024 05:29
@atoulme atoulme force-pushed the support_embedded_structs_confmap branch from bd0f8f2 to 63231e7 Compare February 28, 2024 02:43
@mx-psi mx-psi linked an issue Mar 1, 2024 that may be closed by this pull request
Copy link
Member

@mx-psi mx-psi left a comment

Choose a reason for hiding this comment

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

Thanks for working on this 🙇 🙇 I left a lot of comments, some probably stemming from my lack of familiarity with reflect.

confmap/confmap.go Outdated Show resolved Hide resolved
confmap/confmap.go Show resolved Hide resolved
confmap/confmap.go Outdated Show resolved Hide resolved
confmap/confmap.go Show resolved Hide resolved
confmap/confmap.go Outdated Show resolved Hide resolved
Comment on lines +280 to +287
conf := New()
if err := conf.Marshal(unmarshaler); err != nil {
return nil, err
}
resultMap := conf.ToStringMap()
for k, v := range resultMap {
fromAsMap[k] = v
}
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this? This is not done in unmarshalerHookFunc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We run after the main unmarshaler from unmarshalerHookFunc and must merge with the result, rather than set it.

Copy link
Member

Choose a reason for hiding this comment

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

Oki, can you add a comment about this? Both in the function itself as well as in mapstructure's configuration to make sure we don't change the order.

Also, I think it would be good to have a test that only triggers unmarshalerHookFunc and test that only triggers this hook (so, one struct with only embedded structs inside and one struct with no embedded structs inside in the tests).

Copy link
Contributor Author

@atoulme atoulme Mar 7, 2024

Choose a reason for hiding this comment

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

  1. Comment: yes,sure can do
  2. oh no
  3. sure can do, I believe https://github.com/open-telemetry/opentelemetry-collector/blob/main/confmap/confmap_test.go#L337 already covers this well.

2. is a big "oh no" because of #7102. We have a top level if/else that explicitly forks the flow if the struct implements Unmarshaler or not.
If it does, it calls directly the Unmarshal function on the struct.
If it doesn't, it goes to mapstructure and then runs through the hooks.
This is this problem: #9635 (comment)

The workaround right now is that all structs with embedded structs that themselves implement Unmarshaler must themselves implement Unmarshaler and call mapstructure as part of their behavior.

We can straighten this up, but maybe in the next PR, if that's ok.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for explaining and being patient with me :) Here is my proposal on what to do:

  1. On this PR, we add a new (failing) test for (2) and that uses t.Skip with a TODO linking to Remove component.UnmarshalConfig #7102. We also add a note on Remove component.UnmarshalConfig #7102 about said skip to not forget about it.
  2. On a future PR tackling Remove component.UnmarshalConfig #7102 we remove this t.Skip

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, added.

confmap/confmap_test.go Show resolved Hide resolved
@mx-psi mx-psi merged commit fbc0ce0 into open-telemetry:main Mar 12, 2024
47 checks passed
@github-actions github-actions bot added this to the next release milestone Mar 12, 2024
@atoulme atoulme deleted the support_embedded_structs_confmap branch March 12, 2024 15:36
XinRanZhAWS pushed a commit to XinRanZhAWS/opentelemetry-collector-contrib that referenced this pull request Mar 13, 2024
tomasmota pushed a commit to tomasmota/opentelemetry-collector that referenced this pull request Mar 14, 2024
…ts. (open-telemetry#9635)

**Description:**
This implements support for calling `Unmarshal` on embedded structs of
structs being decoded.

**Link to tracking Issue:**
Fixes open-telemetry#6671

**Testing:**
Unit tests.

Contrib fix is open:
open-telemetry/opentelemetry-collector-contrib#31406
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.

Decide what to do w/ mapstructure library Confmap does not honor Unmarshal methods on embedded structs
2 participants