-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
107 lines (85 loc) · 2.39 KB
/
node.go
File metadata and controls
107 lines (85 loc) · 2.39 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package ovskv
import (
"fmt"
"path"
"github.com/ebay/libovsdb"
)
// Taken from etcd. Thanks.
// A key-value pair will have a string value
// A directory will have a children map
type node struct {
Path string
CreatedIndex uint64
ModifiedIndex uint64
Parent *node `json:"-"` // should not encode this field! avoid circular dependency.
Data *libovsdb.ResultRow // for key-value pairs
Children map[string]*node // for directory
}
// newKV creates a Key-Value pair
func newKV(nodePath string, data *libovsdb.ResultRow, createdIndex uint64, parent *node) *node {
return &node{
Path: nodePath,
CreatedIndex: createdIndex,
ModifiedIndex: createdIndex,
Parent: parent,
Data: data,
}
}
// newDir creates a directory
func newDir(nodePath string, createdIndex uint64, parent *node) *node {
return &node{
Path: nodePath,
CreatedIndex: createdIndex,
ModifiedIndex: createdIndex,
Parent: parent,
Children: make(map[string]*node),
}
}
func (n *node) UUID() string {
return (*n.Data)["_uuid"].(libovsdb.UUID).GoUUID
}
func (n *node) Map() map[interface {}]interface {} {
return (*n.Data)["data"].(libovsdb.OvsMap).GoMap
}
func (n *node) Value() string {
return (*n.Data)["data"].(libovsdb.OvsMap).GoMap["v"].(string)
}
func (n *node) Key() string {
if n.IsDir() {
return n.Path
}
return pathKey((*n.Data)["path"])
}
// IsDir function checks whether the node is a directory.
// If the node is a directory, the function will return true.
// Otherwise the function will return false.
func (n *node) IsDir() bool {
return n.Children != nil
}
// GetChild function returns the child node under the directory node.
// On success, it returns the file node
func (n *node) GetChild(name string) (*node, error) {
if !n.IsDir() {
return nil, fmt.Errorf("not a directory")
}
child, ok := n.Children[name]
if ok {
return child, nil
}
return nil, nil
}
// Add function adds a node to the receiver node.
// If the receiver is not a directory, a "Not A Directory" error will be returned.
// If there is an existing node with the same name under the directory, a "Already Exist"
// error will be returned
func (n *node) Add(child *node) error {
if !n.IsDir() {
return fmt.Errorf("not a directory")
}
_, name := path.Split(child.Path)
if _, ok := n.Children[name]; ok {
return fmt.Errorf("node exists")
}
n.Children[name] = child
return nil
}