forked from gogearbox/gearbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
175 lines (150 loc) · 3.84 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
package gearbox
import (
"strings"
)
type nodeType uint8
const (
static nodeType = iota
root
param
catchAll
)
type node struct {
path string
param *node
children map[string]*node
nType nodeType
handlers handlersChain
}
// addRoute adds a node with the provided handlers to the path
func (n *node) addRoute(path string, handlers handlersChain) {
currentNode := n
originalPath := path
path = path[1:]
paramNames := make(map[string]bool)
for {
pathLen := len(path)
if pathLen == 0 {
if currentNode.handlers != nil {
panic("handlers are already registered for path '" + originalPath + "'")
}
// Make a deep copy of handler's references
routeHandlers := make(handlersChain, len(handlers))
copy(routeHandlers, handlers)
currentNode.handlers = routeHandlers
break
}
segmentDelimiter := strings.Index(path, "/")
if segmentDelimiter == -1 {
segmentDelimiter = pathLen
}
pathSegment := path[:segmentDelimiter]
if pathSegment[0] == ':' || pathSegment[0] == '*' {
// Parameter
if len(currentNode.children) > 0 {
panic("parameter " + pathSegment +
" conflicts with existing static children in path '" +
originalPath + "'")
}
if currentNode.param != nil {
if currentNode.param.path[0] == '*' {
panic("parameter " + pathSegment +
" conflicts with catch all (*) route in path '" +
originalPath + "'")
} else if currentNode.param.path != pathSegment {
panic("parameter " + pathSegment + " in new path '" +
originalPath + "' conflicts with existing wildcard '" +
currentNode.param.path)
}
}
if currentNode.param == nil {
var nType nodeType
if pathSegment[0] == '*' {
nType = catchAll
if pathLen > 1 {
panic("catch all (*) routes are only allowed " +
"at the end of the path in path '" +
originalPath + "'")
}
} else {
nType = param
if _, ok := paramNames[pathSegment]; ok {
panic("parameter " + pathSegment +
" must be unique in path '" + originalPath + "'")
} else {
paramNames[pathSegment] = true
}
}
currentNode.param = &node{
path: pathSegment,
nType: nType,
children: make(map[string]*node),
}
}
currentNode = currentNode.param
} else {
// Static
if currentNode.param != nil {
panic(pathSegment + "' conflicts with existing parameter " +
currentNode.param.path + " in path '" + originalPath + "'")
}
if child, ok := currentNode.children[pathSegment]; ok {
currentNode = child
} else {
child = &node{
path: pathSegment,
nType: static,
children: make(map[string]*node),
}
currentNode.children[pathSegment] = child
currentNode = child
}
}
if pathLen > segmentDelimiter {
segmentDelimiter++
}
path = path[segmentDelimiter:]
}
}
// matchRoute returns handlers registered with the given path
func (n *node) matchRoute(path string, ctx *context) handlersChain {
pathLen := len(path)
if pathLen > 0 && path[0] != '/' {
return nil
}
currentNode := n
path = path[1:]
for {
pathLen = len(path)
if pathLen == 0 || currentNode.nType == catchAll {
return currentNode.handlers
}
segmentDelimiter := strings.Index(path, "/")
if segmentDelimiter == -1 {
segmentDelimiter = pathLen
}
pathSegment := path[:segmentDelimiter]
if pathLen > segmentDelimiter {
segmentDelimiter++
}
path = path[segmentDelimiter:]
if currentNode.param != nil {
currentNode = currentNode.param
ctx.paramValues[currentNode.path[1:]] = pathSegment
continue
}
if child, ok := currentNode.children[pathSegment]; ok {
currentNode = child
continue
}
return nil
}
}
// createRootNode creates an instance of node with root type
func createRootNode() *node {
return &node{
nType: root,
path: "/",
children: make(map[string]*node),
}
}