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.
This commit is contained in:
James Bardin 2017-03-01 13:28:02 -05:00
parent 177400dbbf
commit 057941ce18
2 changed files with 25 additions and 1 deletions

View File

@ -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

View File

@ -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",