Skip to content

Commit

Permalink
Cover empty sets
Browse files Browse the repository at this point in the history
  • Loading branch information
pselle committed Sep 10, 2019
1 parent 4e17209 commit 7e2ab78
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
9 changes: 4 additions & 5 deletions terraform/eval_for_each.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func evaluateResourceForEachExpressionKnown(expr hcl.Expression, ctx EvalContext
return nil, true, diags
case !forEachVal.IsKnown():
return map[string]cty.Value{}, false, 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([]))
case forEachVal.LengthInt() == 0:
return map[string]cty.Value{}, true, diags
}

if !forEachVal.CanIterateElements() || forEachVal.Type().IsListType() || forEachVal.Type().IsTupleType() {
Expand Down Expand Up @@ -84,10 +88,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
}
4 changes: 4 additions & 0 deletions terraform/graph_builder_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions terraform/testdata/plan-for-each/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}

Expand Down

0 comments on commit 7e2ab78

Please sign in to comment.