Skip to content

Commit

Permalink
Iterates over all possible paths
Browse files Browse the repository at this point in the history
Considers arrays
see issue basgys#23
basgys#23 (comment)
  • Loading branch information
riccardomanfrin committed Aug 1, 2019
1 parent 996d9fc commit 414c1ca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
8 changes: 5 additions & 3 deletions plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@ func NodePlugin(path string, plugin nodePlugin) nodeFormatter {
}

func (nf *nodeFormatter) Format(node *Node) {
child := node.GetChild(nf.path)
if child != nil {
nf.plugin.AddTo(child)
children := node.GetChildren(nf.path)
if children != nil {
for _, child := range children {
nf.plugin.AddTo(child)
}
}
}

Expand Down
28 changes: 28 additions & 0 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,31 @@ func (n *Node) GetChild(path string) *Node {
}
return result
}

// GetChildren returns a slice of Children for a given path
func (n *Node) GetChildren(path string) []*Node {
pathtok := strings.Split(path, ".")
return n.getChildren(pathtok)
}

func (n *Node) getChildren(pt []string) []*Node {
var result []*Node
name := pt[0]

children, exists := n.Children[name]
if !exists {
return nil
}
if len(children) == 0 {
return nil
}
if len(pt) > 1 {
for _, child := range children {
result = append(result, child.getChildren(pt[1:])...)
}
} else {
result = append(result, children...)
}

return result
}

0 comments on commit 414c1ca

Please sign in to comment.