Skip to content

Start hiding viper in public APIs #2806

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

Merged
merged 1 commit into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 8 additions & 33 deletions config/configparser/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
"github.com/spf13/viper"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config/configload"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configmodels"
)

Expand All @@ -38,7 +38,6 @@ type configErrorCode int

const (
_ configErrorCode = iota // skip 0, start errors codes from 1.
errInvalidSubConfig
errInvalidTypeAndNameKey
errUnknownType
errDuplicateName
Expand Down Expand Up @@ -94,12 +93,9 @@ type pipelineSettings struct {
// typeAndNameSeparator is the separator that is used between type and name in type/name composite keys.
const typeAndNameSeparator = "/"

// Load loads a Config from Viper.
// Load loads a Config from Parser.
// After loading the config, need to check if it is valid by calling `ValidateConfig`.
func Load(
v *viper.Viper,
factories component.Factories,
) (*configmodels.Config, error) {
func Load(v *config.Parser, factories component.Factories) (*configmodels.Config, error) {

var config configmodels.Config

Expand All @@ -120,27 +116,27 @@ func Load(

// Start with the service extensions.

extensions, err := loadExtensions(v.GetStringMap(extensionsKeyName), factories.Extensions)
extensions, err := loadExtensions(cast.ToStringMap(v.Get(extensionsKeyName)), factories.Extensions)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the final destination? Are we going to get rid of cast.ToStringMap?

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 don't think I need it, but will followup. I want to start with a minimal API for Parser

if err != nil {
return nil, err
}
config.Extensions = extensions

// Load data components (receivers, exporters, and processors).

receivers, err := loadReceivers(v.GetStringMap(receiversKeyName), factories.Receivers)
receivers, err := loadReceivers(cast.ToStringMap(v.Get(receiversKeyName)), factories.Receivers)
if err != nil {
return nil, err
}
config.Receivers = receivers

exporters, err := loadExporters(v.GetStringMap(exportersKeyName), factories.Exporters)
exporters, err := loadExporters(cast.ToStringMap(v.Get(exportersKeyName)), factories.Exporters)
if err != nil {
return nil, err
}
config.Exporters = exporters

processors, err := loadProcessors(v.GetStringMap(processorsKeyName), factories.Processors)
processors, err := loadProcessors(cast.ToStringMap(v.Get(processorsKeyName)), factories.Processors)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -550,29 +546,8 @@ func defaultUnmarshaler(componentViperSection *viper.Viper, intoCfg interface{})
return componentViperSection.UnmarshalExact(intoCfg)
}

// Copied from the Viper but changed to use the same delimiter
// and return error if the sub is not a map.
// See https://github.com/spf13/viper/issues/871
func ViperSubExact(v *viper.Viper, key string) (*viper.Viper, error) {
data := v.Get(key)
if data == nil {
return configload.NewViper(), nil
}

if reflect.TypeOf(data).Kind() == reflect.Map {
subv := configload.NewViper()
// Cannot return error because the subv is empty.
_ = subv.MergeConfigMap(cast.ToStringMap(data))
return subv, nil
}
return nil, &configError{
code: errInvalidSubConfig,
msg: fmt.Sprintf("unexpected sub-config value kind for key:%s value:%v kind:%v)", key, data, reflect.TypeOf(data).Kind()),
}
}

func viperFromStringMap(data map[string]interface{}) *viper.Viper {
v := configload.NewViper()
v := config.NewViper()
// Cannot return error because the subv is empty.
_ = v.MergeConfigMap(cast.ToStringMap(data))
return v
Expand Down
Loading