almost there

This commit is contained in:
Bel LaPointe
2022-02-09 12:30:12 -07:00
parent c4b63825aa
commit f41b34aa5c
3 changed files with 142 additions and 45 deletions

View File

@@ -9,8 +9,8 @@ import (
)
type Branch struct {
Leaf Leaf
Branches map[string]Branch
Leaf Leaf `json:"Leaf,omitempty"`
Branches map[string]Branch `json:"Branches,omitempty"`
}
func (branch Branch) IsZero() bool {
@@ -29,6 +29,24 @@ func (branch Branch) Find(baseId string) ([]string, bool) {
return nil, false
}
func (branch Branch) ForEach(foo func([]string, Leaf) error) error {
return branch.forEach([]string{}, foo)
}
func (branch Branch) forEach(preid []string, foo func([]string, Leaf) error) error {
if err := foo(preid, branch.Leaf); err != nil {
return err
}
postid := append(preid, "")
for id, child := range branch.Branches {
postid[len(postid)-1] = id
if err := child.forEach(postid, foo); err != nil {
return err
}
}
return nil
}
type Leaf struct {
Title string
Deleted bool
@@ -39,6 +57,9 @@ func (base Leaf) Merge(updated Leaf) Leaf {
if updated.Title != "" {
base.Title = updated.Title
}
if base.Title == "" {
base.Title = "Untitled"
}
base.Deleted = updated.Deleted
base.Content = updated.Content
return base
@@ -55,6 +76,7 @@ func NewTree(root string) Tree {
func (tree Tree) WithRoot(root string) Tree {
tree.root = root
tree.cachedRoot = Branch{}
return tree
}