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 1/4] 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() } From 244d9523789acf4920885204bd0b60b8860f9300 Mon Sep 17 00:00:00 2001 From: Pablo Alvarez de Sotomayor Posadillo Date: Tue, 26 Jun 2018 19:42:04 -0500 Subject: [PATCH 2/4] Fix a call to Unset. --- viper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/viper.go b/viper.go index d9974bc..95ca75b 100644 --- a/viper.go +++ b/viper.go @@ -1408,7 +1408,7 @@ func (v *Viper) Set(key string, value interface{}) { // Unset deletes a key from Viper. // Unset is case-insensitive for a key. -func Unset(key string) { v.Unset(key, value) } +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)) From 9956d044940deacec5e08dfbadf8e7eea8fdeb44 Mon Sep 17 00:00:00 2001 From: Pablo Alvarez de Sotomayor Posadillo Date: Tue, 26 Jun 2018 19:42:04 -0500 Subject: [PATCH 3/4] Fix a call to Unset. --- viper.go | 1 + 1 file changed, 1 insertion(+) diff --git a/viper.go b/viper.go index 95ca75b..3205b0f 100644 --- a/viper.go +++ b/viper.go @@ -1409,6 +1409,7 @@ func (v *Viper) Set(key string, value interface{}) { // Unset deletes a key from Viper. // Unset is case-insensitive for a key. 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)) From 6b0e9f4bbb75fa2d64041bd1d4331bfb7779ee36 Mon Sep 17 00:00:00 2001 From: Ritho Date: Sun, 21 Feb 2021 21:00:11 +0100 Subject: [PATCH 4/4] Improve unset to delete it from all the config maps and from the alias. Add a new test. --- viper.go | 11 +++++++++-- viper_test.go | 11 +++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/viper.go b/viper.go index 3205b0f..e8ec2a3 100644 --- a/viper.go +++ b/viper.go @@ -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 diff --git a/viper_test.go b/viper_test.go index 45bf8e9..da66d5c 100644 --- a/viper_test.go +++ b/viper_test.go @@ -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")