mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
Add a new Unset function to remove a key from viper.
In some circunstances, you want to remove a key for your config in viper, so, instead of setting the key value to nil, it's better to have a new Unset function to remove the key from the inner map.
This commit is contained in:
parent
4613c4a95f
commit
55f6336645
1 changed files with 14 additions and 0 deletions
14
viper.go
14
viper.go
|
@ -1406,6 +1406,20 @@ func (v *Viper) Set(key string, value interface{}) {
|
|||
deepestMap[lastKey] = value
|
||||
}
|
||||
|
||||
// Unset deletes a key from Viper.
|
||||
// Unset is case-insensitive for a key.
|
||||
func Unset(key string) { v.Unset(key, value) }
|
||||
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])
|
||||
deepestMap := deepSearch(v.override, path[0:len(path)-1])
|
||||
|
||||
delete(deepestMap, lastKey)
|
||||
}
|
||||
|
||||
// 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() }
|
||||
|
|
Loading…
Reference in a new issue