Skip to content

Commit 9431223

Browse files
authored
Start hiding viper in public APIs (#2806)
The new Parser struct is mostly a wrapper around Viper with bunch of fixes. Signed-off-by: Bogdan Drutu <[email protected]>
1 parent 525e2cf commit 9431223

File tree

17 files changed

+180
-173
lines changed

17 files changed

+180
-173
lines changed

config/configparser/config.go

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
"github.com/spf13/viper"
2929

3030
"go.opentelemetry.io/collector/component"
31-
"go.opentelemetry.io/collector/config/configload"
31+
"go.opentelemetry.io/collector/config"
3232
"go.opentelemetry.io/collector/config/configmodels"
3333
)
3434

@@ -38,7 +38,6 @@ type configErrorCode int
3838

3939
const (
4040
_ configErrorCode = iota // skip 0, start errors codes from 1.
41-
errInvalidSubConfig
4241
errInvalidTypeAndNameKey
4342
errUnknownType
4443
errDuplicateName
@@ -94,12 +93,9 @@ type pipelineSettings struct {
9493
// typeAndNameSeparator is the separator that is used between type and name in type/name composite keys.
9594
const typeAndNameSeparator = "/"
9695

97-
// Load loads a Config from Viper.
96+
// Load loads a Config from Parser.
9897
// After loading the config, need to check if it is valid by calling `ValidateConfig`.
99-
func Load(
100-
v *viper.Viper,
101-
factories component.Factories,
102-
) (*configmodels.Config, error) {
98+
func Load(v *config.Parser, factories component.Factories) (*configmodels.Config, error) {
10399

104100
var config configmodels.Config
105101

@@ -120,27 +116,27 @@ func Load(
120116

121117
// Start with the service extensions.
122118

123-
extensions, err := loadExtensions(v.GetStringMap(extensionsKeyName), factories.Extensions)
119+
extensions, err := loadExtensions(cast.ToStringMap(v.Get(extensionsKeyName)), factories.Extensions)
124120
if err != nil {
125121
return nil, err
126122
}
127123
config.Extensions = extensions
128124

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

131-
receivers, err := loadReceivers(v.GetStringMap(receiversKeyName), factories.Receivers)
127+
receivers, err := loadReceivers(cast.ToStringMap(v.Get(receiversKeyName)), factories.Receivers)
132128
if err != nil {
133129
return nil, err
134130
}
135131
config.Receivers = receivers
136132

137-
exporters, err := loadExporters(v.GetStringMap(exportersKeyName), factories.Exporters)
133+
exporters, err := loadExporters(cast.ToStringMap(v.Get(exportersKeyName)), factories.Exporters)
138134
if err != nil {
139135
return nil, err
140136
}
141137
config.Exporters = exporters
142138

143-
processors, err := loadProcessors(v.GetStringMap(processorsKeyName), factories.Processors)
139+
processors, err := loadProcessors(cast.ToStringMap(v.Get(processorsKeyName)), factories.Processors)
144140
if err != nil {
145141
return nil, err
146142
}
@@ -550,29 +546,8 @@ func defaultUnmarshaler(componentViperSection *viper.Viper, intoCfg interface{})
550546
return componentViperSection.UnmarshalExact(intoCfg)
551547
}
552548

553-
// Copied from the Viper but changed to use the same delimiter
554-
// and return error if the sub is not a map.
555-
// See https://github.com/spf13/viper/issues/871
556-
func ViperSubExact(v *viper.Viper, key string) (*viper.Viper, error) {
557-
data := v.Get(key)
558-
if data == nil {
559-
return configload.NewViper(), nil
560-
}
561-
562-
if reflect.TypeOf(data).Kind() == reflect.Map {
563-
subv := configload.NewViper()
564-
// Cannot return error because the subv is empty.
565-
_ = subv.MergeConfigMap(cast.ToStringMap(data))
566-
return subv, nil
567-
}
568-
return nil, &configError{
569-
code: errInvalidSubConfig,
570-
msg: fmt.Sprintf("unexpected sub-config value kind for key:%s value:%v kind:%v)", key, data, reflect.TypeOf(data).Kind()),
571-
}
572-
}
573-
574549
func viperFromStringMap(data map[string]interface{}) *viper.Viper {
575-
v := configload.NewViper()
550+
v := config.NewViper()
576551
// Cannot return error because the subv is empty.
577552
_ = v.MergeConfigMap(cast.ToStringMap(data))
578553
return v

0 commit comments

Comments
 (0)