-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[confmap] confmap honors Unmarshal methods on config embedded structs.
#9635
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
Changes from all commits
bb5b428
63231e7
4e8a093
9ce3bb8
5096427
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # Use this changelog template to create an entry for release notes. | ||
|
|
||
| # One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
| change_type: bug_fix | ||
|
|
||
| # The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver) | ||
| component: confmap | ||
|
|
||
| # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
| note: confmap honors `Unmarshal` methods on config embedded structs. | ||
|
|
||
| # One or more tracking issues or pull requests related to the change | ||
| issues: [6671] | ||
|
|
||
| # (Optional) One or more lines of additional information to render under the primary note. | ||
| # These lines will be padded with 2 spaces and then inserted directly into the document. | ||
| # Use pipe (|) for multiline entries. | ||
| subtext: | ||
|
|
||
| # Optional: The change log or logs in which this entry should be included. | ||
| # e.g. '[user]' or '[user, api]' | ||
| # Include 'user' if the change is relevant to end users. | ||
| # Include 'api' if there is a change to a library API. | ||
| # Default: '[user]' | ||
| change_logs: [] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -157,6 +157,9 @@ func decodeConfig(m *Conf, result any, errorUnused bool) error { | |
| mapstructure.StringToTimeDurationHookFunc(), | ||
| mapstructure.TextUnmarshallerHookFunc(), | ||
| unmarshalerHookFunc(result), | ||
| // after the main unmarshaler hook is called, | ||
| // we unmarshal the embedded structs if present to merge with the result: | ||
| unmarshalerEmbeddedStructsHookFunc(), | ||
| zeroSliceHookFunc(), | ||
| ), | ||
| } | ||
|
|
@@ -261,6 +264,42 @@ func mapKeyStringToMapKeyTextUnmarshalerHookFunc() mapstructure.DecodeHookFuncTy | |
| } | ||
| } | ||
|
|
||
| // unmarshalerEmbeddedStructsHookFunc provides a mechanism for embedded structs to define their own unmarshal logic, | ||
| // by implementing the Unmarshaler interface. | ||
| func unmarshalerEmbeddedStructsHookFunc() mapstructure.DecodeHookFuncValue { | ||
| return func(from reflect.Value, to reflect.Value) (any, error) { | ||
| if to.Type().Kind() != reflect.Struct { | ||
| return from.Interface(), nil | ||
| } | ||
| fromAsMap, ok := from.Interface().(map[string]any) | ||
| if !ok { | ||
| return from.Interface(), nil | ||
| } | ||
mx-psi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for i := 0; i < to.Type().NumField(); i++ { | ||
| // embedded structs passed in via `squash` cannot be pointers. We just check if they are structs: | ||
| if to.Type().Field(i).IsExported() && to.Type().Field(i).Anonymous { | ||
| if unmarshaler, ok := to.Field(i).Addr().Interface().(Unmarshaler); ok { | ||
| if err := unmarshaler.Unmarshal(NewFromStringMap(fromAsMap)); err != nil { | ||
| return nil, err | ||
| } | ||
| // the struct we receive from this unmarshaling only contains fields related to the embedded struct. | ||
| // we merge this partially unmarshaled struct with the rest of the result. | ||
| // note we already unmarshaled the main struct earlier, and therefore merge with it. | ||
| conf := New() | ||
| if err := conf.Marshal(unmarshaler); err != nil { | ||
| return nil, err | ||
| } | ||
| resultMap := conf.ToStringMap() | ||
| for k, v := range resultMap { | ||
| fromAsMap[k] = v | ||
| } | ||
|
Comment on lines
+288
to
+295
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this? This is not done in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We run after the main unmarshaler from
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, added. |
||
| } | ||
| } | ||
| } | ||
| return fromAsMap, nil | ||
| } | ||
| } | ||
|
|
||
| // Provides a mechanism for individual structs to define their own unmarshal logic, | ||
| // by implementing the Unmarshaler interface. | ||
| func unmarshalerHookFunc(result any) mapstructure.DecodeHookFuncValue { | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.