seen: verify arrays (#663)

Fixes #662
This commit is contained in:
Thomas Pelletier
2021-11-09 20:26:30 -05:00
committed by GitHub
parent 644515958c
commit f27a07d31a
3 changed files with 39 additions and 9 deletions
-7
View File
@@ -136,7 +136,6 @@ func (n *Node) Key() Iterator {
// Guaranteed to be non-nil.
// Panics if not called on a KeyValue node, or if the Children are malformed.
func (n *Node) Value() *Node {
assertKind(KeyValue, *n)
return n.Child()
}
@@ -144,9 +143,3 @@ func (n *Node) Value() *Node {
func (n *Node) Children() Iterator {
return Iterator{node: n.Child()}
}
func assertKind(k Kind, n Node) {
if n.Kind != k {
panic(fmt.Errorf("method was expecting a %s, not a %s", k, n.Kind))
}
}
+33 -2
View File
@@ -232,9 +232,14 @@ func (s *SeenTracker) checkKeyValue(parentIdx int, node *ast.Node) error {
kind := valueKind
var err error
if node.Value().Kind == ast.InlineTable {
value := node.Value()
switch value.Kind {
case ast.InlineTable:
kind = tableKind
err = s.checkInlineTable(parentIdx, node.Value())
err = s.checkInlineTable(parentIdx, value)
case ast.Array:
err = s.checkArray(parentIdx, value)
}
s.entries[parentIdx].kind = kind
@@ -242,6 +247,32 @@ func (s *SeenTracker) checkKeyValue(parentIdx int, node *ast.Node) error {
return err
}
func (s *SeenTracker) checkArray(parentIdx int, node *ast.Node) error {
set := false
it := node.Children()
for it.Next() {
if set {
s.clear(parentIdx)
}
n := it.Node()
switch n.Kind {
case ast.InlineTable:
err := s.checkInlineTable(parentIdx, n)
if err != nil {
return err
}
set = true
case ast.Array:
err := s.checkArray(parentIdx, n)
if err != nil {
return err
}
set = true
}
}
return nil
}
func (s *SeenTracker) checkInlineTable(parentIdx int, node *ast.Node) error {
it := node.Children()
for it.Next() {