diff --git a/momentum-core/tree/momentum-tree.go b/momentum-core/tree/momentum-tree.go index 3979e6e..630b17e 100644 --- a/momentum-core/tree/momentum-tree.go +++ b/momentum-core/tree/momentum-tree.go @@ -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) } } diff --git a/momentum-core/tree/tree.go b/momentum-core/tree/tree.go index 5b16b90..f58a2b3 100644 --- a/momentum-core/tree/tree.go +++ b/momentum-core/tree/tree.go @@ -80,6 +80,9 @@ func (n *Node) Remove() { } } n.Parent.Children = newChilds + + n.Parent.RemoveYamlChild(n.Path) + n.Parent = nil } diff --git a/momentum-core/tree/yaml-node-factory.go b/momentum-core/tree/yaml-node-factory.go index cd9ed12..6521830 100644 --- a/momentum-core/tree/yaml-node-factory.go +++ b/momentum-core/tree/yaml-node-factory.go @@ -2,6 +2,7 @@ package tree import ( "errors" + "momentum-core/utils" "momentum-core/yaml" ) @@ -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 {