mirror of https://github.com/spf13/viper.git
Improve unset to delete it from all the config maps and from the alias. Add a new test.
This commit is contained in:
parent
9956d04494
commit
6b0e9f4bbb
11
viper.go
11
viper.go
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue