-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Create configload.Parser struct to abstract away from Viper #2728
Conversation
Codecov Report
@@ Coverage Diff @@
## main #2728 +/- ##
==========================================
- Coverage 91.67% 91.64% -0.04%
==========================================
Files 293 294 +1
Lines 15611 15635 +24
==========================================
+ Hits 14312 14329 +17
- Misses 890 896 +6
- Partials 409 410 +1
Continue to review full report at Codecov.
|
config/config.go
Outdated
// toStringMap creates a map[string]interface{} from a Viper object | ||
// It is equivalent to v.AllSettings() but it maps nil values | ||
// It is used here because of https://github.com/spf13/viper/issues/819 | ||
func toStringMap(v *viper.Viper) map[string]interface{} { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified if/once spf13/viper#819 gets resolved (it seems like Bogdan already stumbled on this one 😄). The code is the same as the AllSettings
method except that it does not skip over nil
values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not used anymore, cast.ToStringMap
seems to overcome this issue
config/config.go
Outdated
|
||
// ToCustomUnmarshaler creates a custom unmarshaler from a Viper unmarshaled. | ||
// It should not be used by new code. | ||
func ToCustomUnmarshaler(viperUnmarshaler func(*viper.Viper, interface{}) error) component.CustomUnmarshaler { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wrote this wrapper for convenience to fix existing tests but I don't think it should be used by new components.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe not in this PR, but I would like to remove this before we declare this module stable. Maybe we can open an issue and decide how to get rid of this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created #2735 to track this
(Note that contrib tests fail because this is a breaking public API change; we can fix this once this and #2597 are dealt with) |
config/config.go
Outdated
|
||
// ToCustomUnmarshaler creates a custom unmarshaler from a Viper unmarshaled. | ||
// It should not be used by new code. | ||
func ToCustomUnmarshaler(viperUnmarshaler func(*viper.Viper, interface{}) error) component.CustomUnmarshaler { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe not in this PR, but I would like to remove this before we declare this module stable. Maybe we can open an issue and decide how to get rid of this.
This commit also moves the `ConfigUnmarshaler` and `CustomUnmarshaler` types into the config package, since otherwise an import cycle would happen.
The `config` package indirectly depends on `exporterhelper`, so `Loader` can't be on it.
For some reason my commit is not showing up on the PR
@bogdandrutu I gave it a try, can you have a look? Note that I had to move some things around to prevent import cycles and that I am exposing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's start with the initial PR to add configload
package and move the viper helpers there.
config/configload/configload.go
Outdated
|
||
// ToStringMap creates a map[string]interface{} from a ConfigLoader | ||
func (l *Loader) ToStringMap() map[string]interface{} { | ||
return cast.ToStringMap(l.v) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure this will return the expected result, we need to test this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some tests, looks like it doesn't so I brought back the implementation I had initially.
- s/instanceg/instance - Add period at end of doc comments - Reword NewViper comment - Rename ViperDelimiter to KeyDelimiter
I think I addressed all your comments. I brought back the initial |
component/component.go
Outdated
type CustomUnmarshaler func(componentSection *configload.Parser, intoCfg interface{}) error | ||
|
||
// ToCustomUnmarshaler creates a custom unmarshaler from a Viper unmarshaler. | ||
func ToCustomUnmarshaler(viperUnmarshaler func(*viper.Viper, interface{}) error) CustomUnmarshaler { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we split for the moment this PR into 2:
- All changes except changes in component.go (we keep the old interface for the moment), but we add the Parser and move helpers for viper.
- Changes in the component.go (reason is maybe we just don't do these changes anymore and jump to add the new unmarshaler directly on the config, and deal that's it).
This commit was done by running git diff origin/main component/component.go exporter/ extension/ \ internal/testcomponents/example_exporter.go processor/ receiver/ | git apply -R
@bogdandrutu PTAL, I removed the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will iterate more in the following PRs.
Description:
configload.Parser
struct to encapsulate Viper.Link to tracking Issue: First step for #2596
Testing: Amended unit tests
Documentation: Updated documentation to reflect new interface