Add Unset method

Before, we would set entries to nil to unset them
This commit is contained in:
Albert Vaca Cintora 2022-05-09 13:19:08 +02:00
parent da54697d46
commit 0b35ab4493

View file

@ -1269,6 +1269,22 @@ func (v *Viper) Set(key string, value interface{}) {
deepestMap[lastKey] = value deepestMap[lastKey] = value
} }
// Unset removes a value set with Set
// Unset is case-insensitive for a key.
// Values which come from flags, config file, ENV or default can't be unset.
func Unset(key string) { v.Unset(key) }
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])
// unset innermost value
delete(deepestMap, lastKey)
}
// ReadInConfig will discover and load the configuration file from disk // ReadInConfig will discover and load the configuration file from disk
// and key/value stores, searching in one of the defined paths. // and key/value stores, searching in one of the defined paths.
func ReadInConfig() error { return v.ReadInConfig() } func ReadInConfig() error { return v.ReadInConfig() }