Skip to content

Commit

Permalink
Improve unset to delete it from all the config maps and from the alia…
Browse files Browse the repository at this point in the history
…s. Add a new test.
  • Loading branch information
i02sopop committed Feb 21, 2021
1 parent 9956d04 commit 6b0e9f4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
11 changes: 9 additions & 2 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions viper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 6b0e9f4

Please sign in to comment.