diff --git a/flatmap/expand.go b/flatmap/expand.go index 6f2f6a228..2bfb3fe3d 100644 --- a/flatmap/expand.go +++ b/flatmap/expand.go @@ -114,6 +114,12 @@ func expandArray(m map[string]string, prefix string) []interface{} { } func expandMap(m map[string]string, prefix string) map[string]interface{} { + // Submaps may not have a '%' key, so we can't count on this value being + // here. If we don't have a count, just procede as if we have have a map. + if count, ok := m[prefix+"%"]; ok && count == "0" { + return map[string]interface{}{} + } + result := make(map[string]interface{}) for k := range m { if !strings.HasPrefix(k, prefix) { diff --git a/flatmap/expand_test.go b/flatmap/expand_test.go index 61b151b17..c0fa83211 100644 --- a/flatmap/expand_test.go +++ b/flatmap/expand_test.go @@ -163,17 +163,29 @@ func TestExpand(t *testing.T) { }, }, }, + + { + Map: map[string]string{ + "empty_map_of_sets.%": "0", + "empty_map_of_sets.set1.#": "0", + "empty_map_of_sets.set1.1234": "x", + }, + Key: "empty_map_of_sets", + Output: map[string]interface{}{}, + }, } for _, tc := range cases { - actual := Expand(tc.Map, tc.Key) - if !reflect.DeepEqual(actual, tc.Output) { - t.Errorf( - "Key: %v\nMap:\n\n%#v\n\nOutput:\n\n%#v\n\nExpected:\n\n%#v\n", - tc.Key, - tc.Map, - actual, - tc.Output) - } + t.Run(tc.Key, func(t *testing.T) { + actual := Expand(tc.Map, tc.Key) + if !reflect.DeepEqual(actual, tc.Output) { + t.Errorf( + "Key: %v\nMap:\n\n%#v\n\nOutput:\n\n%#v\n\nExpected:\n\n%#v\n", + tc.Key, + tc.Map, + actual, + tc.Output) + } + }) } }