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