-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
715 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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] | ||
} |
Oops, something went wrong.