Cover empty sets

This commit is contained in:
Pam Selle 2019-09-10 10:37:54 -04:00
parent 4e17209bcc
commit a25abbe7d8
3 changed files with 13 additions and 5 deletions

View File

@ -64,6 +64,12 @@ func evaluateResourceForEachExpressionKnown(expr hcl.Expression, ctx EvalContext
return nil, true, diags
}
// If the map is empty ({}), return an empty map, because cty will return nil when representing {} AsValueMap
// This also covers an empty set (toset([]))
if forEachVal.LengthInt() == 0 {
return map[string]cty.Value{}, true, diags
}
if forEachVal.Type().IsSetType() {
if forEachVal.Type().ElementType() != cty.String {
diags = diags.Append(&hcl.Diagnostic{
@ -84,10 +90,5 @@ func evaluateResourceForEachExpressionKnown(expr hcl.Expression, ctx EvalContext
}
}
// If the map is empty ({}), return an empty map, because cty will return nil when representing {} AsValueMap
if forEachVal.LengthInt() == 0 {
return map[string]cty.Value{}, true, diags
}
return forEachVal.AsValueMap(), true, nil
}

View File

@ -338,6 +338,8 @@ var.foo
const testPlanGraphBuilderForEachStr = `
aws_instance.bar
provider.aws
aws_instance.bar2
provider.aws
aws_instance.bat
aws_instance.boo
provider.aws
@ -349,6 +351,7 @@ aws_instance.foo
provider.aws
meta.count-boundary (EachMode fixup)
aws_instance.bar
aws_instance.bar2
aws_instance.bat
aws_instance.baz
aws_instance.boo
@ -357,6 +360,7 @@ meta.count-boundary (EachMode fixup)
provider.aws
provider.aws (close)
aws_instance.bar
aws_instance.bar2
aws_instance.bat
aws_instance.baz
aws_instance.boo

View File

@ -10,6 +10,9 @@ resource "aws_instance" "foo" {
# sets
resource "aws_instance" "bar" {
for_each = toset([])
}
resource "aws_instance" "bar2" {
for_each = toset(list("z", "y", "x"))
}