tree.get does not include deleted branches
parent
77b057147c
commit
6a6d2ba822
|
|
@ -103,7 +103,7 @@ func (tree Tree) getRoot(withContent bool) (Branch, error) {
|
||||||
} else if entry.IsDir() {
|
} else if entry.IsDir() {
|
||||||
if branch, err := tree.WithRoot(path.Join(tree.root, entry.Name())).getRoot(withContent); err != nil {
|
if branch, err := tree.WithRoot(path.Join(tree.root, entry.Name())).getRoot(withContent); err != nil {
|
||||||
return Branch{}, err
|
return Branch{}, err
|
||||||
} else {
|
} else if !branch.Leaf.Deleted && !branch.IsZero() {
|
||||||
m.Branches[entry.Name()] = branch
|
m.Branches[entry.Name()] = branch
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -145,6 +145,21 @@ func (tree Tree) Put(id []string, input Leaf) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tree Tree) Del(id []string) error {
|
func (tree Tree) Del(id []string) error {
|
||||||
|
got, err := tree.Get(id)
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if got.Deleted {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
got.Deleted = true
|
||||||
|
return tree.Put(id, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (tree Tree) HardDel(id []string) error {
|
||||||
os.RemoveAll(tree.toDir(id))
|
os.RemoveAll(tree.toDir(id))
|
||||||
tree.cachedRoot = Branch{}
|
tree.cachedRoot = Branch{}
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,30 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestTreeDel(t *testing.T) {
|
||||||
|
tree := NewTree(t.TempDir())
|
||||||
|
if err := tree.Put([]string{"id"}, Leaf{}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := tree.Put([]string{"id", "subid"}, Leaf{}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tree.Del([]string{"id"}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if got, err := tree.Get([]string{"id"}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if !got.Deleted {
|
||||||
|
t.Fatal(got)
|
||||||
|
}
|
||||||
|
|
||||||
|
if root, err := tree.GetRoot(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
} else if len(root.Branches) > 0 {
|
||||||
|
t.Fatal(root.Branches)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTreeCrud(t *testing.T) {
|
func TestTreeCrud(t *testing.T) {
|
||||||
tree := NewTree(t.TempDir())
|
tree := NewTree(t.TempDir())
|
||||||
|
|
||||||
|
|
@ -22,7 +46,7 @@ func TestTreeCrud(t *testing.T) {
|
||||||
|
|
||||||
want := Leaf{
|
want := Leaf{
|
||||||
Title: "leaf title",
|
Title: "leaf title",
|
||||||
Deleted: true,
|
Deleted: false,
|
||||||
Content: "leaf content",
|
Content: "leaf content",
|
||||||
}
|
}
|
||||||
if err := tree.Put([]string{"id"}, want); err != nil {
|
if err := tree.Put([]string{"id"}, want); err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue