Skip to content

Commit

Permalink
Introduce parent to traverse tree function
Browse files Browse the repository at this point in the history
Add new parent node argument to the `traverseTree` function.

Add new parent node argument to the caller function.
  • Loading branch information
HeavyWombat committed Sep 5, 2020
1 parent 21a6f7c commit 04e828b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
12 changes: 8 additions & 4 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func ListPaths(location string) ([]Path, error) {
for idx, document := range inputfile.Documents {
root := Path{DocumentIdx: idx}

traverseTree(root, document, func(path Path, _ *yamlv3.Node) {
traverseTree(root, nil, document, func(path Path, _ *yamlv3.Node, _ *yamlv3.Node) {
paths = append(paths, path)
})
}
Expand All @@ -240,7 +240,7 @@ func IsPathInTree(tree *yamlv3.Node, pathString string) (bool, error) {

go func() {
for _, node := range tree.Content {
traverseTree(Path{}, node, func(path Path, _ *yamlv3.Node) {
traverseTree(Path{}, nil, node, func(path Path, _ *yamlv3.Node, _ *yamlv3.Node) {
if path.ToGoPatchStyle() == searchPath.ToGoPatchStyle() {
resultChan <- true
}
Expand All @@ -253,11 +253,12 @@ func IsPathInTree(tree *yamlv3.Node, pathString string) (bool, error) {
return <-resultChan, nil
}

func traverseTree(path Path, node *yamlv3.Node, leafFunc func(p Path, n *yamlv3.Node)) {
func traverseTree(path Path, parent *yamlv3.Node, node *yamlv3.Node, leafFunc func(path Path, parent *yamlv3.Node, leaf *yamlv3.Node)) {
switch node.Kind {
case yamlv3.DocumentNode:
traverseTree(
path,
node,
node.Content[0],
leafFunc,
)
Expand All @@ -275,6 +276,7 @@ func traverseTree(path Path, node *yamlv3.Node, leafFunc func(p Path, n *yamlv3.

traverseTree(
NewPathWithNamedElement(tmpPath, k.Value),
node,
v,
leafFunc,
)
Expand All @@ -285,6 +287,7 @@ func traverseTree(path Path, node *yamlv3.Node, leafFunc func(p Path, n *yamlv3.
for idx, entry := range node.Content {
traverseTree(
NewPathWithIndexedListElement(path, idx),
node,
entry,
leafFunc,
)
Expand All @@ -296,13 +299,14 @@ func traverseTree(path Path, node *yamlv3.Node, leafFunc func(p Path, n *yamlv3.
k, v := node.Content[i], node.Content[i+1]
traverseTree(
NewPathWithNamedElement(path, k.Value),
node,
v,
leafFunc,
)
}

default:
leafFunc(path, node)
leafFunc(path, parent, node)
}
}

Expand Down
3 changes: 2 additions & 1 deletion restructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ func maxDepth(node *yamlv3.Node) (max int) {
rootPath, _ := ParseGoPatchStylePathString("/")
traverseTree(
rootPath,
nil,
node,
func(p Path, _ *yamlv3.Node) {
func(p Path, _ *yamlv3.Node, _ *yamlv3.Node) {
if depth := len(p.PathElements); depth > max {
max = depth
}
Expand Down

0 comments on commit 04e828b

Please sign in to comment.