This commit is contained in:
Bel LaPointe
2022-02-16 07:58:34 -07:00
parent 552a3f46ff
commit 63caf9ed03
8 changed files with 199 additions and 211 deletions

View File

@@ -10,37 +10,23 @@ import (
type Branch struct {
Leaf Leaf
Branches map[string]Branch
Branches map[ID]Branch
}
func (branch Branch) IsZero() bool {
return branch.Leaf == (Leaf{}) && len(branch.Branches) == 0
}
func (branch Branch) Find(baseId string) ([]string, bool) {
if _, ok := branch.Branches[baseId]; ok {
return []string{baseId}, true
}
for pid, child := range branch.Branches {
if subids, ok := child.Find(baseId); ok {
return append([]string{pid}, subids...), true
}
}
return nil, false
func (branch Branch) ForEach(foo func(ID, Leaf) error) error {
return branch.forEach(NewID(""), foo)
}
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 {
func (branch Branch) forEach(preid ID, foo func(ID, 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 {
if err := child.forEach(preid.Push(string(id)), foo); err != nil {
return err
}
}
@@ -80,31 +66,23 @@ func (tree Tree) WithRoot(root string) Tree {
return tree
}
func (tree Tree) Find(baseId string) ([]string, bool) {
root, err := tree.GetRoot()
if err != nil {
return nil, false
}
return root.Find(baseId)
}
func (tree Tree) GetRootMeta() (Branch, error) {
return tree.getRoot(false, false)
return tree.getRoot(NewID(""), false, false)
}
func (tree Tree) GetRoot() (Branch, error) {
if !tree.cachedRoot.IsZero() {
return tree.cachedRoot, nil
}
got, err := tree.getRoot(true, false)
got, err := tree.getRoot(NewID(""), true, false)
if err == nil {
tree.cachedRoot = got
}
return got, err
}
func (tree Tree) getRoot(withContent, withDeleted bool) (Branch, error) {
m := Branch{Branches: map[string]Branch{}}
func (tree Tree) getRoot(pid ID, withContent, withDeleted bool) (Branch, error) {
m := Branch{Branches: map[ID]Branch{}}
entries, err := os.ReadDir(tree.root)
if os.IsNotExist(err) {
return m, nil
@@ -127,25 +105,25 @@ func (tree Tree) getRoot(withContent, withDeleted bool) (Branch, error) {
}
} else if entry.IsDir() {
subtree := tree.WithRoot(path.Join(tree.root, entry.Name()))
if branch, err := subtree.getRoot(withContent, withDeleted); err != nil {
if branch, err := subtree.getRoot(pid.Push(entry.Name()), withContent, withDeleted); err != nil {
return Branch{}, err
} else if !branch.IsZero() && (!branch.Leaf.Deleted || withDeleted) {
m.Branches[entry.Name()] = branch
m.Branches[pid.Push(entry.Name())] = branch
}
}
}
return m, nil
}
func (tree Tree) toDir(id []string) string {
func (tree Tree) toDir(id ID) string {
return path.Dir(tree.toData(id))
}
func (tree Tree) toData(id []string) string {
return path.Join(tree.root, path.Join(id...), "data.yaml")
func (tree Tree) toData(id ID) string {
return path.Join(tree.root, string(id), "data.yaml")
}
func (tree Tree) Put(id []string, input Leaf) error {
func (tree Tree) Put(id ID, input Leaf) error {
if _, err := os.Stat(tree.toData(id)); os.IsNotExist(err) {
b, err := yaml.Marshal(Leaf{})
if err != nil {
@@ -170,7 +148,7 @@ func (tree Tree) Put(id []string, input Leaf) error {
return nil
}
func (tree Tree) Del(id []string) error {
func (tree Tree) Del(id ID) error {
got, err := tree.Get(id)
if os.IsNotExist(err) {
return nil
@@ -185,13 +163,13 @@ func (tree Tree) Del(id []string) error {
return tree.Put(id, got)
}
func (tree Tree) HardDel(id []string) error {
func (tree Tree) HardDel(id ID) error {
os.RemoveAll(tree.toDir(id))
tree.cachedRoot = Branch{}
return nil
}
func (tree Tree) Get(id []string) (Leaf, error) {
func (tree Tree) Get(id ID) (Leaf, error) {
f, err := os.Open(tree.toData(id))
if err != nil {
return Leaf{}, err