Skip to content

Commit

Permalink
Merge pull request #437 from hashicorp/alisdair/template-join-expr-ma…
Browse files Browse the repository at this point in the history
…rked

hclsyntax: Fix panic for marked template loops
  • Loading branch information
alisdair authored Jan 5, 2021
2 parents 8aa03bf + f2f7dd7 commit 088f7af
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
12 changes: 9 additions & 3 deletions hclsyntax/expression_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ func (e *TemplateJoinExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnosti
return cty.UnknownVal(cty.String), diags
}

tuple, marks := tuple.Unmark()
allMarks := []cty.ValueMarks{marks}
buf := &bytes.Buffer{}
it := tuple.ElementIterator()
for it.Next() {
Expand All @@ -171,7 +173,7 @@ func (e *TemplateJoinExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnosti
continue
}
if val.Type() == cty.DynamicPseudoType {
return cty.UnknownVal(cty.String), diags
return cty.UnknownVal(cty.String).WithMarks(marks), diags
}
strVal, err := convert.Convert(val, cty.String)
if err != nil {
Expand All @@ -189,13 +191,17 @@ func (e *TemplateJoinExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnosti
continue
}
if !val.IsKnown() {
return cty.UnknownVal(cty.String), diags
return cty.UnknownVal(cty.String).WithMarks(marks), diags
}

strVal, strValMarks := strVal.Unmark()
if len(strValMarks) > 0 {
allMarks = append(allMarks, strValMarks)
}
buf.WriteString(strVal.AsString())
}

return cty.StringVal(buf.String()), diags
return cty.StringVal(buf.String()).WithMarks(allMarks...), diags
}

func (e *TemplateJoinExpr) Range() hcl.Range {
Expand Down
42 changes: 42 additions & 0 deletions hclsyntax/expression_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,48 @@ trim`,
cty.StringVal(`Authenticate with "my voice is my passport"`).WithMarks(cty.NewValueMarks("sensitive")),
0,
},
{ // can loop over marked collections
`%{ for s in secrets }${s}%{ endfor }`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"secrets": cty.ListVal([]cty.Value{
cty.StringVal("foo"),
cty.StringVal("bar"),
cty.StringVal("baz"),
}).Mark("sensitive"),
},
},
cty.StringVal("foobarbaz").Mark("sensitive"),
0,
},
{ // marks on individual elements propagate to the result
`%{ for s in secrets }${s}%{ endfor }`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"secrets": cty.ListVal([]cty.Value{
cty.StringVal("foo"),
cty.StringVal("bar").Mark("sensitive"),
cty.StringVal("baz"),
}),
},
},
cty.StringVal("foobarbaz").Mark("sensitive"),
0,
},
{ // lots of marks!
`%{ for s in secrets }${s}%{ endfor }`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"secrets": cty.ListVal([]cty.Value{
cty.StringVal("foo").Mark("x"),
cty.StringVal("bar").Mark("y"),
cty.StringVal("baz").Mark("z"),
}).Mark("x"), // second instance of x
},
},
cty.StringVal("foobarbaz").WithMarks(cty.NewValueMarks("x", "y", "z")),
0,
},
}

for _, test := range tests {
Expand Down

0 comments on commit 088f7af

Please sign in to comment.