mirror of
https://github.com/spf13/viper
synced 2025-04-28 16:27:17 +00:00
Merge 6b0e9f4bbb
into 1508a7ba44
This commit is contained in:
commit
3c382647a4
2 changed files with 33 additions and 0 deletions
22
viper.go
22
viper.go
|
@ -1476,6 +1476,28 @@ func (v *Viper) Set(key string, value any) {
|
|||
deepestMap[lastKey] = value
|
||||
}
|
||||
|
||||
// Unset deletes a key from Viper.
|
||||
// Unset is case-insensitive for a key.
|
||||
func Unset(key string) { v.Unset(key) }
|
||||
|
||||
func (v *Viper) Unset(key string) {
|
||||
// If alias passed in, then set the proper override
|
||||
key = v.realKey(strings.ToLower(key))
|
||||
|
||||
path := strings.Split(key, v.keyDelim)
|
||||
lastKey := strings.ToLower(path[len(path)-1])
|
||||
|
||||
for _, cfgMap := range []map[string]interface{}{
|
||||
v.override, v.config, v.defaults,
|
||||
v.kvstore,
|
||||
} {
|
||||
cfg := deepSearch(cfgMap, path[0:len(path)-1])
|
||||
delete(cfg, lastKey)
|
||||
}
|
||||
|
||||
delete(v.aliases, key)
|
||||
}
|
||||
|
||||
// ReadInConfig will discover and load the configuration file from disk
|
||||
// and key/value stores, searching in one of the defined paths.
|
||||
func ReadInConfig() error { return v.ReadInConfig() }
|
||||
|
|
|
@ -513,6 +513,17 @@ func TestOverrides(t *testing.T) {
|
|||
assert.Equal(t, 40, v.Get("age"))
|
||||
}
|
||||
|
||||
func TestUnset(t *testing.T) {
|
||||
SetDefault("unset", 20)
|
||||
Set("unset", 10)
|
||||
RegisterAlias("unset_alias", "unset")
|
||||
|
||||
Unset("unset")
|
||||
|
||||
assert.Equal(t, nil, Get("unset"))
|
||||
assert.Equal(t, nil, Get("unset_alias"))
|
||||
}
|
||||
|
||||
func TestDefaultPost(t *testing.T) {
|
||||
v := New()
|
||||
assert.NotEqual(t, "NYC", v.Get("state"))
|
||||
|
|
Loading…
Add table
Reference in a new issue