From f7adde0c448749bccd7004d73ad0d9ebca347394 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 13 Apr 2017 11:11:41 -0400 Subject: [PATCH] remove maps with empty counts during expand When we encounter maps with empty counts, remove them from the expansion to prevent already empty sub-elements from being retained. Convert TestExpand to subtests for easier debugging. --- flatmap/expand.go | 6 ++++++ flatmap/expand_test.go | 30 +++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 9 deletions(-) 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) + } + }) } }