vendor/github.com/pelletier/go-toml/v2/internal/tracker/seen.go
changeset 265 05c40b36d3b2
parent 260 445e01aede7e
equal deleted inserted replaced
264:8f478162d991 265:05c40b36d3b2
     3 import (
     3 import (
     4 	"bytes"
     4 	"bytes"
     5 	"fmt"
     5 	"fmt"
     6 	"sync"
     6 	"sync"
     7 
     7 
     8 	"github.com/pelletier/go-toml/v2/internal/ast"
     8 	"github.com/pelletier/go-toml/v2/unstable"
     9 )
     9 )
    10 
    10 
    11 type keyKind uint8
    11 type keyKind uint8
    12 
    12 
    13 const (
    13 const (
   148 }
   148 }
   149 
   149 
   150 // CheckExpression takes a top-level node and checks that it does not contain
   150 // CheckExpression takes a top-level node and checks that it does not contain
   151 // keys that have been seen in previous calls, and validates that types are
   151 // keys that have been seen in previous calls, and validates that types are
   152 // consistent.
   152 // consistent.
   153 func (s *SeenTracker) CheckExpression(node *ast.Node) error {
   153 func (s *SeenTracker) CheckExpression(node *unstable.Node) error {
   154 	if s.entries == nil {
   154 	if s.entries == nil {
   155 		s.reset()
   155 		s.reset()
   156 	}
   156 	}
   157 	switch node.Kind {
   157 	switch node.Kind {
   158 	case ast.KeyValue:
   158 	case unstable.KeyValue:
   159 		return s.checkKeyValue(node)
   159 		return s.checkKeyValue(node)
   160 	case ast.Table:
   160 	case unstable.Table:
   161 		return s.checkTable(node)
   161 		return s.checkTable(node)
   162 	case ast.ArrayTable:
   162 	case unstable.ArrayTable:
   163 		return s.checkArrayTable(node)
   163 		return s.checkArrayTable(node)
   164 	default:
   164 	default:
   165 		panic(fmt.Errorf("this should not be a top level node type: %s", node.Kind))
   165 		panic(fmt.Errorf("this should not be a top level node type: %s", node.Kind))
   166 	}
   166 	}
   167 }
   167 }
   168 
   168 
   169 func (s *SeenTracker) checkTable(node *ast.Node) error {
   169 func (s *SeenTracker) checkTable(node *unstable.Node) error {
   170 	if s.currentIdx >= 0 {
   170 	if s.currentIdx >= 0 {
   171 		s.setExplicitFlag(s.currentIdx)
   171 		s.setExplicitFlag(s.currentIdx)
   172 	}
   172 	}
   173 
   173 
   174 	it := node.Key()
   174 	it := node.Key()
   217 	s.currentIdx = idx
   217 	s.currentIdx = idx
   218 
   218 
   219 	return nil
   219 	return nil
   220 }
   220 }
   221 
   221 
   222 func (s *SeenTracker) checkArrayTable(node *ast.Node) error {
   222 func (s *SeenTracker) checkArrayTable(node *unstable.Node) error {
   223 	if s.currentIdx >= 0 {
   223 	if s.currentIdx >= 0 {
   224 		s.setExplicitFlag(s.currentIdx)
   224 		s.setExplicitFlag(s.currentIdx)
   225 	}
   225 	}
   226 
   226 
   227 	it := node.Key()
   227 	it := node.Key()
   265 	s.currentIdx = idx
   265 	s.currentIdx = idx
   266 
   266 
   267 	return nil
   267 	return nil
   268 }
   268 }
   269 
   269 
   270 func (s *SeenTracker) checkKeyValue(node *ast.Node) error {
   270 func (s *SeenTracker) checkKeyValue(node *unstable.Node) error {
   271 	parentIdx := s.currentIdx
   271 	parentIdx := s.currentIdx
   272 	it := node.Key()
   272 	it := node.Key()
   273 
   273 
   274 	for it.Next() {
   274 	for it.Next() {
   275 		k := it.Node().Data
   275 		k := it.Node().Data
   295 	s.entries[parentIdx].kind = valueKind
   295 	s.entries[parentIdx].kind = valueKind
   296 
   296 
   297 	value := node.Value()
   297 	value := node.Value()
   298 
   298 
   299 	switch value.Kind {
   299 	switch value.Kind {
   300 	case ast.InlineTable:
   300 	case unstable.InlineTable:
   301 		return s.checkInlineTable(value)
   301 		return s.checkInlineTable(value)
   302 	case ast.Array:
   302 	case unstable.Array:
   303 		return s.checkArray(value)
   303 		return s.checkArray(value)
   304 	}
   304 	}
   305 
   305 
   306 	return nil
   306 	return nil
   307 }
   307 }
   308 
   308 
   309 func (s *SeenTracker) checkArray(node *ast.Node) error {
   309 func (s *SeenTracker) checkArray(node *unstable.Node) error {
   310 	it := node.Children()
   310 	it := node.Children()
   311 	for it.Next() {
   311 	for it.Next() {
   312 		n := it.Node()
   312 		n := it.Node()
   313 		switch n.Kind {
   313 		switch n.Kind {
   314 		case ast.InlineTable:
   314 		case unstable.InlineTable:
   315 			err := s.checkInlineTable(n)
   315 			err := s.checkInlineTable(n)
   316 			if err != nil {
   316 			if err != nil {
   317 				return err
   317 				return err
   318 			}
   318 			}
   319 		case ast.Array:
   319 		case unstable.Array:
   320 			err := s.checkArray(n)
   320 			err := s.checkArray(n)
   321 			if err != nil {
   321 			if err != nil {
   322 				return err
   322 				return err
   323 			}
   323 			}
   324 		}
   324 		}
   325 	}
   325 	}
   326 	return nil
   326 	return nil
   327 }
   327 }
   328 
   328 
   329 func (s *SeenTracker) checkInlineTable(node *ast.Node) error {
   329 func (s *SeenTracker) checkInlineTable(node *unstable.Node) error {
   330 	if pool.New == nil {
   330 	if pool.New == nil {
   331 		pool.New = func() interface{} {
   331 		pool.New = func() interface{} {
   332 			return &SeenTracker{}
   332 			return &SeenTracker{}
   333 		}
   333 		}
   334 	}
   334 	}