diff --git a/flatmap/expand.go b/flatmap/expand.go index 2c0a9266b7..87bc9b5dab 100644 --- a/flatmap/expand.go +++ b/flatmap/expand.go @@ -61,12 +61,15 @@ func expandMap(m map[string]string, prefix string) map[string]interface{} { key := k[len(prefix):] idx := strings.Index(key, ".") - if idx == -1 { - idx = len(k) + if idx != -1 { + key = key[:idx] + } + if _, ok := result[key]; ok { + continue } // It contains a period, so it is a more complex structure - result[key] = Expand(m, k[:idx]) + result[key] = Expand(m, k[:len(prefix)+len(key)]) } return result diff --git a/flatmap/expand_test.go b/flatmap/expand_test.go index 6ad54945e3..baf68e6a97 100644 --- a/flatmap/expand_test.go +++ b/flatmap/expand_test.go @@ -49,12 +49,32 @@ func TestExpand(t *testing.T) { }, }, }, + + { + Map: map[string]string{ + "foo.#": "1", + "foo.0.name": "bar", + "foo.0.ports.#": "2", + "foo.0.ports.0": "1", + "foo.0.ports.1": "2", + }, + Key: "foo", + Output: []interface{}{ + map[string]interface{}{ + "name": "bar", + "ports": []interface{}{ + 1, + 2, + }, + }, + }, + }, } for _, tc := range cases { actual := Expand(tc.Map, tc.Key) if !reflect.DeepEqual(actual, tc.Output) { - t.Fatalf( + t.Errorf( "Key: %v\nMap:\n\n%#v\n\nOutput:\n\n%#v\n\nExpected:\n\n%#v\n", tc.Key, tc.Map, diff --git a/flatmap/flatten_test.go b/flatmap/flatten_test.go index 0bd15e8821..9f21d23643 100644 --- a/flatmap/flatten_test.go +++ b/flatmap/flatten_test.go @@ -52,6 +52,27 @@ func TestFlatten(t *testing.T) { "foo.0.enabled": "true", }, }, + + { + Input: map[string]interface{}{ + "foo": []map[interface{}]interface{}{ + map[interface{}]interface{}{ + "name": "bar", + "ports": []string{ + "1", + "2", + }, + }, + }, + }, + Output: map[string]string{ + "foo.#": "1", + "foo.0.name": "bar", + "foo.0.ports.#": "2", + "foo.0.ports.0": "1", + "foo.0.ports.1": "2", + }, + }, } for _, tc := range cases {