mirror of
https://github.com/spf13/viper
synced 2024-12-22 11:37:02 +00:00
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
2 changed files with 20 additions and 2 deletions
11
viper.go
11
viper.go
|
@ -1416,9 +1416,16 @@ func (v *Viper) Unset(key string) {
|
||||||
|
|
||||||
path := strings.Split(key, v.keyDelim)
|
path := strings.Split(key, v.keyDelim)
|
||||||
lastKey := strings.ToLower(path[len(path)-1])
|
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
|
// 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"))
|
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) {
|
func TestDefaultPost(t *testing.T) {
|
||||||
assert.NotEqual(t, "NYC", Get("state"))
|
assert.NotEqual(t, "NYC", Get("state"))
|
||||||
SetDefault("state", "NYC")
|
SetDefault("state", "NYC")
|
||||||
|
|
Loading…
Reference in a new issue