Skip to content

Commit

Permalink
Merge pull request #433 from hashicorp/pselle/marked-for
Browse files Browse the repository at this point in the history
Return an error for invalid for expressions with marks
  • Loading branch information
Pam Selle authored Dec 17, 2020
2 parents baa494e + dfa6aff commit d0b4a68
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
13 changes: 13 additions & 0 deletions hclsyntax/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1188,6 +1188,19 @@ func (e *ForExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) {
continue
}

if key.IsMarked() {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Invalid object key",
Detail: "Marked values cannot be used as object keys.",
Subject: e.KeyExpr.Range().Ptr(),
Context: &e.SrcRange,
Expression: e.KeyExpr,
EvalContext: childCtx,
})
continue
}

val, valDiags := e.ValExpr.Value(childCtx)
diags = append(diags, valDiags...)

Expand Down
32 changes: 31 additions & 1 deletion hclsyntax/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,37 @@ upper(
}).Mark("sensitive"),
0,
},

{ // Marked map member carries marks through
`{for k, v in things: k => !v}`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"things": cty.MapVal(map[string]cty.Value{
"a": cty.True.Mark("sensitive"),
"b": cty.False,
}),
},
},
cty.ObjectVal(map[string]cty.Value{
"a": cty.False.Mark("sensitive"),
"b": cty.True,
}),
0,
},
{ // Error when using marked value as object key
`{for v in things: v => "${v}-friend"}`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"things": cty.MapVal(map[string]cty.Value{
"a": cty.StringVal("rosie").Mark("sensitive"),
"b": cty.StringVal("robin"),
}),
},
},
cty.ObjectVal(map[string]cty.Value{
"robin": cty.StringVal("robin-friend"),
}),
1,
},
{
`[{name: "Steve"}, {name: "Ermintrude"}].*.name`,
nil,
Expand Down

0 comments on commit d0b4a68

Please sign in to comment.