mirror of
https://github.com/spf13/viper
synced 2025-05-05 19:57:18 +00:00
Merge pull request #31 from DataDog/pgimalac/is-known
This commit is contained in:
commit
3e7837fd38
2 changed files with 23 additions and 0 deletions
8
viper.go
8
viper.go
|
@ -1265,6 +1265,14 @@ func (v *Viper) GetKnownKeys() map[string]interface{} {
|
|||
return ret
|
||||
}
|
||||
|
||||
// IsKnown returns whether the given key has been set as a known key
|
||||
func IsKnown(key string) bool { return v.IsKnown(key) }
|
||||
func (v *Viper) IsKnown(key string) bool {
|
||||
key = strings.ToLower(key)
|
||||
_, exists := v.knownKeys[key]
|
||||
return exists
|
||||
}
|
||||
|
||||
// Set sets the value for the key in the override register.
|
||||
// Set is case-insensitive for a key.
|
||||
// Will be used instead of values obtained via
|
||||
|
|
|
@ -1879,3 +1879,18 @@ func TestKnownKeys(t *testing.T) {
|
|||
t.Error("SetKnown didn't mark key as known")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsKnown(t *testing.T) {
|
||||
v := New()
|
||||
|
||||
v.SetDefault("default", 45)
|
||||
assert.True(t, v.IsKnown("default"))
|
||||
|
||||
v.SetKnown("known")
|
||||
assert.True(t, v.IsKnown("known"))
|
||||
|
||||
v.SetKnown("UpperKnown")
|
||||
assert.True(t, v.IsKnown("UpperKnown"))
|
||||
|
||||
assert.False(t, v.IsKnown("unknown"))
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue