tree ids all full paths

master
bel 2022-02-15 21:16:41 -07:00
parent 04defa3999
commit 24acc02dc7
3 changed files with 23 additions and 6 deletions

View File

@ -210,6 +210,23 @@ func TestServerPutTreeGetFile(t *testing.T) {
if !bytes.Contains(w.Body.Bytes(), []byte(`{"Title":"my title","Deleted":false,"Content":"`)) {
t.Fatal(w)
}
var branch Branch
if err := json.NewDecoder(w.Body).Decode(&branch); err != nil {
t.Fatal(err)
}
t.Logf("TODO: %+v", branch)
if branch.Leaf != (Leaf{}) {
t.Error(branch.Leaf)
}
if parent, ok := branch.Branches["my pid"]; !ok {
t.Error(ok, branch)
} else if parent.Leaf.Title != "Untitled" {
t.Error(parent.Leaf)
} else if child, ok := parent.Branches[NewID(id)]; !ok {
t.Error(ok, NewID("my pid").Push(id), parent)
} else if child.Leaf.Title != "my title" {
t.Error(child.Leaf)
}
})
t.Run("get", func(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/"+url.PathEscape(id), nil)

View File

@ -67,21 +67,21 @@ func (tree Tree) WithRoot(root string) Tree {
}
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) {
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) {
@ -105,10 +105,10 @@ 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[NewID(entry.Name())] = branch
m.Branches[pid.Push(entry.Name())] = branch
}
}
}

View File

@ -29,7 +29,7 @@ func TestTreeDel(t *testing.T) {
t.Fatal(root.Branches)
}
if root, err := tree.getRoot(false, true); err != nil {
if root, err := tree.getRoot(NewID(""), false, true); err != nil {
t.Fatal(err)
} else if len(root.Branches) != 1 {
t.Fatal(root.Branches)