summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/pelletier/go-toml/v2/internal/tracker/key.go
blob: 7c148f48d4af5df507fae743d25e3368ed83b01b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package tracker

import (
	"github.com/pelletier/go-toml/v2/internal/ast"
)

// KeyTracker is a tracker that keeps track of the current Key as the AST is
// walked.
type KeyTracker struct {
	k []string
}

// UpdateTable sets the state of the tracker with the AST table node.
func (t *KeyTracker) UpdateTable(node *ast.Node) {
	t.reset()
	t.Push(node)
}

// UpdateArrayTable sets the state of the tracker with the AST array table node.
func (t *KeyTracker) UpdateArrayTable(node *ast.Node) {
	t.reset()
	t.Push(node)
}

// Push the given key on the stack.
func (t *KeyTracker) Push(node *ast.Node) {
	it := node.Key()
	for it.Next() {
		t.k = append(t.k, string(it.Node().Data))
	}
}

// Pop key from stack.
func (t *KeyTracker) Pop(node *ast.Node) {
	it := node.Key()
	for it.Next() {
		t.k = t.k[:len(t.k)-1]
	}
}

// Key returns the current key
func (t *KeyTracker) Key() []string {
	k := make([]string, len(t.k))
	copy(k, t.k)
	return k
}

func (t *KeyTracker) reset() {
	t.k = t.k[:0]
}