vendor/github.com/pelletier/go-toml/v2/internal/tracker/key.go
changeset 260 445e01aede7e
child 265 05c40b36d3b2
equal deleted inserted replaced
259:db4911b0c721 260:445e01aede7e
       
     1 package tracker
       
     2 
       
     3 import (
       
     4 	"github.com/pelletier/go-toml/v2/internal/ast"
       
     5 )
       
     6 
       
     7 // KeyTracker is a tracker that keeps track of the current Key as the AST is
       
     8 // walked.
       
     9 type KeyTracker struct {
       
    10 	k []string
       
    11 }
       
    12 
       
    13 // UpdateTable sets the state of the tracker with the AST table node.
       
    14 func (t *KeyTracker) UpdateTable(node *ast.Node) {
       
    15 	t.reset()
       
    16 	t.Push(node)
       
    17 }
       
    18 
       
    19 // UpdateArrayTable sets the state of the tracker with the AST array table node.
       
    20 func (t *KeyTracker) UpdateArrayTable(node *ast.Node) {
       
    21 	t.reset()
       
    22 	t.Push(node)
       
    23 }
       
    24 
       
    25 // Push the given key on the stack.
       
    26 func (t *KeyTracker) Push(node *ast.Node) {
       
    27 	it := node.Key()
       
    28 	for it.Next() {
       
    29 		t.k = append(t.k, string(it.Node().Data))
       
    30 	}
       
    31 }
       
    32 
       
    33 // Pop key from stack.
       
    34 func (t *KeyTracker) Pop(node *ast.Node) {
       
    35 	it := node.Key()
       
    36 	for it.Next() {
       
    37 		t.k = t.k[:len(t.k)-1]
       
    38 	}
       
    39 }
       
    40 
       
    41 // Key returns the current key
       
    42 func (t *KeyTracker) Key() []string {
       
    43 	k := make([]string, len(t.k))
       
    44 	copy(k, t.k)
       
    45 	return k
       
    46 }
       
    47 
       
    48 func (t *KeyTracker) reset() {
       
    49 	t.k = t.k[:0]
       
    50 }