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

Create configload.Parser struct to abstract away from Viper #2728

Merged
merged 17 commits into from
Mar 25, 2021

Conversation

mx-psi
Copy link
Member

@mx-psi mx-psi commented Mar 18, 2021

Description:

  • Create 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

@codecov
Copy link

codecov bot commented Mar 18, 2021

Codecov Report

Merging #2728 (db90982) into main (34c17ea) will decrease coverage by 0.03%.
The diff coverage is 78.12%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
config/configload/configload.go 72.00% <72.00%> (ø)
config/configparser/config.go 98.62% <100.00%> (-0.01%) ⬇️
config/configtest/configtest.go 100.00% <100.00%> (ø)
service/application.go 76.52% <100.00%> (ø)
service/set_flag.go 80.95% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 34c17ea...db90982. Read the comment docs.

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{} {
Copy link
Member Author

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.

Copy link
Member Author

@mx-psi mx-psi Mar 22, 2021

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 {
Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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

@mx-psi mx-psi marked this pull request as ready for review March 18, 2021 13:52
@mx-psi mx-psi requested a review from a team March 18, 2021 13:52
@mx-psi
Copy link
Member Author

mx-psi commented Mar 18, 2021

(Note that contrib tests fail because this is a breaking public API change; we can fix this once this and #2597 are dealt with)

component/component.go Outdated Show resolved Hide resolved
component/component.go Outdated Show resolved Hide resolved
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 {
Copy link
Member

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.

component/component.go Outdated Show resolved Hide resolved
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
@mx-psi
Copy link
Member Author

mx-psi commented Mar 22, 2021

@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 Viper() temporarily because of that (to be removed when fixing #2735, which I can do here if you don't mind it being a bigger PR)

Copy link
Member

@bogdandrutu bogdandrutu left a 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 Show resolved Hide resolved

// ToStringMap creates a map[string]interface{} from a ConfigLoader
func (l *Loader) ToStringMap() map[string]interface{} {
return cast.ToStringMap(l.v)
Copy link
Member

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.

Copy link
Member Author

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.

config/configload/configload.go Outdated Show resolved Hide resolved
config/configload/configload.go Outdated Show resolved Hide resolved
config/configload/configload.go Outdated Show resolved Hide resolved
- s/instanceg/instance
- Add period at end of doc comments
- Reword NewViper comment
- Rename ViperDelimiter to KeyDelimiter
@mx-psi
Copy link
Member Author

mx-psi commented Mar 23, 2021

I think I addressed all your comments. I brought back the initial ToStringMap definition since cast.ToStringMap doesn't work (I got confused and thought it would since it is used in some config/config.go code for unrelated stuff)

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 {
Copy link
Member

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:

  1. 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.
  2. 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
@mx-psi mx-psi changed the title Hide Viper from unmarshaler interface Create configload.Parser struct to abstract away from Viper Mar 25, 2021
@mx-psi
Copy link
Member Author

mx-psi commented Mar 25, 2021

@bogdandrutu PTAL, I removed the component.go and related changes (check_links failure is unrelated to this PR)

Copy link
Member

@bogdandrutu bogdandrutu left a 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.

@bogdandrutu bogdandrutu merged commit 06ea2c6 into open-telemetry:main Mar 25, 2021
@mx-psi mx-psi deleted the mx-psi/remove-viper branch March 26, 2021 11:26
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.

2 participants