-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
269 lines (244 loc) · 5.22 KB
/
tree.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package cotton
import (
"fmt"
"strings"
)
const (
nodeStatic = iota
nodeEmpty
nodeParam
nodeCatchAll
keyParam = ":_"
)
type (
tree struct {
root *node
}
node struct {
key string
fullpath string
nodeType byte
children map[string]*node
isRealNode bool
handler HandlerFunc
middleware []HandlerFunc
paramName string
}
resultFind struct {
node *node
params map[string]string
}
)
// func init() {
// paramsPool.New = func() interface{} {
// return make(map[string]string)
// }
// }
func (t *tree) add(path string, handler HandlerFunc) *node {
if len(path) == 0 || path[0] != '/' {
panic(fmt.Errorf("path [%s] must start with /", path))
}
for i, j := 0, len(path); i < j; i++ {
if path[i] == '*' {
pathSub := path[i+1:]
for ii, jj := 0, len(pathSub); ii < jj; ii++ {
if pathSub[ii] == '/' {
panic(fmt.Errorf("action [*%s] must end of path [%s]", pathSub[:ii], path))
}
}
break
}
}
var nodeCurrent = t.root
var numParams = 0
var start, lenstr = 1, len(path)
if path != nodeCurrent.key {
for i := start; i < lenstr; i++ {
if path[i] == '/' {
key := path[start:i]
if key == "" {
panic(fmt.Errorf("path [%s] has multiple '/'", path))
}
if strings.Index(key, " ") > -1 {
panic(fmt.Errorf("path [%s] has space", path))
}
nodeCurrent.insertNode(key, path)
child, ok := nodeCurrent.children[key]
if !ok {
child = nodeCurrent.children[keyParam]
numParams++
} else {
for _, nc := range nodeCurrent.children {
if nc.nodeType == nodeCatchAll {
panic(fmt.Errorf("path [%s] conflicts with [%s]", path, nc.fullpath))
}
}
}
nodeCurrent = child
start = i + 1
continue
}
}
if start == lenstr {
start--
}
key := path[start:]
nodeCurrent.insertNode(key, path)
child, ok := nodeCurrent.children[key]
if !ok {
child = nodeCurrent.children[keyParam]
for _, c := range nodeCurrent.children {
if c.isRealNode && c.nodeType == nodeStatic && c.key != "/" {
panic(fmt.Errorf("path [%s] conflicts with [%s]", path, c.fullpath))
}
}
numParams++
} else {
if c, ok := nodeCurrent.children[keyParam]; ok && c.isRealNode {
panic(fmt.Errorf("path [%s] conflicts with [%s]", path, c.fullpath))
}
}
nodeCurrent = child
}
nodeCurrent.isRealNode = true
nodeCurrent.handler = handler
nodeCurrent.fullpath = path
return nodeCurrent
}
func (n *node) print(deep int) {
key := n.key
if n.nodeType == nodeParam {
key = ":" + n.paramName
} else if n.nodeType == nodeCatchAll {
key = "*" + n.paramName
}
fmt.Printf("%s %d %s %v\n", strings.Repeat(" ", deep), deep, key, n.isRealNode)
for _, n := range n.children {
n.print(deep + 1)
}
}
func (n *node) insertNode(key string, fullpath string) {
if n.nodeType == nodeCatchAll {
panic(fmt.Errorf("action [%s] must end of path [%s]", n.key, fullpath))
}
keyCheck := key
if keyCheck[0] == ':' || keyCheck[0] == '*' {
keyCheck = keyParam
if cCheck, ok := n.children[keyParam]; ok && cCheck.paramName != key[1:] {
var fullpathV = cCheck.fullpath
if !cCheck.isRealNode {
for _, v := range n.children {
if v.key == "/" {
continue
}
vCheck := v
for !vCheck.isRealNode {
for _, vv := range v.children {
vCheck = vv
break
}
}
fullpathV = vCheck.fullpath
}
}
panic(fmt.Errorf("[%s] in path [%s] conflicts with [%s]", key, fullpath, fullpathV))
}
}
if _, ok := n.children[keyCheck]; !ok {
n.children[keyCheck] = newNode(key)
}
}
func (n *node) find(path string) (result resultFind) {
result.params = make(map[string]string)
if path == "/" && n.key == path {
if n.isRealNode {
result.node = n
}
return
}
pathNew := []byte{}
has := false
for i, j := 0, len(path); i < j; i++ {
if path[i] == '/' {
if !has {
has = true
pathNew = append(pathNew, path[i])
}
} else {
has = false
pathNew = append(pathNew, path[i])
}
}
path = string(pathNew)
var child *node
var ok bool
var start, lenstr = 1, len(path)
for i := start; i < lenstr; i++ {
if path[i] == '/' {
key := path[start:i]
if key == "" {
start = i + 1
continue
}
child, ok = n.children[key]
if !ok {
child, ok = n.children[keyParam]
if ok {
if child.nodeType == nodeCatchAll {
result.params[child.paramName] = path[start:]
result.node = child
return
}
result.params[child.paramName] = key
}
}
if !ok {
return
}
n = child
start = i + 1
}
}
if start == lenstr {
start--
}
key := path[start:]
child, ok = n.children[key]
if !ok && key != "/" {
child, ok = n.children[keyParam]
if ok {
result.params[child.paramName] = key
}
}
if ok && child.isRealNode {
result.node = child
}
return
}
func newTree() *tree {
t := new(tree)
n := newNode("/")
t.root = n
return t
}
func newNode(key string) *node {
n := new(node)
n.nodeType = nodeEmpty
if len(key) > 0 {
switch key[0] {
case ':':
n.nodeType = nodeParam
n.paramName = key[1:]
n.key = keyParam
case '*':
n.nodeType = nodeCatchAll
n.paramName = key[1:]
n.key = keyParam
default:
n.nodeType = nodeStatic
n.key = key
}
}
n.children = make(map[string]*node)
return n
}