Skip to content

Commit

Permalink
Remove unused function
Browse files Browse the repository at this point in the history
Extract common prefix function
Fix case of empty key for the scoped provider
Update prefix instead of recursing
  • Loading branch information
Alexandr Samylkin committed Jan 19, 2017
1 parent 96fa6e3 commit c8840b2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
48 changes: 23 additions & 25 deletions config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

package config

import "fmt"

// ConfigurationChangeCallback is called for updates of configuration data
type ConfigurationChangeCallback func(key string, provider string, configdata interface{})

Expand All @@ -43,10 +41,6 @@ type Provider interface {
UnregisterChangeCallback(token string) error
}

func keyNotFound(key string) error {
return fmt.Errorf("couldn't find key %q", key)
}

// ScopedProvider defines recursive interface of providers based on the prefix
type ScopedProvider struct {
Provider
Expand All @@ -55,38 +49,42 @@ type ScopedProvider struct {
}

// NewScopedProvider creates a child provider given a prefix
func NewScopedProvider(prefix string, provider Provider) Provider {
func NewScopedProvider(prefix string, provider Provider) *ScopedProvider {
return &ScopedProvider{provider, prefix}
}

// Get returns configuration value
func (sp ScopedProvider) Get(key string) Value {
if sp.prefix != "" {
key = sp.prefix + "." + key
func addPrefix(prefix, key string) string {
if prefix == "" {
return key
}

return sp.Provider.Get(key)
if key == "" {
return prefix
}

return prefix + "." + key
}

// Get returns configuration value
func (sp ScopedProvider) Get(key string) Value {
return sp.Provider.Get(addPrefix(sp.prefix, key))
}

// Scope returns new scoped provider, given a prefix
func (sp ScopedProvider) Scope(prefix string) Provider {
return NewScopedProvider(prefix, sp)
if prefix == "" {
return sp
}

return NewScopedProvider(addPrefix(sp.prefix, prefix), sp.Provider)
}

// RegisterChangeCallback registers the callback in the underling provider
// RegisterChangeCallback registers the callback in the underlying provider
func (sp ScopedProvider) RegisterChangeCallback(key string, callback ConfigurationChangeCallback) error {
if sp.prefix != "" {
key = sp.prefix + "." + key
}

return sp.Provider.RegisterChangeCallback(key, callback)
return sp.Provider.RegisterChangeCallback(addPrefix(sp.prefix, key), callback)
}

// UnregisterChangeCallback un registers a callback in the underling provider
// UnregisterChangeCallback un registers a callback in the underlying provider
func (sp ScopedProvider) UnregisterChangeCallback(key string) error {
if sp.prefix != "" {
key = sp.prefix + "." + key
}

return sp.Provider.UnregisterChangeCallback(key)
return sp.Provider.UnregisterChangeCallback(addPrefix(sp.prefix, key))
}
25 changes: 25 additions & 0 deletions config/provider_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
)

func TestProviderGroup(t *testing.T) {
t.Parallel()
pg := NewProviderGroup("test-group", NewYAMLProviderFromBytes([]byte(`id: test`)))
assert.Equal(t, "test-group", pg.Name())
assert.Equal(t, "test", pg.Get("id").AsString())
Expand All @@ -38,12 +39,14 @@ func TestProviderGroup(t *testing.T) {
}

func TestProviderGroupScope(t *testing.T) {
t.Parallel()
data := map[string]interface{}{"hello.world": 42}
pg := NewProviderGroup("test-group", NewStaticProvider(data))
assert.Equal(t, 42, pg.Scope("hello").Get("world").AsInt())
}

func TestCallbacks_WithDynamicProvider(t *testing.T) {
t.Parallel()
data := map[string]interface{}{"hello.world": 42}
mock := NewProviderGroup("with-dynamic", NewStaticProvider(data))
mock = mock.(providerGroup).WithProvider(newMockDynamicProvider(data))
Expand All @@ -60,6 +63,7 @@ func TestCallbacks_WithDynamicProvider(t *testing.T) {
}

func TestCallbacks_WithoutDynamicProvider(t *testing.T) {
t.Parallel()
data := map[string]interface{}{"hello.world": 42}
mock := NewProviderGroup("with-dynamic", NewStaticProvider(data))
mock = mock.(providerGroup).WithProvider(NewStaticProvider(data))
Expand All @@ -69,6 +73,7 @@ func TestCallbacks_WithoutDynamicProvider(t *testing.T) {
}

func TestCallbacks_WithScopedProvider(t *testing.T) {
t.Parallel()
mock := &mockDynamicProvider{}
mock.Set("uber.fx", "go-lang")
scope := NewScopedProvider("uber", mock)
Expand All @@ -91,7 +96,27 @@ func TestCallbacks_WithScopedProvider(t *testing.T) {
val = scope.Get("fx").AsString()
require.Equal(t, "unregister works too!", val)
assert.Equal(t, 1, callCount)
}

func TestScope_WithScopedProvider(t *testing.T) {
t.Parallel()
mock := &mockDynamicProvider{}
mock.Set("uber.fx", "go-lang")
scope := NewScopedProvider("", mock)
require.Equal(t, "go-lang", scope.Get("uber.fx").AsString())
require.False(t, scope.Get("uber").HasValue())

base := scope.Scope("uber")
require.Equal(t, "go-lang", base.Get("fx").AsString())
require.False(t, base.Get("").HasValue())

uber := base.Scope("")
require.Equal(t, "go-lang", uber.Get("fx").AsString())
require.False(t, uber.Get("").HasValue())

fx := uber.Scope("fx")
require.Equal(t, "go-lang", fx.Get("").AsString())
require.False(t, fx.Get("fx").HasValue())
}

type mockDynamicProvider struct {
Expand Down

0 comments on commit c8840b2

Please sign in to comment.