Skip to content

Commit

Permalink
Replace NewRetrievedFromMap with NewRetrieved (#5468)
Browse files Browse the repository at this point in the history
It is ok to change NewRetrievedFromMap to NewRetrieved since it was just moved, not yet released. This is a step towards supporting providing any value type.

Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Jun 3, 2022
1 parent 8ed9e43 commit e0cf05e
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- `config.Map` -> `confmap.ConfMap`
- `config.MapProvider` -> `confmap.Provider`
- `config.Received` -> `confmap.Received`
- `config.NewReceivedFromMap` -> `confmap.NewReceived`
- `config.CloseFunc` -> `confmap.CloseFunc`
- `config.ChangeEvent` -> `confmap.ChangeEvent`
- `config.MapConverter` -> `confmap.Converter`
Expand Down
7 changes: 5 additions & 2 deletions config/moved_configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ type RetrievedOption = confmap.RetrievedOption
// Deprecated: [v0.53.0] use confmap.WithRetrievedClose
var WithRetrievedClose = confmap.WithRetrievedClose

// Deprecated: [v0.53.0] use confmap.NewRetrievedFromMap
var NewRetrievedFromMap = confmap.NewRetrievedFromMap
// Deprecated: [v0.53.0] use confmap.NewRetrieved
func NewRetrievedFromMap(conf *confmap.Conf, opts ...confmap.RetrievedOption) confmap.Retrieved {
ret, _ := confmap.NewRetrieved(conf.ToStringMap(), opts...)
return ret
}

// Deprecated: [v0.53.0] use confmap.WatcherFunc
type WatcherFunc = confmap.WatcherFunc
Expand Down
6 changes: 3 additions & 3 deletions confmap/confmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestToStringMap(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
conf := newConfFromFile(t, test.fileName)
assert.Equal(t, test.stringMap, conf.ToStringMap())
assert.Equal(t, test.stringMap, conf)
})
}
}
Expand Down Expand Up @@ -221,12 +221,12 @@ func TestMapKeyStringToMapKeyTextUnmarshalerHookFuncErrorUnmarshal(t *testing.T)
}

// newConfFromFile creates a new Conf by reading the given file.
func newConfFromFile(t *testing.T, fileName string) *Conf {
func newConfFromFile(t *testing.T, fileName string) map[string]interface{} {
content, err := ioutil.ReadFile(filepath.Clean(fileName))
require.NoErrorf(t, err, "unable to read the file %v", fileName)

var data map[string]interface{}
require.NoError(t, yaml.Unmarshal(content, &data), "unable to parse yaml")

return NewFromStringMap(data)
return NewFromStringMap(data).ToStringMap()
}
10 changes: 3 additions & 7 deletions confmap/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,13 @@ func WithRetrievedClose(closeFunc CloseFunc) RetrievedOption {
}
}

// NewRetrievedFromMap returns a new Retrieved instance that contains a Map data.
// * conf the Map that will be merged to the given map in the MergeTo.
// * CloseFunc specifies a function to be invoked when the configuration for which it was
// used to retrieve values is no longer in use and should close and release any watchers
// that it may have created.
func NewRetrievedFromMap(conf *Conf, opts ...RetrievedOption) Retrieved {
// NewRetrieved returns a new Retrieved instance that contains the data from the raw deserialized config.
func NewRetrieved(rawConf map[string]interface{}, opts ...RetrievedOption) (Retrieved, error) {
set := retrievedSettings{}
for _, opt := range opts {
opt(&set)
}
return Retrieved{conf: conf, closeFunc: set.closeFunc}
return Retrieved{conf: NewFromStringMap(rawConf), closeFunc: set.closeFunc}, nil
}

// AsMap returns the retrieved configuration parsed as a Map.
Expand Down
1 change: 1 addition & 0 deletions confmap/provider/envprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func (emp *provider) Retrieve(_ context.Context, uri string, _ confmap.WatcherFu
if !strings.HasPrefix(uri, schemeName+":") {
return confmap.Retrieved{}, fmt.Errorf("%q uri is not supported by %q provider", uri, schemeName)
}

return internal.NewRetrievedFromYAML([]byte(os.Getenv(uri[len(schemeName)+1:])))
}

Expand Down
2 changes: 1 addition & 1 deletion confmap/provider/internal/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ func NewRetrievedFromYAML(yamlBytes []byte, opts ...confmap.RetrievedOption) (co
if err := yaml.Unmarshal(yamlBytes, &rawConf); err != nil {
return confmap.Retrieved{}, err
}
return confmap.NewRetrievedFromMap(confmap.NewFromStringMap(rawConf), opts...), nil
return confmap.NewRetrieved(rawConf, opts...)
}
16 changes: 8 additions & 8 deletions confmap/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ import (
"github.com/stretchr/testify/require"
)

func TestNewRetrievedFromMap(t *testing.T) {
conf := New()
ret := NewRetrievedFromMap(conf)
func TestNewRetrieved(t *testing.T) {
ret, err := NewRetrieved(nil)
require.NoError(t, err)
retMap, err := ret.AsMap()
require.NoError(t, err)
assert.Same(t, conf, retMap)
assert.Equal(t, New(), retMap)
assert.NoError(t, ret.Close(context.Background()))
}

func TestNewRetrievedFromMapWithOptions(t *testing.T) {
func TestNewRetrievedWithOptions(t *testing.T) {
want := errors.New("my error")
conf := New()
ret := NewRetrievedFromMap(conf, WithRetrievedClose(func(context.Context) error { return want }))
ret, err := NewRetrieved(nil, WithRetrievedClose(func(context.Context) error { return want }))
require.NoError(t, err)
retMap, err := ret.AsMap()
require.NoError(t, err)
assert.Same(t, conf, retMap)
assert.Equal(t, New(), retMap)
assert.Equal(t, want, ret.Close(context.Background()))
}
20 changes: 9 additions & 11 deletions confmap/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

type mockProvider struct {
scheme string
retC *Conf
retM map[string]interface{}
errR error
errS error
errW error
Expand All @@ -38,15 +38,13 @@ func (m *mockProvider) Retrieve(_ context.Context, _ string, watcher WatcherFunc
if m.errR != nil {
return Retrieved{}, m.errR
}
if m.retC == nil {
return NewRetrievedFromMap(New()), nil
if m.retM == nil {
return NewRetrieved(nil)
}
if watcher != nil {
watcher(&ChangeEvent{Error: m.errW})
}
return NewRetrievedFromMap(
m.retC,
WithRetrievedClose(func(ctx context.Context) error { return m.errC })), nil
return NewRetrieved(m.retM, WithRetrievedClose(func(ctx context.Context) error { return m.errC }))
}

func (m *mockProvider) Scheme() string {
Expand Down Expand Up @@ -120,7 +118,7 @@ func TestResolverErrors(t *testing.T) {
locations: []string{"mock:", "err:"},
providers: func() []Provider {
conf := newConfFromFile(t, filepath.Join("testdata", "config.yaml"))
return []Provider{&mockProvider{}, &mockProvider{scheme: "err", retC: conf, errW: errors.New("watch_err")}}
return []Provider{&mockProvider{}, &mockProvider{scheme: "err", retM: conf, errW: errors.New("watch_err")}}
}(),
expectWatchErr: true,
},
Expand All @@ -131,7 +129,7 @@ func TestResolverErrors(t *testing.T) {
conf := newConfFromFile(t, filepath.Join("testdata", "config.yaml"))
return []Provider{
&mockProvider{},
&mockProvider{scheme: "err", retC: conf, errC: errors.New("close_err")},
&mockProvider{scheme: "err", retM: conf, errC: errors.New("close_err")},
}
}(),
expectCloseErr: true,
Expand All @@ -143,7 +141,7 @@ func TestResolverErrors(t *testing.T) {
conf := newConfFromFile(t, filepath.Join("testdata", "config.yaml"))
return []Provider{
&mockProvider{},
&mockProvider{scheme: "err", retC: conf, errS: errors.New("close_err")},
&mockProvider{scheme: "err", retM: conf, errS: errors.New("close_err")},
}
}(),
expectShutdownErr: true,
Expand Down Expand Up @@ -232,7 +230,7 @@ func TestBackwardsCompatibilityForFilePath(t *testing.T) {

func TestResolver(t *testing.T) {
provider := func() Provider {
return &mockProvider{retC: newConfFromFile(t, filepath.Join("testdata", "config.yaml"))}
return &mockProvider{retM: newConfFromFile(t, filepath.Join("testdata", "config.yaml"))}
}()

resolver, err := NewResolver(ResolverSettings{URIs: []string{"mock:"}, Providers: makeMapProvidersMap(provider), Converters: nil})
Expand Down Expand Up @@ -267,7 +265,7 @@ func TestResolverNoProviders(t *testing.T) {

func TestResolverShutdownClosesWatch(t *testing.T) {
provider := func() Provider {
return &mockProvider{retC: newConfFromFile(t, filepath.Join("testdata", "config.yaml"))}
return &mockProvider{retM: newConfFromFile(t, filepath.Join("testdata", "config.yaml"))}
}()

resolver, err := NewResolver(ResolverSettings{URIs: []string{"mock:"}, Providers: makeMapProvidersMap(provider), Converters: nil})
Expand Down

0 comments on commit e0cf05e

Please sign in to comment.