From cc3708f414d8d5a9ccd15b35930cead2eb4bb297 Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Wed, 12 Jan 2022 15:24:46 -0800 Subject: [PATCH] Move service.ConfigMapConverterFunc to config.MapConverterFunc The drop of the Config prefix from the func name was forced by go lint, which does not like to repeat the package name as the prefix for types. Signed-off-by: Bogdan Drutu --- config/configmap.go | 5 +++++ config/configmapprovider/properties.go | 3 +-- service/config_provider.go | 10 +++------- service/config_provider_test.go | 4 ++-- service/internal/configprovider/expand.go | 4 +--- 5 files changed, 12 insertions(+), 14 deletions(-) diff --git a/config/configmap.go b/config/configmap.go index cfd66b42c4c..deeb975ba17 100644 --- a/config/configmap.go +++ b/config/configmap.go @@ -30,6 +30,11 @@ const ( KeyDelimiter = "::" ) +// MapConverterFunc is a converter function for the config.Map that allows distributions +// (in the future components as well) to build backwards compatible config converters. +// TODO: Consider to add a context as the first argument. +type MapConverterFunc func(*Map) error + // NewMap creates a new empty config.Map instance. func NewMap() *Map { return &Map{k: koanf.New(KeyDelimiter)} diff --git a/config/configmapprovider/properties.go b/config/configmapprovider/properties.go index 832ec2ac1a5..4b6ecf9a610 100644 --- a/config/configmapprovider/properties.go +++ b/config/configmapprovider/properties.go @@ -30,8 +30,7 @@ import ( // Properties must follow the Java properties format, key-value list separated by equal sign with a "." // as key delimiter. // ["processors.batch.timeout=2s", "processors.batch/foo.timeout=3s"] -// TODO: Find the right place for service.ConfigMapConverterFunc and return that. Currently this will cause circular dependency. -func NewOverwritePropertiesConverter(properties []string) func(*config.Map) error { +func NewOverwritePropertiesConverter(properties []string) config.MapConverterFunc { return func(cfgMap *config.Map) error { return convert(properties, cfgMap) } diff --git a/service/config_provider.go b/service/config_provider.go index b372a1883ab..964d66dea5f 100644 --- a/service/config_provider.go +++ b/service/config_provider.go @@ -67,7 +67,7 @@ type ConfigProvider interface { type configProvider struct { locations []string configMapProviders map[string]configmapprovider.Provider - cfgMapConverters []ConfigMapConverterFunc + cfgMapConverters []config.MapConverterFunc configUnmarshaler configunmarshaler.ConfigUnmarshaler sync.Mutex @@ -86,7 +86,7 @@ type configProvider struct { func NewConfigProvider( locations []string, configMapProviders map[string]configmapprovider.Provider, - cfgMapConverters []ConfigMapConverterFunc, + cfgMapConverters []config.MapConverterFunc, configUnmarshaler configunmarshaler.ConfigUnmarshaler) ConfigProvider { return &configProvider{ locations: locations, @@ -97,10 +97,6 @@ func NewConfigProvider( } } -// ConfigMapConverterFunc is a converter function for the config.Map that allows distributions -// (in the future components as well) to build backwards compatible config converters. -type ConfigMapConverterFunc func(*config.Map) error - // NewDefaultConfigProvider returns the default ConfigProvider, and it creates configuration from a file // defined by the given configFile and overwrites fields using properties. func NewDefaultConfigProvider(configFileName string, properties []string) ConfigProvider { @@ -110,7 +106,7 @@ func NewDefaultConfigProvider(configFileName string, properties []string) Config "file": configmapprovider.NewFile(), "env": configmapprovider.NewEnv(), }, - []ConfigMapConverterFunc{ + []config.MapConverterFunc{ configmapprovider.NewOverwritePropertiesConverter(properties), configprovider.NewExpandConverter(), }, diff --git a/service/config_provider_test.go b/service/config_provider_test.go index 6af6bb30565..3d5b58b01d2 100644 --- a/service/config_provider_test.go +++ b/service/config_provider_test.go @@ -100,7 +100,7 @@ func TestConfigProvider_Errors(t *testing.T) { name string locations []string parserProvider map[string]configmapprovider.Provider - cfgMapConverters []ConfigMapConverterFunc + cfgMapConverters []config.MapConverterFunc configUnmarshaler configunmarshaler.ConfigUnmarshaler expectNewErr bool expectWatchErr bool @@ -142,7 +142,7 @@ func TestConfigProvider_Errors(t *testing.T) { "mock": &mockProvider{}, "file": configmapprovider.NewFile(), }, - cfgMapConverters: []ConfigMapConverterFunc{func(c *config.Map) error { return errors.New("converter_err") }}, + cfgMapConverters: []config.MapConverterFunc{func(c *config.Map) error { return errors.New("converter_err") }}, configUnmarshaler: configunmarshaler.NewDefault(), expectNewErr: true, }, diff --git a/service/internal/configprovider/expand.go b/service/internal/configprovider/expand.go index 2edb6ec14a7..32e40a96ba0 100644 --- a/service/internal/configprovider/expand.go +++ b/service/internal/configprovider/expand.go @@ -21,9 +21,7 @@ import ( ) // NewExpandConverter returns a service.ConfigMapConverterFunc, that expands all environment variables for a given config.Map. -// -// This does not directly return service.ConfigMapConverterFunc to avoid circular dependencies, not a problem since it is internal. -func NewExpandConverter() func(*config.Map) error { +func NewExpandConverter() config.MapConverterFunc { return func(cfgMap *config.Map) error { for _, k := range cfgMap.AllKeys() { cfgMap.Set(k, expandStringValues(cfgMap.Get(k)))