Skip to content

Commit

Permalink
decoder: fix block body inferred target collection
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 29, 2023
1 parent 286886d commit 5c324fa
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 9 deletions.
26 changes: 18 additions & 8 deletions decoder/reference_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,9 +678,21 @@ func bodySchemaAsAttrTypes(bodySchema *schema.BodySchema) map[string]cty.Type {
}

for name, attr := range bodySchema.Attributes {
attrType, ok := exprConstraintToDataType(attr.Expr)
if ok {
attrTypes[name] = attrType
if attr.Constraint != nil {
cons, ok := attr.Constraint.(schema.TypeAwareConstraint)
if !ok {
continue
}
typ, ok := cons.ConstraintType()
if !ok {
continue
}
attrTypes[name] = typ
} else {
attrType, ok := exprConstraintToDataType(attr.Expr)
if ok {
attrTypes[name] = attrType
}
}
}

Expand Down Expand Up @@ -774,7 +786,7 @@ func (d *PathDecoder) collectInferredReferenceTargetsForBody(addr lang.Address,
attrExpr = attr.Expr
}

if attrExpr != nil {
if attrExpr != nil && !attrType.IsPrimitiveType() {
if aSchema.Constraint != nil {
ref.NestedTargets = make(reference.Targets, 0)
expr, ok := newExpression(d.pathCtx, attrExpr, aSchema.Constraint).(ReferenceTargetsExpression)
Expand All @@ -783,10 +795,8 @@ func (d *PathDecoder) collectInferredReferenceTargetsForBody(addr lang.Address,
ref.NestedTargets = append(ref.NestedTargets, expr.ReferenceTargets(ctx, targetCtx)...)
}
} else {
if !attrType.IsPrimitiveType() {
ref.NestedTargets = make(reference.Targets, 0)
ref.NestedTargets = append(ref.NestedTargets, decodeReferenceTargetsForComplexTypeExpr(attrAddr, attrExpr, attrType, bAddrSchema.ScopeId)...)
}
ref.NestedTargets = make(reference.Targets, 0)
ref.NestedTargets = append(ref.NestedTargets, decodeReferenceTargetsForComplexTypeExpr(attrAddr, attrExpr, attrType, bAddrSchema.ScopeId)...)
}
}

Expand Down
85 changes: 84 additions & 1 deletion decoder/reference_targets_collect_hcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4732,10 +4732,93 @@ module "different" {
`,
reference.Targets{},
},
{
"inferred body targets which are missing",
&schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"block": {
Address: &schema.BlockAddrSchema{
Steps: []schema.AddrStep{
schema.StaticStep{Name: "blk"},
},
BodyAsData: true,
InferBody: true,
},
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"foo": {
Constraint: schema.LiteralType{
Type: cty.String,
},
},
"bar": {
Constraint: schema.LiteralType{
Type: cty.Number,
},
},
},
},
},
},
},
`block { foo = "" }`,
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "blk"},
},
RangePtr: &hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 19, Byte: 18},
},
DefRangePtr: &hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 6, Byte: 5},
},
Type: cty.Object(map[string]cty.Type{
"foo": cty.String,
"bar": cty.Number,
}),
NestedTargets: reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "blk"},
lang.AttrStep{Name: "bar"},
},
RangePtr: &hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 9, Byte: 8},
End: hcl.Pos{Line: 1, Column: 9, Byte: 8},
},
Type: cty.Number,
},
{
Addr: lang.Address{
lang.RootStep{Name: "blk"},
lang.AttrStep{Name: "foo"},
},
RangePtr: &hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 9, Byte: 8},
End: hcl.Pos{Line: 1, Column: 17, Byte: 16},
},
DefRangePtr: &hcl.Range{
Filename: "test.tf",
Start: hcl.Pos{Line: 1, Column: 9, Byte: 8},
End: hcl.Pos{Line: 1, Column: 12, Byte: 11},
},
Type: cty.String,
},
},
},
},
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.name), func(t *testing.T) {
t.Run(fmt.Sprintf("%2d-%s", i, tc.name), func(t *testing.T) {
f, _ := hclsyntax.ParseConfig([]byte(tc.cfg), "test.tf", hcl.InitialPos)

d := testPathDecoder(t, &PathContext{
Expand Down

0 comments on commit 5c324fa

Please sign in to comment.