Skip to content

Commit

Permalink
Fix attribute lookup for dependent schemas (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Mar 16, 2021
1 parent 8b85609 commit 55fe46d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
6 changes: 3 additions & 3 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,15 @@ func traversalToAddress(traversal hcl.Traversal) (lang.Address, error) {
addr := lang.Address{}
for _, tr := range traversal {
switch t := tr.(type) {
case *hcl.TraverseRoot:
case hcl.TraverseRoot:
addr = append(addr, lang.RootStep{
Name: t.Name,
})
case *hcl.TraverseAttr:
case hcl.TraverseAttr:
addr = append(addr, lang.AttrStep{
Name: t.Name,
})
case *hcl.TraverseIndex:
case hcl.TraverseIndex:
addr = append(addr, lang.IndexStep{
Key: t.Key,
})
Expand Down
58 changes: 58 additions & 0 deletions decoder/decoder_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package decoder

import (
"fmt"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/zclconf/go-cty-debug/ctydebug"
"github.com/zclconf/go-cty/cty"
)

func TestDecoder_LoadFile_nilFile(t *testing.T) {
Expand All @@ -31,3 +36,56 @@ func TestDecoder_LoadFile_nilRootBody(t *testing.T) {
t.Fatalf("unexpected error: %s", diff)
}
}

func TestTraversalToAddress(t *testing.T) {
testCases := []struct {
rawTraversal string
expectedAddr lang.Address
}{
{
"one",
lang.Address{
lang.RootStep{Name: "one"},
},
},
{
"first.second",
lang.Address{
lang.RootStep{Name: "first"},
lang.AttrStep{Name: "second"},
},
},
{
"foo[2]",
lang.Address{
lang.RootStep{Name: "foo"},
lang.IndexStep{Key: cty.NumberIntVal(2)},
},
},
{
`foo["bar"]`,
lang.Address{
lang.RootStep{Name: "foo"},
lang.IndexStep{Key: cty.StringVal("bar")},
},
},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
traversal, diags := hclsyntax.ParseTraversalAbs([]byte(tc.rawTraversal), "test.tf", hcl.InitialPos)
if len(diags) > 0 {
t.Fatal(diags)
}

addr, err := traversalToAddress(traversal)
if err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(tc.expectedAddr, addr, ctydebug.CmpOptions); diff != "" {
t.Fatalf("address mismatch: %s", diff)
}
})
}
}

0 comments on commit 55fe46d

Please sign in to comment.