Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recurse into arrays when converting keys to lowercase #1387

Merged
merged 1 commit into from Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 23 additions & 10 deletions util.go
Expand Up @@ -64,18 +64,25 @@ func copyAndInsensitiviseMap(m map[string]interface{}) map[string]interface{} {
return nm
}

func insensitiviseVal(val interface{}) interface{} {
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{}))
case []interface{}:
// nested array: recursively insensitivise
insensitiveArray(val.([]interface{}))
}
return val
}

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{}))
}

val = insensitiviseVal(val)
lower := strings.ToLower(key)
if key != lower {
// remove old key (not lower-cased)
Expand All @@ -86,6 +93,12 @@ func insensitiviseMap(m map[string]interface{}) {
}
}

func insensitiveArray(a []interface{}) {
for i, val := range a {
a[i] = insensitiviseVal(val)
}
}

func absPathify(logger Logger, inPath string) string {
logger.Info("trying to resolve absolute path", "path", inPath)

Expand Down
11 changes: 9 additions & 2 deletions viper_test.go
Expand Up @@ -2516,7 +2516,10 @@ func TestKeyDelimiter(t *testing.T) {
}

var yamlDeepNestedSlices = []byte(`TV:
- title: "The expanse"
- title: "The Expanse"
title_i18n:
USA: "The Expanse"
Japan: "エクスパンス -巨獣めざめる-"
seasons:
- first_released: "December 14, 2015"
episodes:
Expand Down Expand Up @@ -2546,11 +2549,15 @@ func TestSliceIndexAccess(t *testing.T) {
err := v.unmarshalReader(r, v.config)
require.NoError(t, err)

assert.Equal(t, "The expanse", v.GetString("tv.0.title"))
assert.Equal(t, "The Expanse", v.GetString("tv.0.title"))
assert.Equal(t, "February 1, 2017", v.GetString("tv.0.seasons.1.first_released"))
assert.Equal(t, "Static", v.GetString("tv.0.seasons.1.episodes.2.title"))
assert.Equal(t, "December 15, 2015", v.GetString("tv.0.seasons.0.episodes.1.air_date"))

// Test nested keys with capital letters
assert.Equal(t, "The Expanse", v.GetString("tv.0.title_i18n.USA"))
assert.Equal(t, "エクスパンス -巨獣めざめる-", v.GetString("tv.0.title_i18n.Japan"))

// Test for index out of bounds
assert.Equal(t, "", v.GetString("tv.0.seasons.2.first_released"))

Expand Down