From 057941ce18bbf1ca288b4de639ce5734081b725e Mon Sep 17 00:00:00 2001 From: James Bardin Date: Wed, 1 Mar 2017 13:28:02 -0500 Subject: [PATCH] make flatmap.Expand understand computed sets For historical reasons, sets are represented as sparse lists in a flatmap, however a computed set does not have a numeric index. Strip the `~` flag from a computed set's index during expansion, and add it back in the prefix after sorting. --- flatmap/expand.go | 13 ++++++++++++- flatmap/expand_test.go | 13 +++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/flatmap/expand.go b/flatmap/expand.go index 422b1ffcc..e325077ef 100644 --- a/flatmap/expand.go +++ b/flatmap/expand.go @@ -57,6 +57,7 @@ func expandArray(m map[string]string, prefix string) []interface{} { // regardless of value, and expand them in numeric order. // See GH-11042 for more details. keySet := map[int]bool{} + computed := map[string]bool{} for k := range m { if !strings.HasPrefix(k, prefix+".") { continue @@ -73,6 +74,12 @@ func expandArray(m map[string]string, prefix string) []interface{} { continue } + // strip the computed flag if there is one + if strings.HasPrefix(key, "~") { + key = key[1:] + computed[key] = true + } + k, err := strconv.Atoi(key) if err != nil { panic(err) @@ -88,7 +95,11 @@ func expandArray(m map[string]string, prefix string) []interface{} { result := make([]interface{}, num) for i, key := range keysList { - result[i] = Expand(m, fmt.Sprintf("%s.%d", prefix, key)) + keyString := strconv.Itoa(key) + if computed[keyString] { + keyString = "~" + keyString + } + result[i] = Expand(m, fmt.Sprintf("%s.%s", prefix, keyString)) } return result diff --git a/flatmap/expand_test.go b/flatmap/expand_test.go index 53963a1ba..cf74fadbc 100644 --- a/flatmap/expand_test.go +++ b/flatmap/expand_test.go @@ -120,6 +120,19 @@ func TestExpand(t *testing.T) { Output: []interface{}{"a", "b", "c"}, }, + { + Map: map[string]string{ + "computed_set.#": "1", + "computed_set.~1234.a": "a", + "computed_set.~1234.b": "b", + "computed_set.~1234.c": "c", + }, + Key: "computed_set", + Output: []interface{}{ + map[string]interface{}{"a": "a", "b": "b", "c": "c"}, + }, + }, + { Map: map[string]string{ "struct.#": "1",