From 436e036a7632913c08ea8d85caa6e382e5a7929d Mon Sep 17 00:00:00 2001 From: Pierre Gimalac Date: Thu, 7 Mar 2024 15:53:11 +0100 Subject: [PATCH] feat: add IsKnown function on viper config --- viper.go | 8 ++++++++ viper_test.go | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/viper.go b/viper.go index c106c53..b77e682 100644 --- a/viper.go +++ b/viper.go @@ -1265,6 +1265,14 @@ func (v *Viper) GetKnownKeys() map[string]interface{} { return ret } +// IsKnown returns whether the given key has been set as a known key +func IsKnown(key string) bool { return v.IsKnown(key) } +func (v *Viper) IsKnown(key string) bool { + key = strings.ToLower(key) + _, exists := v.knownKeys[key] + return exists +} + // Set sets the value for the key in the override register. // Set is case-insensitive for a key. // Will be used instead of values obtained via diff --git a/viper_test.go b/viper_test.go index 2016561..5507a6d 100644 --- a/viper_test.go +++ b/viper_test.go @@ -1879,3 +1879,18 @@ func TestKnownKeys(t *testing.T) { t.Error("SetKnown didn't mark key as known") } } + +func TestIsKnown(t *testing.T) { + v := New() + + v.SetDefault("default", 45) + assert.True(t, v.IsKnown("default")) + + v.SetKnown("known") + assert.True(t, v.IsKnown("known")) + + v.SetKnown("UpperKnown") + assert.True(t, v.IsKnown("UpperKnown")) + + assert.False(t, v.IsKnown("unknown")) +}