Skip to content

Commit db75ddc

Browse files
committed
improve comments
1 parent 3093bab commit db75ddc

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

router.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -339,34 +339,37 @@ func (r *Router) Find(method, path string, c Context) {
339339
pvalues = ctx.pvalues // Use the internal slice so the interface can keep the illusion of a dynamic slice
340340
)
341341

342-
// backtracking happens when we reach dead end when matching nodes in the router tree. To backtrack we set
343-
// current node to parent to current node (this was previous node we checked) and choose next node kind to check.
344-
// Next node kind relies on routing priority (static->param->any). So for example if there is no static node match we
345-
// should check parent next sibling by kind (param). Backtracking itself does not check if there is next sibling this
346-
// is left up to matching logic
342+
// Backtracking is needed when a dead end (leaf node) is reached in the router tree.
343+
// To backtrack the current node will be changed to the parent node and the next kind for the
344+
// router logic will be returned based on fromKind or kind of the dead end node (static > param > any).
345+
// For example if there is no static node match we should check parent next sibling by kind (param).
346+
// Backtracking itself does not check if there is a next sibling, this is done by the router logic.
347347
backtrackToNextNodeKind := func(fromKind kind) (nextNodeKind kind, valid bool) {
348348
previous := cn
349349
cn = previous.parent
350350
valid = cn != nil
351351

352-
// next node type by priority
353-
// NOTE: by current implementation we never backtrack from any route so `previous.kind` value is here always static or any
354-
// if this requirement is to change then for any route next kind would be static kind and this statement should be changed
352+
// Next node type by priority
353+
// NOTE: With the current implementation we never backtrack from an `any` route, so `previous.kind` is
354+
// always `static` or `any`
355+
// If this is changed then for any route next kind would be `static` and this statement should be changed
355356
nextNodeKind = previous.kind + 1
356357

357358
if fromKind == skind {
358359
// when backtracking is done from static kind block we did not change search so nothing to restore
359360
return
360361
}
361362

363+
// restore search to value it was before we move to current node we are backtracking from.
362364
if previous.kind == skind {
363365
searchIndex -= len(previous.prefix)
364-
search = path[searchIndex:]
365366
} else {
366367
n--
368+
// for param/any node.prefix value is always `:` so we can not deduce searchIndex from that and must use pValue
369+
// for that index as it would also contain part of path we cut off before moving into node we are backtracking from
367370
searchIndex -= len(pvalues[n])
368-
search = path[searchIndex:]
369371
}
372+
search = path[searchIndex:]
370373
return
371374
}
372375

0 commit comments

Comments
 (0)