Skip to content

Commit

Permalink
Fix incorrect map unflatten behaviour on no delim. Closes #275.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Apr 3, 2024
1 parent 6b3f67c commit 14948fa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
8 changes: 7 additions & 1 deletion maps/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,16 @@ func Unflatten(m map[string]interface{}, delim string) map[string]interface{} {
// Iterate through the flat conf map.
for k, v := range m {
var (
keys = strings.Split(k, delim)
keys []string
next = out
)

if delim != "" {
keys = strings.Split(k, delim)
} else {
keys = []string{k}
}

// Iterate through key parts, for eg:, parent.child.key
// will be ["parent", "child", "key"]
for _, k := range keys[:len(keys)-1] {
Expand Down
12 changes: 6 additions & 6 deletions tests/maps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var testMap3 = map[string]interface{}{
}

func TestFlatten(t *testing.T) {
f, k := maps.Flatten(testMap, nil, ".")
f, k := maps.Flatten(testMap, nil, delim)
assert.Equal(t, map[string]interface{}{
"parent.child.key": 123,
"parent.child.key.with.dot": 456,
Expand All @@ -118,17 +118,17 @@ func TestFlatten(t *testing.T) {

func BenchmarkFlatten(b *testing.B) {
for n := 0; n < b.N; n++ {
maps.Flatten(testMap3, nil, ".")
maps.Flatten(testMap3, nil, delim)
}
}

func TestUnflatten(t *testing.T) {
m, _ := maps.Flatten(testMap, nil, ".")
um := maps.Unflatten(m, ".")
m, _ := maps.Flatten(testMap, nil, delim)
um := maps.Unflatten(m, delim)
assert.NotEqual(t, um, testMap)

m, _ = maps.Flatten(testMap2, nil, ".")
um = maps.Unflatten(m, ".")
m, _ = maps.Flatten(testMap2, nil, delim)
um = maps.Unflatten(m, delim)
assert.Equal(t, um, testMap2)
}

Expand Down

0 comments on commit 14948fa

Please sign in to comment.