-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
56 lines (48 loc) · 1.09 KB
/
node.go
File metadata and controls
56 lines (48 loc) · 1.09 KB
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
package main
// This file implements the Nodes of a filesystem tree.
import (
"fmt"
"io"
)
// Node in the Library's file tree.
type Node struct {
payload interface{}
parent *Node
children []*Node
}
// Walks through the tree and applies function f to each Node.
func (this *Node) Walk(f func(*Node)) {
f(this)
for _, c := range this.children {
c.Walk(f)
}
}
// Construct new node with given parent
// and link the parent-child pointers.
func (parent *Node) NewChild(file string) (child *Node) {
child = &Node{file, parent, nil}
parent.children = append(parent.children, child)
return
}
// Returns full path represented by this node.
func (n *Node) String() string {
str := fmt.Sprint(n.payload)
for p := n.parent; p != nil; p = p.parent {
str = fmt.Sprint(p.payload, str)
}
return str
}
// Write full path to out.
func (this *Node) WriteTo(out io.Writer) (n int, err error) {
n, err = fmt.Fprintln(out, this)
return
}
// Get a child by its file string.
func (n *Node) Child(file string) *Node {
for _, c := range n.children {
if c.payload == file {
return c
}
}
return nil
}