Skip to content

Commit

Permalink
decoder: Fix collection of implied declared targets of complex types (#…
Browse files Browse the repository at this point in the history
…259)

* decoder: add tests for implied declared targets of complex types

* decoder: Fix collection of implied declared targets
  • Loading branch information
radeksimko authored Apr 18, 2023
1 parent 34c0b60 commit 43d4964
Show file tree
Hide file tree
Showing 6 changed files with 761 additions and 16 deletions.
134 changes: 134 additions & 0 deletions decoder/expr_list_ref_targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,73 @@ func TestCollectRefTargets_exprList_implied_hcl(t *testing.T) {
},
},
},
{
"declared as type",
&schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"blk": {
Address: &schema.BlockAddrSchema{
Steps: schema.Address{
schema.StaticStep{Name: "blk"},
},
BodyAsData: true,
InferBody: true,
},
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.LiteralType{Type: cty.Bool},
},
IsOptional: true,
},
},
},
},
},
},
`blk { attr = [] }`,
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "blk"},
},
RangePtr: &hcl.Range{
Filename: "test.hcl",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 18, Byte: 17},
},
DefRangePtr: &hcl.Range{
Filename: "test.hcl",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 4, Byte: 3},
},
Type: cty.Object(map[string]cty.Type{
"attr": cty.List(cty.Bool),
}),
NestedTargets: reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "blk"},
lang.AttrStep{Name: "attr"},
},
RangePtr: &hcl.Range{
Filename: "test.hcl",
Start: hcl.Pos{Line: 1, Column: 7, Byte: 6},
End: hcl.Pos{Line: 1, Column: 16, Byte: 15},
},
DefRangePtr: &hcl.Range{
Filename: "test.hcl",
Start: hcl.Pos{Line: 1, Column: 7, Byte: 6},
End: hcl.Pos{Line: 1, Column: 11, Byte: 10},
},
Type: cty.List(cty.Bool),
NestedTargets: reference.Targets{},
},
},
},
},
},
{
"undeclared as reference",
&schema.BodySchema{
Expand Down Expand Up @@ -1013,6 +1080,73 @@ func TestCollectRefTargets_exprList_implied_json(t *testing.T) {
},
},
},
{
"declared as type",
&schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"blk": {
Address: &schema.BlockAddrSchema{
Steps: schema.Address{
schema.StaticStep{Name: "blk"},
},
BodyAsData: true,
InferBody: true,
},
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.List{
Elem: schema.LiteralType{Type: cty.Bool},
},
IsOptional: true,
},
},
},
},
},
},
`{"blk": {"attr": []}}`,
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "blk"},
},
RangePtr: &hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line: 1, Column: 9, Byte: 8},
End: hcl.Pos{Line: 1, Column: 21, Byte: 20},
},
DefRangePtr: &hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line: 1, Column: 9, Byte: 8},
End: hcl.Pos{Line: 1, Column: 10, Byte: 9},
},
Type: cty.Object(map[string]cty.Type{
"attr": cty.List(cty.Bool),
}),
NestedTargets: reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "blk"},
lang.AttrStep{Name: "attr"},
},
RangePtr: &hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line: 1, Column: 10, Byte: 9},
End: hcl.Pos{Line: 1, Column: 20, Byte: 19},
},
DefRangePtr: &hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line: 1, Column: 10, Byte: 9},
End: hcl.Pos{Line: 1, Column: 16, Byte: 15},
},
Type: cty.List(cty.Bool),
NestedTargets: reference.Targets{},
},
},
},
},
},
{
"undeclared as reference",
&schema.BodySchema{
Expand Down
134 changes: 134 additions & 0 deletions decoder/expr_map_ref_targets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,73 @@ func TestCollectRefTargets_exprMap_implied_hcl(t *testing.T) {
},
},
},
{
"declared as type",
&schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"blk": {
Address: &schema.BlockAddrSchema{
Steps: schema.Address{
schema.StaticStep{Name: "blk"},
},
BodyAsData: true,
InferBody: true,
},
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Map{
Elem: schema.LiteralType{Type: cty.Bool},
},
IsOptional: true,
},
},
},
},
},
},
`blk { attr = {} }`,
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "blk"},
},
RangePtr: &hcl.Range{
Filename: "test.hcl",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 18, Byte: 17},
},
DefRangePtr: &hcl.Range{
Filename: "test.hcl",
Start: hcl.Pos{Line: 1, Column: 1, Byte: 0},
End: hcl.Pos{Line: 1, Column: 4, Byte: 3},
},
Type: cty.Object(map[string]cty.Type{
"attr": cty.Map(cty.Bool),
}),
NestedTargets: reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "blk"},
lang.AttrStep{Name: "attr"},
},
RangePtr: &hcl.Range{
Filename: "test.hcl",
Start: hcl.Pos{Line: 1, Column: 7, Byte: 6},
End: hcl.Pos{Line: 1, Column: 16, Byte: 15},
},
DefRangePtr: &hcl.Range{
Filename: "test.hcl",
Start: hcl.Pos{Line: 1, Column: 7, Byte: 6},
End: hcl.Pos{Line: 1, Column: 11, Byte: 10},
},
Type: cty.Map(cty.Bool),
NestedTargets: reference.Targets{},
},
},
},
},
},
{
"undeclared as reference",
&schema.BodySchema{
Expand Down Expand Up @@ -1284,6 +1351,73 @@ func TestCollectRefTargets_exprMap_implied_json(t *testing.T) {
},
},
},
{
"declared as type",
&schema.BodySchema{
Blocks: map[string]*schema.BlockSchema{
"blk": {
Address: &schema.BlockAddrSchema{
Steps: schema.Address{
schema.StaticStep{Name: "blk"},
},
BodyAsData: true,
InferBody: true,
},
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"attr": {
Constraint: schema.Map{
Elem: schema.LiteralType{Type: cty.Bool},
},
IsOptional: true,
},
},
},
},
},
},
`{"blk": {"attr": {}}}`,
reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "blk"},
},
RangePtr: &hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line: 1, Column: 9, Byte: 8},
End: hcl.Pos{Line: 1, Column: 21, Byte: 20},
},
DefRangePtr: &hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line: 1, Column: 9, Byte: 8},
End: hcl.Pos{Line: 1, Column: 10, Byte: 9},
},
Type: cty.Object(map[string]cty.Type{
"attr": cty.Map(cty.Bool),
}),
NestedTargets: reference.Targets{
{
Addr: lang.Address{
lang.RootStep{Name: "blk"},
lang.AttrStep{Name: "attr"},
},
RangePtr: &hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line: 1, Column: 10, Byte: 9},
End: hcl.Pos{Line: 1, Column: 20, Byte: 19},
},
DefRangePtr: &hcl.Range{
Filename: "test.hcl.json",
Start: hcl.Pos{Line: 1, Column: 10, Byte: 9},
End: hcl.Pos{Line: 1, Column: 16, Byte: 15},
},
Type: cty.Map(cty.Bool),
NestedTargets: reference.Targets{},
},
},
},
},
},
{
"undeclared as reference",
&schema.BodySchema{
Expand Down
Loading

0 comments on commit 43d4964

Please sign in to comment.