Merge pull request #12345 from hashicorp/jbardin/GH-12145

make flatmap.Expand understand computed sets
This commit is contained in:
James Bardin 2017-03-01 14:54:50 -05:00 committed by GitHub
commit 87dfe06598
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",