Skip to content

Commit

Permalink
Add IsExplicit function to check for non-default.
Browse files Browse the repository at this point in the history
The IsSet function returns true even if the value that's available comes
from the default. Rather than changing the behavior of IsSet, adding a
new IsExplicit function allows checking for non-default values without
breaking existing behavior. This is intended to address issue spf13#276 among
other things.
  • Loading branch information
josephtfrank committed Feb 26, 2019
1 parent d104d25 commit 2ecf3ac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
20 changes: 17 additions & 3 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ func GetViper() *Viper {
func Get(key string) interface{} { return v.Get(key) }
func (v *Viper) Get(key string) interface{} {
lcaseKey := strings.ToLower(key)
val := v.find(lcaseKey)
val := v.find(lcaseKey, true)
if val == nil {
return nil
}
Expand Down Expand Up @@ -951,7 +951,7 @@ func (v *Viper) BindEnv(input ...string) error {
// flag, env, config file, key/value store, default.
// Viper will check to see if an alias exists first.
// Note: this assumes a lower-cased key given.
func (v *Viper) find(lcaseKey string) interface{} {
func (v *Viper) find(lcaseKey string, useDefault bool) interface{} {

var (
val interface{}
Expand Down Expand Up @@ -1039,6 +1039,11 @@ func (v *Viper) find(lcaseKey string) interface{} {
return nil
}

// Only use default values if asked
if !useDefault {
return nil
}

// Default next
val = v.searchMap(v.defaults, path)
if val != nil {
Expand Down Expand Up @@ -1084,7 +1089,16 @@ func readAsCSV(val string) ([]string, error) {
func IsSet(key string) bool { return v.IsSet(key) }
func (v *Viper) IsSet(key string) bool {
lcaseKey := strings.ToLower(key)
val := v.find(lcaseKey)
val := v.find(lcaseKey, true)
return val != nil
}

// IsExplicit checks to see if the key has a non-default value set.
// IsExplicit is case-insensitive for a key.
func IsExplicit(key string) bool { return v.IsExplicit(key) }
func (v *Viper) IsExplicit(key string) bool {
lcaseKey := strings.ToLower(key)
val := v.find(lcaseKey, false)
return val != nil
}

Expand Down
15 changes: 15 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,21 @@ func TestIsSet(t *testing.T) {
assert.False(t, v.IsSet("helloworld"))
v.Set("helloworld", "fubar")
assert.True(t, v.IsSet("helloworld"))
v.SetDefault("default", "value")
assert.True(t, v.IsSet("default"))
}

func TestIsExplicit(t *testing.T) {
v := New()
v.SetConfigType("yaml")
v.ReadConfig(bytes.NewBuffer(yamlExample))
assert.True(t, v.IsExplicit("clothing.jacket"))
assert.False(t, v.IsExplicit("clothing.jackets"))
assert.False(t, v.IsExplicit("helloworld"))
v.Set("helloworld", "fubar")
assert.True(t, v.IsExplicit("helloworld"))
v.SetDefault("default", "value")
assert.False(t, v.IsExplicit("default"))
}

func TestDirsSearch(t *testing.T) {
Expand Down

0 comments on commit 2ecf3ac

Please sign in to comment.