Skip to content

Commit

Permalink
fix: flat tree preorder
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel-Haeberli committed Sep 13, 2023
1 parent cca3669 commit 5d11856
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 4 deletions.
13 changes: 9 additions & 4 deletions momentum-core/tree/momentum-tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,20 @@ func (n *Node) Values() []*Node {
return make([]*Node, 0)
}

return n.flat(make([]*Node, 0))
return n.flatPreorder(make([]*Node, 0))
}

func (n *Node) flat(result []*Node) []*Node {
func (n *Node) flatPreorder(result []*Node) []*Node {

if n == nil {
return result
}

result = append(result, n)

if len(n.Children) > 0 {
result = append(result, n.Children...)
for _, child := range n.Children {
result = append(result, child.flat(result)...)
result = child.flatPreorder(result)
}
}

Expand Down
3 changes: 3 additions & 0 deletions momentum-core/tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ func (n *Node) Remove() {
}
}
n.Parent.Children = newChilds

n.Parent.RemoveYamlChild(n.Path)

n.Parent = nil
}

Expand Down
41 changes: 41 additions & 0 deletions momentum-core/tree/yaml-node-factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tree

import (
"errors"
"momentum-core/utils"
"momentum-core/yaml"
)

Expand All @@ -19,6 +20,46 @@ const (
mergeTag = "!!merge"
)

func (n *Node) RemoveYamlChildren() error {

errs := make([]error, 0)
for _, chld := range n.Children {
errs = append(errs, chld.RemoveYamlChild(chld.Path))
}

return errors.Join(errs...)
}

func (n *Node) RemoveYamlChild(path string) error {

updated := make([]*Node, 0)
for _, child := range n.Children {
if child.Path != path {
updated = append(updated, child)
}

updatedYaml := make([]*yaml.Node, 0)
if child.Path == path {
if child.Parent != nil && child.Parent.YamlNode != nil {
if child.Parent.YamlNode != nil {
for _, yamlChild := range child.Parent.YamlNode.Content {
if yamlChild.Value != utils.LastPartOfPath(path) {
updatedYaml = append(updatedYaml, yamlChild)
}
}
child.Parent.YamlNode.Content = updatedYaml
}
}
}

return nil
}

n.Children = updated

return nil
}

func (n *Node) AddYamlSequence(key string, values []string, style yaml.Style) error {

if len(values) < 1 {
Expand Down

0 comments on commit 5d11856

Please sign in to comment.