Merge pull request #12345 from hashicorp/jbardin/GH-12145
make flatmap.Expand understand computed sets
This commit is contained in:
commit
87dfe06598
|
@ -57,6 +57,7 @@ func expandArray(m map[string]string, prefix string) []interface{} {
|
||||||
// regardless of value, and expand them in numeric order.
|
// regardless of value, and expand them in numeric order.
|
||||||
// See GH-11042 for more details.
|
// See GH-11042 for more details.
|
||||||
keySet := map[int]bool{}
|
keySet := map[int]bool{}
|
||||||
|
computed := map[string]bool{}
|
||||||
for k := range m {
|
for k := range m {
|
||||||
if !strings.HasPrefix(k, prefix+".") {
|
if !strings.HasPrefix(k, prefix+".") {
|
||||||
continue
|
continue
|
||||||
|
@ -73,6 +74,12 @@ func expandArray(m map[string]string, prefix string) []interface{} {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// strip the computed flag if there is one
|
||||||
|
if strings.HasPrefix(key, "~") {
|
||||||
|
key = key[1:]
|
||||||
|
computed[key] = true
|
||||||
|
}
|
||||||
|
|
||||||
k, err := strconv.Atoi(key)
|
k, err := strconv.Atoi(key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -88,7 +95,11 @@ func expandArray(m map[string]string, prefix string) []interface{} {
|
||||||
|
|
||||||
result := make([]interface{}, num)
|
result := make([]interface{}, num)
|
||||||
for i, key := range keysList {
|
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
|
return result
|
||||||
|
|
|
@ -120,6 +120,19 @@ func TestExpand(t *testing.T) {
|
||||||
Output: []interface{}{"a", "b", "c"},
|
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{
|
Map: map[string]string{
|
||||||
"struct.#": "1",
|
"struct.#": "1",
|
||||||
|
|
Loading…
Reference in New Issue