Skip to content

Commit

Permalink
Add nop config provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandr Samylkin committed Mar 14, 2017
1 parent 3303fa8 commit 0980a75
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,16 @@ func TestEnvProviderWithEmptyPrefix(t *testing.T) {
scope := emptyScope.Get("key")
require.Equal(t, "value", scope.Get("").AsString())
}

func TestNopProvider_Get(t *testing.T) {
t.Parallel()
p := NopProvider{}
assert.Equal(t, "NopProvider", p.Name())
assert.NoError(t, p.RegisterChangeCallback("key", nil))
assert.NoError(t, p.UnregisterChangeCallback("token"))

v := p.Get("randomKey")
assert.Equal(t, "NopProvider", v.Source())
assert.True(t, v.HasValue())
assert.Nil(t, v.Value())
}
49 changes: 49 additions & 0 deletions config/nop_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package config

import "time"

// NopProvider is an implementation of config provider that does nothing.
// It should be used for testing purposes only.
type NopProvider struct{}

var _ Provider = (*NopProvider)(nil)

// Name is NopProvider
func (p NopProvider) Name() string {
return "NopProvider"
}

// Get returns an invalid Value.
func (p NopProvider) Get(key string) Value {
return NewValue(p, key, nil, true, Invalid, &time.Time{})
}

// RegisterChangeCallback does nothing and simply returns nil.
func (p NopProvider) RegisterChangeCallback(key string, callback ChangeCallback) error {
return nil
}

// UnregisterChangeCallback does nothing and simply returns nil.
func (p NopProvider) UnregisterChangeCallback(token string) error {
return nil
}

0 comments on commit 0980a75

Please sign in to comment.