Skip to content

Commit 66637ed

Browse files
committed
use new unwrapJSONRefExpr for replace_triggered_by
Get rid of the duplicated code, and use the new function which also validates the expression result type that was missed in the existing implementation.
1 parent a2b189f commit 66637ed

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

internal/configs/resource.go

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/hashicorp/hcl/v2"
1010
"github.com/hashicorp/hcl/v2/gohcl"
1111
"github.com/hashicorp/hcl/v2/hclsyntax"
12-
hcljson "github.com/hashicorp/hcl/v2/json"
1312

1413
"github.com/hashicorp/terraform/internal/addrs"
1514
"github.com/hashicorp/terraform/internal/lang"
@@ -539,34 +538,25 @@ func decodeDataBlock(block *hcl.Block, override, nested bool) (*Resource, hcl.Di
539538
// replace_triggered_by expressions, ensuring they only contains references to
540539
// a single resource, and the only extra variables are count.index or each.key.
541540
func decodeReplaceTriggeredBy(expr hcl.Expression) ([]hcl.Expression, hcl.Diagnostics) {
542-
// Since we are manually parsing the replace_triggered_by argument, we
543-
// need to specially handle json configs, in which case the values will
544-
// be json strings rather than hcl. To simplify parsing however we will
545-
// decode the individual list elements, rather than the entire expression.
546-
isJSON := hcljson.IsJSONExpression(expr)
547-
548541
exprs, diags := hcl.ExprList(expr)
542+
if diags.HasErrors() {
543+
return nil, diags
544+
}
549545

550546
for i, expr := range exprs {
551-
if isJSON {
552-
// We can abuse the hcl json api and rely on the fact that calling
553-
// Value on a json expression with no EvalContext will return the
554-
// raw string. We can then parse that as normal hcl syntax, and
555-
// continue with the decoding.
556-
v, ds := expr.Value(nil)
557-
diags = diags.Extend(ds)
558-
if diags.HasErrors() {
559-
continue
560-
}
561-
562-
expr, ds = hclsyntax.ParseExpression([]byte(v.AsString()), "", expr.Range().Start)
563-
diags = diags.Extend(ds)
564-
if diags.HasErrors() {
565-
continue
566-
}
567-
// make sure to swap out the expression we're returning too
568-
exprs[i] = expr
547+
// Since we are manually parsing the replace_triggered_by argument, we
548+
// need to specially handle json configs, in which case the values will
549+
// be json strings rather than hcl. To simplify parsing however we will
550+
// decode the individual list elements, rather than the entire
551+
// expression.
552+
var jsDiags hcl.Diagnostics
553+
expr, jsDiags = unwrapJSONRefExpr(expr)
554+
diags = diags.Extend(jsDiags)
555+
if diags.HasErrors() {
556+
continue
569557
}
558+
// re-assign the value in case it was replaced by a json expression
559+
exprs[i] = expr
570560

571561
refs, refDiags := lang.ReferencesInExpr(addrs.ParseRef, expr)
572562
for _, diag := range refDiags {

0 commit comments

Comments
 (0)