Skip to content

Commit

Permalink
Add a root node string
Browse files Browse the repository at this point in the history
Remove a confusing interface for getting the root node.
  • Loading branch information
Alexandr Samylkin committed Dec 23, 2016
1 parent d100262 commit 6584091
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 11 deletions.
6 changes: 3 additions & 3 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func TestNestedStructs(t *testing.T) {

str := &root{}

v := provider.Get("")
v := provider.Get(Root)

assert.True(t, v.HasValue())
v.PopulateStruct(str)
Expand All @@ -238,7 +238,7 @@ func TestArrayOfStructs(t *testing.T) {

target := &arrayOfStructs{}

v := provider.Get("")
v := provider.Get(Root)

assert.True(t, v.HasValue())
assert.NoError(t, v.PopulateStruct(target))
Expand All @@ -253,7 +253,7 @@ func TestDefault(t *testing.T) {
NewEnvProvider(defaultEnvPrefix, mapEnvironmentProvider{values: env}),
)
target := &nested{}
v := provider.Get("")
v := provider.Get(Root)
assert.True(t, v.HasValue())
assert.NoError(t, v.PopulateStruct(target))
assert.Equal(t, "default_name", target.Name)
Expand Down
3 changes: 3 additions & 0 deletions config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import "fmt"
// ConfigurationChangeCallback is called for updates of configuration data
type ConfigurationChangeCallback func(key string, provider string, configdata interface{})

// Root marks the root node in a Provider
const Root = ""

// A Provider provides a unified interface to accessing
// configuration systems.
type Provider interface {
Expand Down
4 changes: 2 additions & 2 deletions config/provider_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func TestProviderGroup(t *testing.T) {
assert.Equal(t, "test-group", pg.Name())
assert.Equal(t, "test", pg.Get("id").AsString())
// TODO this should not require a cast GFM-74
assert.Empty(t, pg.(providerGroup).RegisterChangeCallback("", nil))
assert.Nil(t, pg.(providerGroup).UnregisterChangeCallback(""))
assert.Empty(t, pg.(providerGroup).RegisterChangeCallback(Root, nil))
assert.Nil(t, pg.(providerGroup).UnregisterChangeCallback(Root))
}

func TestProviderGroupScope(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions config/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestYamlStructRoot(t *testing.T) {

cs := &configStruct{}

assert.NoError(t, provider.Get("").PopulateStruct(cs))
assert.NoError(t, provider.Get(Root).PopulateStruct(cs))

assert.Equal(t, "keyvalue", cs.AppID)
assert.Equal(t, "owner@service.com", cs.Owner)
Expand Down Expand Up @@ -95,8 +95,8 @@ func TestNewYamlProviderFromReader(t *testing.T) {
buff := bytes.NewBuffer([]byte(yamlConfig1))
provider := NewYamlProviderFromReader(ioutil.NopCloser(buff))
cs := &configStruct{}
assert.NoError(t, provider.Get("").PopulateStruct(cs))
assert.Equal(t, "yaml", provider.Scope("").Name())
assert.NoError(t, provider.Get(Root).PopulateStruct(cs))
assert.Equal(t, "yaml", provider.Scope(Root).Name())
assert.Equal(t, "keyvalue", cs.AppID)
assert.Equal(t, "owner@service.com", cs.Owner)
}
Expand Down Expand Up @@ -234,7 +234,7 @@ func TestParsingUnparsableDuration(t *testing.T) {
func TestTypeOfTypes(t *testing.T) {
withYamlBytes(t, typeStructYaml, func(provider Provider) {
tts := typeStructStruct{}
err := provider.Get("").PopulateStruct(&tts)
err := provider.Get(Root).PopulateStruct(&tts)
assert.NoError(t, err)
assert.Equal(t, userDefinedTypeInt(123), *tts.TypeStruct.TestInt)
assert.Equal(t, userDefinedTypeUInt(456), *tts.TypeStruct.TestUInt)
Expand Down
1 change: 0 additions & 1 deletion service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func New(options ...Option) (Owner, error) {
svc.setupAuthClient(svc.configProvider)

// load standard config
// TODO(glib): `.Get("")` is a confusing interface for getting the root config node
if err := svc.setupStandardConfig(svc.Config()); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion service/service_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (svc *serviceCore) setupLogging(cfg config.Provider) {
}

func (svc *serviceCore) setupStandardConfig(cfg config.Provider) error {
if err := cfg.Get("").PopulateStruct(&svc.standardConfig); err != nil {
if err := cfg.Get(config.Root).PopulateStruct(&svc.standardConfig); err != nil {
return errors.Wrap(err, "unable to load standard configuration")
}

Expand Down

0 comments on commit 6584091

Please sign in to comment.