mirror of
https://github.com/spf13/viper
synced 2025-05-06 20:27:17 +00:00
feat: add IsKnown function on viper config
This commit is contained in:
parent
1d9194ffab
commit
436e036a76
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
|
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 sets the value for the key in the override register.
|
||||||
// Set is case-insensitive for a key.
|
// Set is case-insensitive for a key.
|
||||||
// Will be used instead of values obtained via
|
// 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")
|
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