-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
376 lines (307 loc) · 9.33 KB
/
main.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package main
import (
"log"
"strings"
)
type router struct {
name, pattern string
handler interface{}
// if {param}, what name was it assigned
param string
// method to run, and filter to run before any method
method, filter string
isRegex, depth int
}
type path struct {
router
node map[rune]map[string]*path
}
var Router *path
func newRouter() *path {
log.SetFlags(log.Lshortfile)
return &path{
router: router{
name: "root",
pattern: "/",
handler: "root hander for the root or default (\"/\") route",
},
}
}
func getCharCodeAt(s string, n int) rune {
for i, r := range s {
if i == n {
return r
}
}
return 0
}
func main() {
Router := newRouter()
addNodeTest(Router)
checkForNodesTest(Router)
}
func addNodeTest(r *path) {
// Create a node
np := &path{
router: router{
name: "user-image-id",
pattern: "/user/{param}/images/{id}",
handler: "/{id} handler for route /user/{param}/images",
},
node: make(map[rune]map[string]*path),
}
np1 := &path{
router: router{
name: "user-image-id",
pattern: "/user/{param}",
handler: "/{param} handler for route /user/",
},
node: make(map[rune]map[string]*path),
}
// Try to add the node to thr route
_, ok := r.addNode(np)
// Test if you will find the route
pattern := "/user/{param}/images/{id}"
p, lp := r.getNode(pattern)
log.Printf("The NODE: %v | lvl: %d \n", p.router.handler, lp)
log.Printf("<==============================| the node has been added |================================>\n\n%q | %v\n\n", r, ok)
_, ok1 := r.addNode(np1)
pattern1 := "/user/{param}"
p1, lp1 := r.getNode(pattern1)
log.Printf("The NODE: %v | lvl: %d \n", p1.router.handler, lp1)
log.Printf("<==============================| the node has been added |================================>\n\n%q | %v\n\n", r, ok1)
}
func checkForNodesTest(r *path) {
h1 := router{
name: "user",
pattern: "/user/{name}",
handler: "/user handler here",
}
h2 := router{
name: "{name}",
pattern: "/user/{name}",
handler: "/{name} handler for the route /user",
}
h3 := router{
name: "images",
pattern: "/user/{name}/images",
handler: "/images handler for the route /user/{name}",
}
h4 := router{
name: "image-id",
pattern: "/user/{name}/images/{id}",
handler: "/{id} handler for the route /user/{name}/images",
}
h5 := router{
name: "/users",
pattern: "/users",
handler: "/users handler here",
}
h6 := router{
name: "users-category",
pattern: "/users/{category}",
handler: "/{category} handler for the route /user[s]",
}
// r.router = hr
r.node = map[rune]map[string]*path{
117: {
"user": &path{
router: h1,
node: map[rune]map[string]*path{
47: {
"{param}": &path{
router: h2,
node: map[rune]map[string]*path{
105: {
"images": &path{
router: h3,
node: map[rune]map[string]*path{
47: {
"{param}": &path{
router: h4,
},
},
},
},
},
},
},
},
},
},
"users": &path{
router: h5,
node: map[rune]map[string]*path{
47: {
"{param}": &path{
router: h6,
},
},
},
},
},
}
rt, lr := r.getNode("/")
a, la := r.getNode("/users/{category}")
b, lb := r.getNode("/user/{name}")
c, lc := r.getNode("/user/{name}/images")
d, ld := r.getNode("/user/{name}/images/{id}")
log.Printf("%v | lvl: %d \n", d.router.handler, ld)
// if you change the handler, and thus it also changes in the tree
// d.router.handler = "changed at the bottom"
dc, ldc := r.getNode("/user/eugene/images/15")
log.Printf("%v | lvl: %d \n", dc.router.handler, ldc)
log.Printf("%v | lvl: %d \n", rt.router.handler, lr)
log.Printf("%v | lvl: %d \n", a.router.handler, la)
log.Printf("%v | lvl: %d \n", b.router.handler, lb)
log.Printf("%v | lvl: %d \n", c.router.handler, lc)
log.Printf("%v | lvl: %d \n", d.router.handler, ld)
}
func transverseNode(branch *path, part string) (*path, bool) {
// get the charcode of the 1st character of the route part
charcode := getCharCodeAt(part, 0)
log.Printf(">> Character code: %v for word: %q\n", charcode, part)
// if it a root route request ("")
if charcode == 0 {
log.Println("This is a root route request.")
return branch, true
}
// get the pathmap of the Character code
pathmap, ok := branch.node[charcode]
// now check if it's node exists
if ok {
// if the node exists, then does it's path details exist
if path, ok := pathmap[part]; ok {
// if yes return it
return path, true
}
// it's path details do not exist
// return branch, false
}
log.Println("<========================= Route has not been found, maybe it is dymanic =========================>")
// If no return has happened
// then no map was found, try match for a {param} like pathmap
// "{" charcode 47 as the holder
if dynamicmap, ok := branch.node[47]; ok {
log.Println("<========================= In here ===========================>")
// if yes, the get the dynamic route pathmap map
if path, ok := dynamicmap["{param}"]; ok {
// if yes return it
return path, true
}
}
// If not return false, with the branch itself
// false bool will aid in breaking the loop
return branch, false
}
// get a specific route node
func (p *path) getNode(pattern string) (*path, int) {
// If it is a route request
if pattern == "/" {
pattern = ""
}
// split the route pattern into parts "/" as the splitting key
routeParts := strings.Split(pattern, "/")
log.Printf("GET NODE: About to loop thru :-- %s | %d\n", routeParts, len(routeParts))
var branchpath *path
var pathFound bool
var level int
// Now loop thru the route pattern parts
for lvl, part := range routeParts {
// how deep are we/did we go
// eg. /user is the 1st level after "/"
level = lvl
// the 1st loop will always be the root route path
if part == "" && level == 0 {
branchpath = p
}
// If a path is returned then it is the nested one
// and fed back to get the next nested branch node
// with the new/next in loop route pattern part
// If not found break, thus return the last branch that was found
// also the level tells us where the branch was found
branchpath, pathFound = transverseNode(branchpath, part)
log.Printf(">> Was the branch found: %v\n", pathFound)
log.Printf(">> Branch returned: %q\n", branchpath)
// If just one value exists in the array
// It's the route array, return it
if len(pattern) == 0 {
log.Println(">> Only asked for route request.")
return branchpath, level
}
// If no nested path is found, break it there is no match
if !pathFound {
// nothing found
break
}
}
// return nothing was found at all
if branchpath == nil {
return nil, -1
}
return branchpath, level
}
func (p *path) addNode(np *path) (*path, bool) {
// Split the given route pattern into parts
patternParts := strings.Split(np.pattern, "/")
// patternParts := strings.Split(strings.Replace(np.pattern, "/", "", 1), "/")
log.Printf("--------- Provided argument: %s, len: %v --------\n\n", patternParts, len(patternParts))
// Prepare to piece them together one by one, adding to the end on each loop
pieceTogether := make([]string, len(patternParts))
var branchpath *path
var isFound int
// loop thru each route part checking if it exists
// getting deeper into the tree after each single loop
for level, part := range patternParts {
// check if that node exists if it does, leave it alone, it not the one we want
// loop into the next part
pattern := strings.Join([]string{"/", part}, "")
pieceTogether = append(pieceTogether, pattern)
// join up the whole array into a route that can be used
fullpath := strings.Replace(strings.Join(pieceTogether, ""), "//", "/", -1)
log.Printf("------ Current path being checked :------ %v\n", fullpath)
if part == "/" {
branchpath = p
continue
}
// now check if the node exists
branchpath, isFound = p.getNode(fullpath)
log.Printf("Branch found: %v | level: %v\n", branchpath, isFound)
// If a branch was found
if branchpath != nil && level < len(patternParts)-1 {
log.Printf("--------- Branch exists!! --------\n", level)
log.Printf("branchpath.node len: %v\n", len(branchpath.node))
// If the node of the branch does exist
if branchpath.node == nil {
log.Printf("Node does not exists!!\n", level)
// The child part
nextChildPart := patternParts[level+1]
log.Printf("Next child path /%s\n", nextChildPart)
// get the character code of the route part
charcode := getCharCodeAt(nextChildPart, 0)
// If not, creeate the node
branchpath.node = make(map[rune]map[string]*path)
// add a defualt route map
routemap := make(map[string]*path)
routemap[nextChildPart] = &path{
router: router{
name: nextChildPart,
pattern: fullpath,
handler: strings.Join([]string{"/", nextChildPart, " handler for the route ", fullpath}, ""),
},
// node: make(map[rune]map[string]*path),
}
// NOTE: Do not reassign like This
// branchpath = branchpath.node[charcode][nextChildPart]
// you're overwriting it, and it will be detected on it own when looped
branchpath.node[charcode] = routemap
}
}
// which level are we in
log.Printf("-------- Level: %v ---------\n\n", level)
}
return p, false
}
func (p *path) addRouteHandler(np *path) {
p.router = np.router
}