Skip to content

Commit

Permalink
Remove config.NewViper, users should use config.NewParser (#2917)
Browse files Browse the repository at this point in the history
* Remove config.NewViper, users should use config.Parser

Signed-off-by: Bogdan Drutu <[email protected]>

* Update changelog

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Apr 13, 2021
1 parent 8507024 commit b428785
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
## 🛑 Breaking changes 🛑

- Remove `configparser.DecodeTypeAndName`, use `config.IDFromString` (#2869)
- Remove config.NewViper, users should use config.NewParser (#2917)

## 💡 Enhancements 💡

Expand Down
14 changes: 7 additions & 7 deletions config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ const (
KeyDelimiter = "::"
)

// NewViper creates a new Viper instance with key delimiter KeyDelimiter instead of the
// newViper creates a new Viper instance with key delimiter KeyDelimiter instead of the
// default ".". This way configs can have keys that contain ".".
func NewViper() *viper.Viper {
func newViper() *viper.Viper {
return viper.NewWithOptions(viper.KeyDelimiter(KeyDelimiter))
}

// NewParser creates a new empty Parser instance.
func NewParser() *Parser {
return &Parser{
v: NewViper(),
v: newViper(),
}
}

// NewParserFromFile creates a new Parser by reading the given file.
func NewParserFromFile(fileName string) (*Parser, error) {
// Read yaml config from file
v := NewViper()
v := newViper()
v.SetConfigFile(fileName)
if err := v.ReadInConfig(); err != nil {
return nil, fmt.Errorf("unable to read the file %v: %w", fileName, err)
Expand All @@ -55,7 +55,7 @@ func NewParserFromFile(fileName string) (*Parser, error) {

// NewParserFromBuffer creates a new Parser by reading the given yaml buffer.
func NewParserFromBuffer(buf io.Reader) (*Parser, error) {
v := NewViper()
v := newViper()
v.SetConfigType("yaml")
if err := v.ReadConfig(buf); err != nil {
return nil, err
Expand All @@ -65,7 +65,7 @@ func NewParserFromBuffer(buf io.Reader) (*Parser, error) {

// NewParserFromStringMap creates a parser from a map[string]interface{}.
func NewParserFromStringMap(data map[string]interface{}) *Parser {
v := NewViper()
v := newViper()
// Cannot return error because the subv is empty.
_ = v.MergeConfigMap(data)
return &Parser{v: v}
Expand Down Expand Up @@ -127,7 +127,7 @@ func (l *Parser) Sub(key string) (*Parser, error) {
}

if reflect.TypeOf(data).Kind() == reflect.Map {
subv := NewViper()
subv := newViper()
// Cannot return error because the subv is empty.
_ = subv.MergeConfigMap(cast.ToStringMap(data))
return &Parser{v: subv}, nil
Expand Down
4 changes: 3 additions & 1 deletion service/parserprovider/setflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
"strings"

"github.com/spf13/viper"

"go.opentelemetry.io/collector/config"
)

Expand Down Expand Up @@ -52,7 +54,7 @@ func (sfl *setFlagProvider) Get() (*config.Parser, error) {
return nil, err
}
}
viperFlags := config.NewViper()
viperFlags := viper.NewWithOptions(viper.KeyDelimiter(config.KeyDelimiter))
viperFlags.SetConfigType(setFlagFileType)
if err := viperFlags.ReadConfig(b); err != nil {
return nil, fmt.Errorf("failed to read set flag config: %v", err)
Expand Down

0 comments on commit b428785

Please sign in to comment.