Skip to content

Commit

Permalink
hclsyntax: maintain marks from unknown values
Browse files Browse the repository at this point in the history
Do not discard marks from unknown values when processing string
templates.
  • Loading branch information
jbardin committed Jul 16, 2021
1 parent 2eb4e9f commit 868335a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
16 changes: 8 additions & 8 deletions hclsyntax/expression_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions hclsyntax/expression_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 868335a

Please sign in to comment.