Skip to content

Commit

Permalink
Merge pull request #404 from hashicorp/alisdair/pass-marks-through-ex…
Browse files Browse the repository at this point in the history
…pression-templates

hclsyntax: Pass marks through template expressions
  • Loading branch information
alisdair committed Sep 24, 2020
2 parents bf0a7fe + 876b423 commit 4997e11
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
19 changes: 16 additions & 3 deletions hclsyntax/expression_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ func (e *TemplateExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)
var diags hcl.Diagnostics
isKnown := true

// Maintain a set of marks for values used in the template
marks := make(cty.ValueMarks)

for _, part := range e.Parts {
partVal, partDiags := part.Value(ctx)
diags = append(diags, partDiags...)
Expand Down Expand Up @@ -71,14 +74,24 @@ func (e *TemplateExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics)
continue
}

buf.WriteString(strVal.AsString())
// Unmark the part and merge its marks into the set
unmarked, partMarks := strVal.Unmark()
for k, v := range partMarks {
marks[k] = v
}

buf.WriteString(unmarked.AsString())
}

var ret cty.Value
if !isKnown {
return cty.UnknownVal(cty.String), diags
ret = cty.UnknownVal(cty.String)
} else {
ret = cty.StringVal(buf.String())
}

return cty.StringVal(buf.String()), diags
// Apply the full set of marks to the returned value
return ret.WithMarks(marks), diags
}

func (e *TemplateExpr) Range() hcl.Range {
Expand Down
21 changes: 21 additions & 0 deletions hclsyntax/expression_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,27 @@ trim`,
cty.UnknownVal(cty.String),
1, // Unexpected endfor directive
},
{ // marks from uninterpolated values are ignored
`hello%{ if false } ${target}%{ endif }`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"target": cty.StringVal("world").WithMarks(cty.NewValueMarks("sensitive")),
},
},
cty.StringVal("hello"),
0,
},
{ // marks from interpolated values are passed through
`${greeting} ${target}`,
&hcl.EvalContext{
Variables: map[string]cty.Value{
"greeting": cty.StringVal("hello").WithMarks(cty.NewValueMarks("english")),
"target": cty.StringVal("world").WithMarks(cty.NewValueMarks("sensitive")),
},
},
cty.StringVal("hello world").WithMarks(cty.NewValueMarks("english", "sensitive")),
0,
},
}

for _, test := range tests {
Expand Down

0 comments on commit 4997e11

Please sign in to comment.