From 868335aa6d5d83178203f86ea8250251713ebff1 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 16 Jul 2021 16:49:07 -0400 Subject: [PATCH] hclsyntax: maintain marks from unknown values Do not discard marks from unknown values when processing string templates. --- hclsyntax/expression_template.go | 16 ++++++++-------- hclsyntax/expression_template_test.go | 10 ++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/hclsyntax/expression_template.go b/hclsyntax/expression_template.go index 0b7e07a5..c3f96943 100644 --- a/hclsyntax/expression_template.go +++ b/hclsyntax/expression_template.go @@ -48,6 +48,12 @@ func (e *TemplateExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) continue } + // Unmark the part and merge its marks into the set + unmarkedVal, partMarks := partVal.Unmark() + for k, v := range partMarks { + marks[k] = v + } + if !partVal.IsKnown() { // If any part is unknown then the result as a whole must be // unknown too. We'll keep on processing the rest of the parts @@ -57,7 +63,7 @@ func (e *TemplateExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) continue } - strVal, err := convert.Convert(partVal, cty.String) + strVal, err := convert.Convert(unmarkedVal, cty.String) if err != nil { diags = append(diags, &hcl.Diagnostic{ Severity: hcl.DiagError, @@ -74,13 +80,7 @@ func (e *TemplateExpr) Value(ctx *hcl.EvalContext) (cty.Value, hcl.Diagnostics) continue } - // 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()) + buf.WriteString(strVal.AsString()) } var ret cty.Value diff --git a/hclsyntax/expression_template_test.go b/hclsyntax/expression_template_test.go index ef830cb6..99892c27 100644 --- a/hclsyntax/expression_template_test.go +++ b/hclsyntax/expression_template_test.go @@ -358,6 +358,16 @@ trim`, cty.StringVal("foobarbaz").WithMarks(cty.NewValueMarks("x", "y", "z")), 0, }, + { // marks from unknown values are maintained + `test_${target}`, + &hcl.EvalContext{ + Variables: map[string]cty.Value{ + "target": cty.UnknownVal(cty.String).Mark("sensitive"), + }, + }, + cty.UnknownVal(cty.String).Mark("sensitive"), + 0, + }, } for _, test := range tests {