@@ -224,7 +224,12 @@ func (r *Router) insert(method, path string, t kind, rm routeMethod) {
224
224
}
225
225
currentNode .isLeaf = currentNode .staticChildren == nil && currentNode .paramChild == nil && currentNode .anyChild == nil
226
226
} else if lcpLen < prefixLen {
227
- // Split node
227
+ // Split node into two before we insert new node.
228
+ // This happens when we are inserting path that is submatch of any existing inserted paths.
229
+ // For example, we have node `/test` and now are about to insert `/te/*`. In that case
230
+ // 1. overlapping part is `/te` that is used as parent node
231
+ // 2. `st` is part from existing node that is not matching - it gets its own node (child to `/te`)
232
+ // 3. `/*` is the new part we are about to insert (child to `/te`)
228
233
n := newNode (
229
234
currentNode .kind ,
230
235
currentNode .prefix [lcpLen :],
@@ -235,6 +240,7 @@ func (r *Router) insert(method, path string, t kind, rm routeMethod) {
235
240
currentNode .paramsCount ,
236
241
currentNode .paramChild ,
237
242
currentNode .anyChild ,
243
+ currentNode .notFoundHandler ,
238
244
)
239
245
// Update parent path for all children to new node
240
246
for _ , child := range currentNode .staticChildren {
@@ -259,6 +265,7 @@ func (r *Router) insert(method, path string, t kind, rm routeMethod) {
259
265
currentNode .anyChild = nil
260
266
currentNode .isLeaf = false
261
267
currentNode .isHandler = false
268
+ currentNode .notFoundHandler = nil
262
269
263
270
// Only Static children could reach here
264
271
currentNode .addStaticChild (n )
@@ -273,7 +280,7 @@ func (r *Router) insert(method, path string, t kind, rm routeMethod) {
273
280
}
274
281
} else {
275
282
// Create child node
276
- n = newNode (t , search [lcpLen :], currentNode , nil , "" , new (routeMethods ), 0 , nil , nil )
283
+ n = newNode (t , search [lcpLen :], currentNode , nil , "" , new (routeMethods ), 0 , nil , nil , nil )
277
284
if rm .handler != nil {
278
285
n .addMethod (method , & rm )
279
286
n .paramsCount = len (rm .pnames )
@@ -292,7 +299,7 @@ func (r *Router) insert(method, path string, t kind, rm routeMethod) {
292
299
continue
293
300
}
294
301
// Create child node
295
- n := newNode (t , search , currentNode , nil , rm .ppath , new (routeMethods ), 0 , nil , nil )
302
+ n := newNode (t , search , currentNode , nil , rm .ppath , new (routeMethods ), 0 , nil , nil , nil )
296
303
if rm .handler != nil {
297
304
n .addMethod (method , & rm )
298
305
n .paramsCount = len (rm .pnames )
@@ -319,20 +326,32 @@ func (r *Router) insert(method, path string, t kind, rm routeMethod) {
319
326
}
320
327
}
321
328
322
- func newNode (t kind , pre string , p * node , sc children , originalPath string , mh * routeMethods , paramsCount int , paramChildren , anyChildren * node ) * node {
329
+ func newNode (
330
+ t kind ,
331
+ pre string ,
332
+ p * node ,
333
+ sc children ,
334
+ originalPath string ,
335
+ methods * routeMethods ,
336
+ paramsCount int ,
337
+ paramChildren ,
338
+ anyChildren * node ,
339
+ notFoundHandler * routeMethod ,
340
+ ) * node {
323
341
return & node {
324
- kind : t ,
325
- label : pre [0 ],
326
- prefix : pre ,
327
- parent : p ,
328
- staticChildren : sc ,
329
- originalPath : originalPath ,
330
- methods : mh ,
331
- paramsCount : paramsCount ,
332
- paramChild : paramChildren ,
333
- anyChild : anyChildren ,
334
- isLeaf : sc == nil && paramChildren == nil && anyChildren == nil ,
335
- isHandler : mh .isHandler (),
342
+ kind : t ,
343
+ label : pre [0 ],
344
+ prefix : pre ,
345
+ parent : p ,
346
+ staticChildren : sc ,
347
+ originalPath : originalPath ,
348
+ methods : methods ,
349
+ paramsCount : paramsCount ,
350
+ paramChild : paramChildren ,
351
+ anyChild : anyChildren ,
352
+ isLeaf : sc == nil && paramChildren == nil && anyChildren == nil ,
353
+ isHandler : methods .isHandler (),
354
+ notFoundHandler : notFoundHandler ,
336
355
}
337
356
}
338
357
0 commit comments