forked from andreaskoch/allmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filetree.go
81 lines (62 loc) · 1.43 KB
/
filetree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2014 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package filetreerenderer
import (
"github.com/andreaskoch/allmark/common/route"
"github.com/andreaskoch/allmark/common/tree"
"github.com/andreaskoch/allmark/model"
)
type FileNode struct {
*tree.Node
}
func (f *FileNode) Value() *model.File {
return nodeToFile(f.Node)
}
func (f *FileNode) Children() []*FileNode {
children := make([]*FileNode, 0)
for _, child := range f.Node.Children() {
children = append(children, &FileNode{child})
}
return children
}
func newTree() *FileTree {
return &FileTree{
*tree.New("", nil),
}
}
type FileTree struct {
tree.Tree
}
func (nodeTree *FileTree) Root() *model.File {
rootNode := nodeTree.Tree.Root()
if rootNode == nil {
return nil
}
return nodeToFile(rootNode)
}
func (nodeTree *FileTree) Insert(file *model.File) {
if file == nil {
return
}
// convert the route to a path
path := tree.RouteToPath(file.Route())
nodeTree.Tree.Insert(path, file)
}
func (nodeTree *FileTree) GetNode(route route.Route) *FileNode {
// convert the route to a path
path := tree.RouteToPath(route)
// locate the node
node := nodeTree.Tree.GetNode(path)
if node == nil {
return nil
}
return &FileNode{node}
}
func nodeToFile(node *tree.Node) *model.File {
val := node.Value()
if val == nil {
return nil
}
return val.(*model.File)
}