mirror of
https://github.com/spf13/viper
synced 2025-05-06 04:07:17 +00:00
Make AllSettings include set-but-empty keys
This commit is contained in:
parent
1cc3fa065c
commit
42406af567
2 changed files with 32 additions and 2 deletions
4
viper.go
4
viper.go
|
@ -1825,8 +1825,8 @@ func (v *Viper) AllSettings() map[string]interface{} {
|
|||
for _, k := range v.AllKeys() {
|
||||
value := v.Get(k)
|
||||
if value == nil {
|
||||
// should not happen, since AllKeys() returns only keys holding a value,
|
||||
// check just in case anything changes
|
||||
// Key set but empty, include it in the output as a null value
|
||||
m[k] = nil
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
|
@ -1818,3 +1818,33 @@ func TestKnownKeys(t *testing.T) {
|
|||
t.Error("SetKnown didn't mark key as known")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptySection(t *testing.T) {
|
||||
|
||||
var yamlWithEnvVars = `
|
||||
key:
|
||||
subkey:
|
||||
another_key:
|
||||
`
|
||||
|
||||
v := New()
|
||||
initConfig(v, "yaml", yamlWithEnvVars)
|
||||
v.SetKnown("is_known")
|
||||
v.SetDefault("has_default", true)
|
||||
|
||||
// AllKeys includes empty keys
|
||||
keys := v.AllKeys()
|
||||
assert.NotContains(t, keys, "key") // Only empty leaf nodes are returned
|
||||
assert.Contains(t, keys, "key.subkey")
|
||||
assert.Contains(t, keys, "another_key")
|
||||
assert.NotContains(t, keys, "is_known")
|
||||
assert.Contains(t, keys, "has_default")
|
||||
|
||||
// AllSettings includes empty keys
|
||||
vars := v.AllSettings()
|
||||
assert.NotContains(t, vars, "key") // Only empty leaf nodes are returned
|
||||
assert.Contains(t, vars, "key.subkey")
|
||||
assert.Contains(t, vars, "another_key")
|
||||
assert.NotContains(t, vars, "is_known")
|
||||
assert.Contains(t, vars, "has_default")
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue