mirror of
https://github.com/spf13/viper
synced 2025-05-06 12:17:18 +00:00
Fixed: insensitiviseMaps and tests
All keys (even nested ones) are now lower-cased recursively. On the way, map[interface{}]interface{} are cast to map[string]interface{}
This commit is contained in:
parent
c14ce6d43c
commit
93f1be46b4
2 changed files with 18 additions and 10 deletions
18
util.go
18
util.go
|
@ -41,15 +41,23 @@ func (pe ConfigParseError) Error() string {
|
|||
|
||||
func insensitiviseMap(m map[string]interface{}) {
|
||||
for key, val := range m {
|
||||
switch val.(type) {
|
||||
case map[interface{}]interface{}:
|
||||
// nested map: cast and recursively insensitivise
|
||||
val = cast.ToStringMap(val)
|
||||
insensitiviseMap(val.(map[string]interface{}))
|
||||
case map[string]interface{}:
|
||||
// nested map: recursively insensitivise
|
||||
insensitiviseMap(val.(map[string]interface{}))
|
||||
}
|
||||
|
||||
lower := strings.ToLower(key)
|
||||
if key != lower {
|
||||
// remove old key (not lower-cased)
|
||||
delete(m, key)
|
||||
m[lower] = val
|
||||
if m2, ok := val.(map[string]interface{}); ok {
|
||||
// nested map: recursively insensitivise
|
||||
insensitiviseMap(m2)
|
||||
}
|
||||
}
|
||||
// update map
|
||||
m[lower] = val
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -269,7 +269,7 @@ func TestUnmarshalling(t *testing.T) {
|
|||
assert.False(t, InConfig("state"))
|
||||
assert.Equal(t, "steve", Get("name"))
|
||||
assert.Equal(t, []interface{}{"skateboarding", "snowboarding", "go"}, Get("hobbies"))
|
||||
assert.Equal(t, map[string]interface{}{"jacket": "leather", "trousers": "denim", "pants": map[interface{}]interface{}{"size": "large"}}, Get("clothing"))
|
||||
assert.Equal(t, map[string]interface{}{"jacket": "leather", "trousers": "denim", "pants": map[string]interface{}{"size": "large"}}, Get("clothing"))
|
||||
assert.Equal(t, 35, Get("age"))
|
||||
}
|
||||
|
||||
|
@ -637,10 +637,10 @@ func TestFindsNestedKeys(t *testing.T) {
|
|||
"age": 35,
|
||||
"owner": map[string]interface{}{
|
||||
"organization": "MongoDB",
|
||||
"Bio": "MongoDB Chief Developer Advocate & Hacker at Large",
|
||||
"bio": "MongoDB Chief Developer Advocate & Hacker at Large",
|
||||
"dob": dob,
|
||||
},
|
||||
"owner.Bio": "MongoDB Chief Developer Advocate & Hacker at Large",
|
||||
"owner.bio": "MongoDB Chief Developer Advocate & Hacker at Large",
|
||||
"type": "donut",
|
||||
"id": "0001",
|
||||
"name": "Cake",
|
||||
|
@ -649,7 +649,7 @@ func TestFindsNestedKeys(t *testing.T) {
|
|||
"clothing": map[string]interface{}{
|
||||
"jacket": "leather",
|
||||
"trousers": "denim",
|
||||
"pants": map[interface{}]interface{}{
|
||||
"pants": map[string]interface{}{
|
||||
"size": "large",
|
||||
},
|
||||
},
|
||||
|
@ -695,7 +695,7 @@ func TestReadBufConfig(t *testing.T) {
|
|||
assert.False(t, v.InConfig("state"))
|
||||
assert.Equal(t, "steve", v.Get("name"))
|
||||
assert.Equal(t, []interface{}{"skateboarding", "snowboarding", "go"}, v.Get("hobbies"))
|
||||
assert.Equal(t, map[string]interface{}{"jacket": "leather", "trousers": "denim", "pants": map[interface{}]interface{}{"size": "large"}}, v.Get("clothing"))
|
||||
assert.Equal(t, map[string]interface{}{"jacket": "leather", "trousers": "denim", "pants": map[string]interface{}{"size": "large"}}, v.Get("clothing"))
|
||||
assert.Equal(t, 35, v.Get("age"))
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue