-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.go
224 lines (195 loc) · 5.07 KB
/
path.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package config
import (
"fmt"
"strings"
)
/*
Path provides a leaf node in a hierarchy of configuration parameters.
When scanning nested structs, for example, there will be a hierarchy such as
TypeNameA +> TypeNameB -> TypeNameC
|
+-> TypeNameD -> TypeNameE
A Path can refer to the edge nodes, TypeNameC or TypeNameE in this case. Each
will have an association with one parent node.
*/
type Path struct {
name string
parent *NodePath
}
// Name returns the name of node p.
func (p *Path) Name() string {
if p == nil {
return ""
}
return p.name
}
/*
Elements returns a list of names from the root node to p.
If the root *NodePath is unnamed, it will not be included in the list.
Otherwise, the first item will be the name of the root node, and the last will
be the name of p. E.g., for
NodePath()->NodePath(usr)->NodePath(bin)->Path(python)
Elements returns
[]string{"usr", "bin", "python"}
*/
func (p *Path) Elements() []string {
if p == nil {
return nil
}
path, i := p, 0
for ; path.parent != nil; path = &path.parent.Path {
i++
}
// if root path has a name, include it.
if path.name != "" {
i++
}
elems := make([]string, i)
for path, i = p, i-1; path.parent != nil; path, i = &path.parent.Path, i-1 {
elems[i] = path.name
}
if i == 0 {
elems[0] = path.name
}
return elems
}
// String returns a string for p by joining the result of p.Elements() with "->".
func (p *Path) String() string {
return strings.Join(p.Elements(), "->")
}
/*
PathCmp compares two Paths to determine ordering.
It returns -1 if a should order before b; 1 if a should order after b; or 0 if
a and b are equivalent.
*/
func PathCmp(a, b *Path) int {
ei, ej := a.Elements(), b.Elements()
for len(ei) != 0 && len(ej) != 0 {
if ei[0] < ej[0] {
return -1
}
if ei[0] > ej[0] {
return 1
}
ei, ej = ei[1:], ej[1:]
}
if len(ei) < len(ej) {
return -1
}
if len(ei) > len(ej) {
return 1
}
return 0
}
type child struct {
*NodePath
*Path
}
/*
NodePath represents an intermediate node in a configuration path.
It is associated with one parent NodePath, or none if it is a root. It is
also associated with 0 or more child NodePath or Path elements.
*/
type NodePath struct {
Path
children map[string]child
}
/*
NewRootPath returns a *NodePath that represents the root of a path hierarchy.
If name is the empty string, the root is unnamed. This is equivalent to the
zero value, which is also ready to use.
*/
func NewRootPath(name string) *NodePath {
return &NodePath{Path: Path{name: name}}
}
/*
NewPath returns a new Path but without adding it as a child of np.
The returned *Path will have np as its parent, but will not be locatable using
np.FindPath.
*/
func (np *NodePath) NewPath(name string) *Path {
return &Path{name: name, parent: np}
}
/*
NewNodePath returns a new NodePath but without adding it as a child of np.
The returned *NodePath will have np as its parent, but will not be locatable
using np.FindNodePath.
*/
func (np *NodePath) NewNodePath(name string) *NodePath {
return &NodePath{Path: Path{name: name, parent: np}}
}
/*
AddPath registers a child Path to np.
This makes the child locatable using np.FindPath.
*/
func (np *NodePath) AddPath(c *Path) *Path {
c.parent = np
if np.children == nil {
np.children = make(map[string]child, 1)
}
item := np.children[c.name]
if item.Path != nil {
panic(fmt.Sprintf("path %s already exists", c))
} else {
item.Path = c
}
np.children[c.name] = item
return c
}
/*
AddNodePath registers a child NodePath to np.
This makes the child locatable using np.FindNodePath.
*/
func (np *NodePath) AddNodePath(c *NodePath) *NodePath {
c.parent = np
if np.children == nil {
np.children = make(map[string]child, 1)
}
item := np.children[c.name]
if item.NodePath != nil {
panic(fmt.Sprintf("node path %s already exists", c))
} else {
item.NodePath = c
}
np.children[c.name] = item
return c
}
func (np *NodePath) find(elements ...string) child {
var item child
for i := range elements {
if np == nil { // shouldn't happen
return child{}
}
var ok bool
if item, ok = np.children[elements[i]]; !ok {
return child{}
}
np = item.NodePath
}
return item
}
/*
FindPath finds the given path.
The name of np is not considered, so the first parameter will be a child of
np. For example, in the hierarchy
NodePath()->NodePath(usr)->NodePath(bin)->Path(python)
Calling FindPath("usr", "bin", "python") on the root node will return
Path(python).
If the path is not found within np, or is not a leaf node (*Path), FindPath
returns nil.
*/
func (np *NodePath) FindPath(elements ...string) *Path {
return np.find(elements...).Path
}
/*
FindNodePath finds the given path.
The name of np is not considered, so the first parameter will be a child of
np. For example, in the hierarchy
NodePath()->NodePath(usr)->NodePath(bin)->Path(python)
Calling FindPath("usr", "bin") on the root node will return NodePath(bin).
If the path is not found within np, or is not an intermediate node (*NodePath),
FindNodePath returns nil.
*/
func (np *NodePath) FindNodePath(elements ...string) *NodePath {
return np.find(elements...).NodePath
}