Skip to content

Commit

Permalink
decoder: Implement reference targets for Reference
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Mar 22, 2023
1 parent df556ec commit 59128a1
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 8 deletions.
8 changes: 0 additions & 8 deletions decoder/expr_reference.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package decoder

import (
"context"

"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
)
Expand All @@ -13,8 +10,3 @@ type Reference struct {
cons schema.Reference
pathCtx *PathContext
}

func (ref Reference) ReferenceTargets(ctx context.Context, targetCtx *TargetContext) reference.Targets {
// TODO
return nil
}
30 changes: 30 additions & 0 deletions decoder/expr_reference_ref_targets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package decoder

import (
"context"

"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl/v2/hclsyntax"
)

func (ref Reference) ReferenceTargets(ctx context.Context, targetCtx *TargetContext) reference.Targets {
if ref.cons.Address == nil {
return reference.Targets{}
}

eType, ok := ref.expr.(*hclsyntax.ScopeTraversalExpr)
if !ok {
return reference.Targets{}
}

targets := make(reference.Targets, 0)
targets = append(targets, reference.Target{
Addr: targetCtx.ParentAddress,
LocalAddr: targetCtx.ParentLocalAddress,
ScopeId: ref.cons.Address.ScopeId,
RangePtr: eType.SrcRange.Ptr(),
Name: ref.cons.Name,
})

return targets
}
86 changes: 86 additions & 0 deletions decoder/expr_reference_ref_targets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package decoder

import (
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl-lang/schema"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
)

func TestCollectRefTargets_exprReference_hcl(t *testing.T) {
testCases := []struct {
testName string
attrSchema map[string]*schema.AttributeSchema
cfg string
expectedRefTargets reference.Targets
}{
// TODO: No traversal
// TODO: Non-addressable reference
// TODO: Addressable reference
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.testName), func(t *testing.T) {
bodySchema := &schema.BodySchema{
Attributes: tc.attrSchema,
}

f, _ := hclsyntax.ParseConfig([]byte(tc.cfg), "test.tf", hcl.InitialPos)
d := testPathDecoder(t, &PathContext{
Schema: bodySchema,
Files: map[string]*hcl.File{
"test.tf": f,
},
})

targets, err := d.CollectReferenceTargets()
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(tc.expectedRefTargets, targets); diff != "" {
t.Fatalf("unexpected tokens: %s", diff)
}
})
}
}

func TestCollectRefTargets_exprReference_json(t *testing.T) {
testCases := []struct {
testName string
attrSchema map[string]*schema.AttributeSchema
cfg string
expectedRefTargets reference.Targets
}{
// TODO: No traversal
// TODO: Non-addressable reference
// TODO: Addressable reference
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.testName), func(t *testing.T) {
bodySchema := &schema.BodySchema{
Attributes: tc.attrSchema,
}

f, _ := hclsyntax.ParseConfig([]byte(tc.cfg), "test.tf.json", hcl.InitialPos)
d := testPathDecoder(t, &PathContext{
Schema: bodySchema,
Files: map[string]*hcl.File{
"test.tf.json": f,
},
})

targets, err := d.CollectReferenceTargets()
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(tc.expectedRefTargets, targets); diff != "" {
t.Fatalf("unexpected tokens: %s", diff)
}
})
}
}

0 comments on commit 59128a1

Please sign in to comment.