-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decoder: Implement reference targets for Reference
- Loading branch information
1 parent
df556ec
commit 59128a1
Showing
3 changed files
with
116 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |