From 55f6336645b3ef0cc16c3cd3df7981d0aa85fb75 Mon Sep 17 00:00:00 2001 From: Pablo Alvarez de Sotomayor Posadillo Date: Tue, 26 Jun 2018 18:26:33 -0500 Subject: [PATCH] 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. --- viper.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/viper.go b/viper.go index 7027715..d9974bc 100644 --- a/viper.go +++ b/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() }