hcl/ast: ObjectList.Prefix
This commit is contained in:
parent
7996653560
commit
0c18c66fff
@ -43,6 +43,34 @@ func (o *ObjectList) Add(item *ObjectItem) {
|
|||||||
o.Items = append(o.Items, item)
|
o.Items = append(o.Items, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o *ObjectList) Prefix(keys ...string) *ObjectList {
|
||||||
|
var result ObjectList
|
||||||
|
for _, item := range o.Items {
|
||||||
|
// If there aren't enough keys, then ignore this
|
||||||
|
if len(item.Keys) < len(keys)+1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
match := true
|
||||||
|
for i, key := range item.Keys[:len(keys)] {
|
||||||
|
if key.Token.Text != keys[i] {
|
||||||
|
match = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !match {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strip off the prefix from the children
|
||||||
|
newItem := *item
|
||||||
|
newItem.Keys = newItem.Keys[len(keys):]
|
||||||
|
result.Add(&newItem)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &result
|
||||||
|
}
|
||||||
|
|
||||||
func (o *ObjectList) Pos() token.Pos {
|
func (o *ObjectList) Pos() token.Pos {
|
||||||
// always returns the uninitiliazed position
|
// always returns the uninitiliazed position
|
||||||
return o.Items[0].Pos()
|
return o.Items[0].Pos()
|
||||||
|
62
hcl/ast/ast_test.go
Normal file
62
hcl/ast/ast_test.go
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
package ast
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/hcl/hcl/token"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestObjectListPrefix(t *testing.T) {
|
||||||
|
var cases = []struct {
|
||||||
|
Prefix []string
|
||||||
|
Input []*ObjectItem
|
||||||
|
Output []*ObjectItem
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
[]string{"foo"},
|
||||||
|
[]*ObjectItem{
|
||||||
|
&ObjectItem{
|
||||||
|
Keys: []*ObjectKey{
|
||||||
|
&ObjectKey{
|
||||||
|
Token: token.Token{Type: token.STRING, Text: `foo`},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
[]string{"foo"},
|
||||||
|
[]*ObjectItem{
|
||||||
|
&ObjectItem{
|
||||||
|
Keys: []*ObjectKey{
|
||||||
|
&ObjectKey{Token: token.Token{Type: token.STRING, Text: `foo`}},
|
||||||
|
&ObjectKey{Token: token.Token{Type: token.STRING, Text: `bar`}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
&ObjectItem{
|
||||||
|
Keys: []*ObjectKey{
|
||||||
|
&ObjectKey{Token: token.Token{Type: token.STRING, Text: `baz`}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
[]*ObjectItem{
|
||||||
|
&ObjectItem{
|
||||||
|
Keys: []*ObjectKey{
|
||||||
|
&ObjectKey{Token: token.Token{Type: token.STRING, Text: `bar`}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
input := &ObjectList{Items: tc.Input}
|
||||||
|
expected := &ObjectList{Items: tc.Output}
|
||||||
|
if actual := input.Prefix(tc.Prefix...); !reflect.DeepEqual(actual, expected) {
|
||||||
|
t.Fatalf("in order: input, expected, actual\n\n%#v\n\n%#v\n\n%#v", input, expected, actual)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user