-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
trie.go
351 lines (295 loc) · 8.57 KB
/
trie.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package muxie
import (
"net/http"
"strings"
)
const (
// ParamStart is the character, as a string, which a path pattern starts to define its named parameter.
ParamStart = ":"
// WildcardParamStart is the character, as a string, which a path pattern starts to define its named parameter for wildcards.
// It allows everything else after that path prefix
// but the Trie checks for static paths and named parameters before that in order to support everything that other implementations do not,
// and if nothing else found then it tries to find the closest wildcard path(super and unique).
WildcardParamStart = "*"
)
// Trie contains the main logic for adding and searching nodes for path segments.
// It supports wildcard and named path parameters.
// Trie supports very coblex and useful path patterns for routes.
// The Trie checks for static paths(path without : or *) and named parameters before that in order to support everything that other implementations do not,
// and if nothing else found then it tries to find the closest wildcard path(super and unique).
type Trie struct {
root *Node
// if true then it will handle any path if not other parent wildcard exists,
// so even 404 (on http services) is up to it, see Trie#Insert.
hasRootWildcard bool
hasRootSlash bool
}
// NewTrie returns a new, empty Trie.
// It is only useful for end-developers that want to design their own mux/router based on my trie implementation.
//
// See `Trie`
func NewTrie() *Trie {
return &Trie{
root: NewNode(),
hasRootWildcard: false,
}
}
// InsertOption is just a function which accepts a pointer to a Node which can alt its `Handler`, `Tag` and `Data` fields.
//
// See `WithHandler`, `WithTag` and `WithData`.
type InsertOption func(*Node)
// WithHandler sets the node's `Handler` field (useful for HTTP).
func WithHandler(handler http.Handler) InsertOption {
if handler == nil {
panic("muxie/WithHandler: empty handler")
}
return func(n *Node) {
if n.Handler == nil {
n.Handler = handler
}
}
}
// WithTag sets the node's `Tag` field (may be useful for HTTP).
func WithTag(tag string) InsertOption {
return func(n *Node) {
if n.Tag == "" {
n.Tag = tag
}
}
}
// WithData sets the node's optionally `Data` field.
func WithData(data interface{}) InsertOption {
return func(n *Node) {
// data can be replaced.
n.Data = data
}
}
// Insert adds a node to the trie.
func (t *Trie) Insert(pattern string, options ...InsertOption) {
if pattern == "" {
panic("muxie/trie#Insert: empty pattern")
}
n := t.insert(pattern, "", nil, nil)
for _, opt := range options {
opt(n)
}
}
const (
pathSep = "/"
pathSepB = '/'
)
func slowPathSplit(path string) []string {
if path == pathSep {
return []string{pathSep}
}
// remove last sep if any.
if path[len(path)-1] == pathSepB {
path = path[:len(path)-1]
}
return strings.Split(path, pathSep)[1:]
}
func resolveStaticPart(key string) string {
i := strings.Index(key, ParamStart)
if i == -1 {
i = strings.Index(key, WildcardParamStart)
}
if i == -1 {
i = len(key)
}
return key[:i]
}
func (t *Trie) insert(key, tag string, optionalData interface{}, handler http.Handler) *Node {
input := slowPathSplit(key)
n := t.root
if key == pathSep {
t.hasRootSlash = true
}
var paramKeys []string
for _, s := range input {
c := s[0]
if isParam, isWildcard := c == ParamStart[0], c == WildcardParamStart[0]; isParam || isWildcard {
n.hasDynamicChild = true
paramKeys = append(paramKeys, s[1:]) // without : or *.
// if node has already a wildcard, don't force a value, check for true only.
if isParam {
n.childNamedParameter = true
s = ParamStart
}
if isWildcard {
n.childWildcardParameter = true
s = WildcardParamStart
if t.root == n {
t.hasRootWildcard = true
}
}
}
if !n.hasChild(s) {
child := NewNode()
n.addChild(s, child)
}
n = n.getChild(s)
}
n.Tag = tag
n.Handler = handler
n.Data = optionalData
n.paramKeys = paramKeys
n.key = key
n.staticKey = resolveStaticPart(key)
n.end = true
return n
}
// SearchPrefix returns the last node which holds the key which starts with "prefix".
func (t *Trie) SearchPrefix(prefix string) *Node {
input := slowPathSplit(prefix)
n := t.root
for i := 0; i < len(input); i++ {
s := input[i]
if child := n.getChild(s); child != nil {
n = child
continue
}
return nil
}
return n
}
// Parents returns the list of nodes that a node with "prefix" key belongs to.
func (t *Trie) Parents(prefix string) (parents []*Node) {
n := t.SearchPrefix(prefix)
if n != nil {
// without this node.
n = n.Parent()
for {
if n == nil {
break
}
if n.IsEnd() {
parents = append(parents, n)
}
n = n.Parent()
}
}
return
}
// HasPrefix returns true if "prefix" is found inside the registered nodes.
func (t *Trie) HasPrefix(prefix string) bool {
return t.SearchPrefix(prefix) != nil
}
// Autocomplete returns the keys that starts with "prefix",
// this is useful for custom search-engines built on top of my trie implementation.
func (t *Trie) Autocomplete(prefix string, sorter NodeKeysSorter) (list []string) {
n := t.SearchPrefix(prefix)
if n != nil {
list = n.Keys(sorter)
}
return
}
// ParamsSetter is the interface which should be implemented by the
// params writer for `Search` in order to store the found named path parameters, if any.
type ParamsSetter interface {
Set(string, string)
}
// Search is the most important part of the Trie.
// It will try to find the responsible node for a specific query (or a request path for HTTP endpoints).
//
// Search supports searching for static paths(path without : or *) and paths that contain
// named parameters or wildcards.
// Priority as:
// 1. static paths
// 2. named parameters with ":"
// 3. wildcards
// 4. closest wildcard if not found, if any
// 5. root wildcard
func (t *Trie) Search(q string, params ParamsSetter) *Node {
end := len(q)
if end == 0 || (end == 1 && q[0] == pathSepB) {
// fixes only root wildcard but no / registered at.
if t.hasRootSlash {
return t.root.getChild(pathSep)
} else if t.hasRootWildcard {
// no need to going through setting parameters, this one has not but it is wildcard.
return t.root.getChild(WildcardParamStart)
}
return nil
}
n := t.root
start := 1
i := 1
var paramValues []string
for {
if i == end || q[i] == pathSepB {
if child := n.getChild(q[start:i]); child != nil {
n = child
} else if n.childNamedParameter { // && n.childWildcardParameter == false {
n = n.getChild(ParamStart)
if ln := len(paramValues); cap(paramValues) > ln {
paramValues = paramValues[:ln+1]
paramValues[ln] = q[start:i]
} else {
paramValues = append(paramValues, q[start:i])
}
} else if n.childWildcardParameter {
n = n.getChild(WildcardParamStart)
if ln := len(paramValues); cap(paramValues) > ln {
paramValues = paramValues[:ln+1]
paramValues[ln] = q[start:]
} else {
paramValues = append(paramValues, q[start:])
}
break
} else {
n = n.findClosestParentWildcardNode()
if n != nil {
// means that it has :param/static and *wildcard, we go trhough the :param
// but the next path segment is not the /static, so go back to *wildcard
// instead of not found.
//
// Fixes:
// /hello/*p
// /hello/:p1/static/:p2
// req: http://localhost:8080/hello/dsadsa/static/dsadsa => found
// req: http://localhost:8080/hello/dsadsa => but not found!
// and
// /second/wild/*p
// /second/wild/static/otherstatic/
// req: /second/wild/static/otherstatic/random => but not found!
params.Set(n.paramKeys[0], q[len(n.staticKey):])
return n
}
return nil
}
if i == end {
break
}
i++
start = i
continue
}
i++
}
if n == nil || !n.end {
if n != nil { // we need it on both places, on last segment (below) or on the first unnknown (above).
if n = n.findClosestParentWildcardNode(); n != nil {
params.Set(n.paramKeys[0], q[len(n.staticKey):])
return n
}
}
if t.hasRootWildcard {
// that's the case for root wildcard, tests are passing
// even without it but stick with it for reference.
// Note ote that something like:
// Routes: /other2/*myparam and /other2/static
// Reqs: /other2/staticed will be handled
// by the /other2/*myparam and not the root wildcard (see above), which is what we want.
n = t.root.getChild(WildcardParamStart)
params.Set(n.paramKeys[0], q[1:])
return n
}
return nil
}
for i, paramValue := range paramValues {
if len(n.paramKeys) > i {
params.Set(n.paramKeys[i], paramValue)
}
}
return n
}