mirror of
https://github.com/spf13/viper
synced 2024-12-22 19:47:01 +00:00
added allowEmptyMap option
This commit is contained in:
parent
419fd86e49
commit
bd4ecaf77c
2 changed files with 34 additions and 1 deletions
23
viper.go
23
viper.go
|
@ -205,6 +205,7 @@ type Viper struct {
|
||||||
automaticEnvApplied bool
|
automaticEnvApplied bool
|
||||||
envKeyReplacer StringReplacer
|
envKeyReplacer StringReplacer
|
||||||
allowEmptyEnv bool
|
allowEmptyEnv bool
|
||||||
|
allowEmptyMap bool
|
||||||
|
|
||||||
config map[string]interface{}
|
config map[string]interface{}
|
||||||
override map[string]interface{}
|
override map[string]interface{}
|
||||||
|
@ -535,6 +536,15 @@ func (v *Viper) AllowEmptyEnv(allowEmptyEnv bool) {
|
||||||
v.allowEmptyEnv = allowEmptyEnv
|
v.allowEmptyEnv = allowEmptyEnv
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AllowEmptyMap tells Viper to consider set,
|
||||||
|
// but empty maps as valid values instead of falling back.
|
||||||
|
// For backward compatibility reasons this is false by default.
|
||||||
|
func AllowEmptyMap(allowEmptyMap bool) { v.AllowEmptyMap(allowEmptyMap) }
|
||||||
|
|
||||||
|
func (v *Viper) AllowEmptyMap(allowEmptyMap bool) {
|
||||||
|
v.allowEmptyMap = allowEmptyMap
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: should getEnv logic be moved into find(). Can generalize the use of
|
// TODO: should getEnv logic be moved into find(). Can generalize the use of
|
||||||
// rewriting keys many things, Ex: Get('someKey') -> some_key
|
// rewriting keys many things, Ex: Get('someKey') -> some_key
|
||||||
// (camel case to snake case for JSON keys perhaps)
|
// (camel case to snake case for JSON keys perhaps)
|
||||||
|
@ -1550,6 +1560,8 @@ func (v *Viper) ReadInConfig() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fmt.Println(config)
|
||||||
|
|
||||||
v.config = config
|
v.config = config
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -1582,7 +1594,9 @@ func ReadConfig(in io.Reader) error { return v.ReadConfig(in) }
|
||||||
|
|
||||||
func (v *Viper) ReadConfig(in io.Reader) error {
|
func (v *Viper) ReadConfig(in io.Reader) error {
|
||||||
v.config = make(map[string]interface{})
|
v.config = make(map[string]interface{})
|
||||||
return v.unmarshalReader(in, v.config)
|
err := v.unmarshalReader(in, v.config)
|
||||||
|
fmt.Println(v.config)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// MergeConfig merges a new configuration with an existing config.
|
// MergeConfig merges a new configuration with an existing config.
|
||||||
|
@ -2011,6 +2025,13 @@ func (v *Viper) flattenAndMergeMap(shadow map[string]bool, m map[string]interfac
|
||||||
shadow[strings.ToLower(fullKey)] = true
|
shadow[strings.ToLower(fullKey)] = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v.allowEmptyMap {
|
||||||
|
if len(val.(map[string]interface{})) == 0 {
|
||||||
|
shadow[strings.ToLower(fullKey)] = true
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
// recursively merge to shadow map
|
// recursively merge to shadow map
|
||||||
shadow = v.flattenAndMergeMap(shadow, m2, fullKey)
|
shadow = v.flattenAndMergeMap(shadow, m2, fullKey)
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,6 +60,8 @@ type testUnmarshalExtra struct {
|
||||||
var tomlExample = []byte(`
|
var tomlExample = []byte(`
|
||||||
title = "TOML Example"
|
title = "TOML Example"
|
||||||
|
|
||||||
|
[ecs.test]
|
||||||
|
|
||||||
[owner]
|
[owner]
|
||||||
organization = "MongoDB"
|
organization = "MongoDB"
|
||||||
Bio = "MongoDB Chief Developer Advocate & Hacker at Large"
|
Bio = "MongoDB Chief Developer Advocate & Hacker at Large"
|
||||||
|
@ -669,6 +671,16 @@ func TestEmptyEnv_Allowed(t *testing.T) {
|
||||||
assert.Equal(t, "Cake", Get("name"))
|
assert.Equal(t, "Cake", Get("name"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestEmptyMap_Allowed(t *testing.T) {
|
||||||
|
initTOML()
|
||||||
|
AllowEmptyMap(true)
|
||||||
|
|
||||||
|
allkeys := sort.StringSlice(AllKeys())
|
||||||
|
allkeys.Sort()
|
||||||
|
|
||||||
|
assert.Equal(t, sort.StringSlice(sort.StringSlice{"ecs.test", "owner.bio", "owner.dob", "owner.organization", "title"}), allkeys)
|
||||||
|
}
|
||||||
|
|
||||||
func TestEnvPrefix(t *testing.T) {
|
func TestEnvPrefix(t *testing.T) {
|
||||||
initJSON()
|
initJSON()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue