Skip to content

Commit b954e17

Browse files
colinhoglundapparentlymart
authored andcommitted
hclwrite: fix TokensForTraversal handling of index steps
1 parent 3c7194d commit b954e17

2 files changed

Lines changed: 48 additions & 3 deletions

File tree

hclwrite/generate.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,12 @@ func appendTokensForValue(val cty.Value, toks Tokens) Tokens {
159159

160160
func appendTokensForTraversal(traversal hcl.Traversal, toks Tokens) Tokens {
161161
for _, step := range traversal {
162-
appendTokensForTraversalStep(step, toks)
162+
toks = appendTokensForTraversalStep(step, toks)
163163
}
164164
return toks
165165
}
166166

167-
func appendTokensForTraversalStep(step hcl.Traverser, toks Tokens) {
167+
func appendTokensForTraversalStep(step hcl.Traverser, toks Tokens) Tokens {
168168
switch ts := step.(type) {
169169
case hcl.TraverseRoot:
170170
toks = append(toks, &Token{
@@ -188,14 +188,16 @@ func appendTokensForTraversalStep(step hcl.Traverser, toks Tokens) {
188188
Type: hclsyntax.TokenOBrack,
189189
Bytes: []byte{'['},
190190
})
191-
appendTokensForValue(ts.Key, toks)
191+
toks = appendTokensForValue(ts.Key, toks)
192192
toks = append(toks, &Token{
193193
Type: hclsyntax.TokenCBrack,
194194
Bytes: []byte{']'},
195195
})
196196
default:
197197
panic(fmt.Sprintf("unsupported traversal step type %T", step))
198198
}
199+
200+
return toks
199201
}
200202

201203
func escapeQuotedStringLit(s string) []byte {

hclwrite/generate_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/google/go-cmp/cmp"
9+
"github.com/hashicorp/hcl/v2"
910
"github.com/hashicorp/hcl/v2/hclsyntax"
1011
"github.com/zclconf/go-cty/cty"
1112
)
@@ -472,3 +473,45 @@ func TestTokensForValue(t *testing.T) {
472473
})
473474
}
474475
}
476+
477+
func TestTokensForTraversal(t *testing.T) {
478+
tests := []struct {
479+
Val hcl.Traversal
480+
Want Tokens
481+
}{
482+
{
483+
hcl.Traversal{
484+
hcl.TraverseRoot{Name: "root"},
485+
hcl.TraverseAttr{Name: "attr"},
486+
hcl.TraverseIndex{Key: cty.StringVal("index")},
487+
},
488+
Tokens{
489+
{Type: hclsyntax.TokenIdent, Bytes: []byte("root")},
490+
{Type: hclsyntax.TokenDot, Bytes: []byte(".")},
491+
{Type: hclsyntax.TokenIdent, Bytes: []byte("attr")},
492+
{Type: hclsyntax.TokenOBrack, Bytes: []byte{'['}},
493+
{Type: hclsyntax.TokenOQuote, Bytes: []byte(`"`)},
494+
{Type: hclsyntax.TokenQuotedLit, Bytes: []byte("index")},
495+
{Type: hclsyntax.TokenCQuote, Bytes: []byte(`"`)},
496+
{Type: hclsyntax.TokenCBrack, Bytes: []byte{']'}},
497+
},
498+
},
499+
}
500+
501+
for _, test := range tests {
502+
got := TokensForTraversal(test.Val)
503+
504+
if !cmp.Equal(got, test.Want) {
505+
diff := cmp.Diff(got, test.Want, cmp.Comparer(func(a, b []byte) bool {
506+
return bytes.Equal(a, b)
507+
}))
508+
var gotBuf, wantBuf bytes.Buffer
509+
got.WriteTo(&gotBuf)
510+
test.Want.WriteTo(&wantBuf)
511+
t.Errorf(
512+
"wrong result\nvalue: %#v\ngot: %s\nwant: %s\ndiff: %s",
513+
test.Val, gotBuf.String(), wantBuf.String(), diff,
514+
)
515+
}
516+
}
517+
}

0 commit comments

Comments
 (0)