Improve unset to delete it from all the config maps and from the alias. Add a new test.

This commit is contained in:
Ritho 2021-02-21 21:00:11 +01:00
parent 9956d04494
commit 6b0e9f4bbb
No known key found for this signature in database
GPG key ID: ABCB521E2495CC69
2 changed files with 20 additions and 2 deletions

View file

@ -1416,9 +1416,16 @@ func (v *Viper) Unset(key string) {
path := strings.Split(key, v.keyDelim)
lastKey := strings.ToLower(path[len(path)-1])
deepestMap := deepSearch(v.override, path[0:len(path)-1])
delete(deepestMap, lastKey)
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

View file

@ -406,6 +406,17 @@ func TestOverrides(t *testing.T) {
assert.Equal(t, 40, 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) {
assert.NotEqual(t, "NYC", Get("state"))
SetDefault("state", "NYC")