-
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
Changes from 9 commits
733a513
f03cabb
3e640df
e52d717
a5ceba1
08451b8
8ab53d5
7ec3127
75302d4
c30788a
8c0691f
26d01c6
2239eda
f749e36
2e5e539
f2dc1df
db90982
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,64 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// package configload implements the configuration Loader struct. | ||
package configload | ||
bogdandrutu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"github.com/spf13/cast" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
const ( | ||
// ViperDelimiter is used as the default key delimiter in the default viper instance | ||
ViperDelimiter = "::" | ||
mx-psi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
// Creates a new Viper instance with a different key-delimitor "::" instead of the | ||
// default ".". This way configs can have keys that contain ".". | ||
mx-psi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func NewViper() *viper.Viper { | ||
return viper.NewWithOptions(viper.KeyDelimiter(ViperDelimiter)) | ||
} | ||
|
||
// NewLoader creates a new Loader instanceg | ||
mx-psi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func NewLoader() *Loader { | ||
mx-psi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return &Loader{ | ||
v: NewViper(), | ||
} | ||
} | ||
|
||
// FromViper creates a Loader from a Viper instance | ||
mx-psi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func FromViper(v *viper.Viper) *Loader { | ||
return &Loader{v: v} | ||
} | ||
|
||
// Loader loads configuration | ||
type Loader struct { | ||
v *viper.Viper | ||
} | ||
|
||
// Viper returns the underlying Viper instance | ||
func (l *Loader) Viper() *viper.Viper { | ||
return l.v | ||
} | ||
|
||
// UnmarshalExact unmarshals the config into a struct, erroring if a field is nonexistent | ||
func (l *Loader) UnmarshalExact(intoCfg interface{}) error { | ||
return l.v.UnmarshalExact(intoCfg) | ||
} | ||
|
||
// 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 commentThe 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 commentThe 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. |
||
} |
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: