gin-gonic / gin

Compare caf2802 ... +0 ... d4ca9a0

Showing 1 of 3 files from the diff.
Other files ignored by Codecov

@@ -118,11 +118,6 @@
Loading
118 118
	fullPath  string
119 119
}
120 120
121 -
type skip struct {
122 -
	path      string
123 -
	paramNode *node
124 -
}
125 -
126 121
// Increments priority of the given child and reorders if necessary
127 122
func (n *node) incrementChildPrio(pos int) int {
128 123
	cs := n.children
@@ -405,7 +400,23 @@
Loading
405 400
// made if a handle exists with an extra (without the) trailing slash for the
406 401
// given path.
407 402
func (n *node) getValue(path string, params *Params, unescape bool) (value nodeValue) {
408 -
	var skipped *skip
403 +
	// path: /abc/123/def
404 +
	// level 1 router:abc
405 +
	// level 2 router:123
406 +
	// level 3 router:def
407 +
	var (
408 +
		skippedPath string
409 +
		latestNode  = n // not found `level 2 router` use latestNode
410 +
411 +
		// match '/' count
412 +
		// matchNum < 1: `level 2 router` not found,the current node needs to be equal to latestNode
413 +
		// matchNum >= 1: `level (2 or 3 or 4 or ...) router`: Normal handling
414 +
		matchNum int // each match will accumulate
415 +
	)
416 +
	//if path == "/", no need to look for tree node
417 +
	if len(path) == 1 {
418 +
		matchNum = 1
419 +
	}
409 420
410 421
walk: // Outer loop for walking the tree
411 422
	for {
@@ -418,32 +429,41 @@
Loading
418 429
				idxc := path[0]
419 430
				for i, c := range []byte(n.indices) {
420 431
					if c == idxc {
421 -
						if strings.HasPrefix(n.children[len(n.children)-1].path, ":") {
422 -
							skipped = &skip{
423 -
								path: prefix + path,
424 -
								paramNode: &node{
425 -
									path:      n.path,
426 -
									wildChild: n.wildChild,
427 -
									nType:     n.nType,
428 -
									priority:  n.priority,
429 -
									children:  n.children,
430 -
									handlers:  n.handlers,
431 -
									fullPath:  n.fullPath,
432 -
								},
432 +
						//  strings.HasPrefix(n.children[len(n.children)-1].path, ":") == n.wildChild
433 +
						if n.wildChild {
434 +
							skippedPath = prefix + path
435 +
							latestNode = &node{
436 +
								path:      n.path,
437 +
								wildChild: n.wildChild,
438 +
								nType:     n.nType,
439 +
								priority:  n.priority,
440 +
								children:  n.children,
441 +
								handlers:  n.handlers,
442 +
								fullPath:  n.fullPath,
433 443
							}
434 444
						}
435 445
436 446
						n = n.children[i]
447 +
448 +
						// match '/', If this condition is matched, the next route is found
449 +
						if (len(n.fullPath) != 0 && n.wildChild) || path[len(path)-1] == '/' {
450 +
							matchNum++
451 +
						}
437 452
						continue walk
438 453
					}
439 454
				}
440 455
456 +
				// level 2 router not found,the current node needs to be equal to latestNode
457 +
				if matchNum < 1 {
458 +
					n = latestNode
459 +
				}
460 +
441 461
				// If there is no wildcard pattern, recommend a redirection
442 462
				if !n.wildChild {
443 463
					// Nothing found.
444 464
					// We can recommend to redirect to the same URL without a
445 465
					// trailing slash if a leaf exists for that path.
446 -
					value.tsr = (path == "/" && n.handlers != nil)
466 +
					value.tsr = path == "/" && n.handlers != nil
447 467
					return
448 468
				}
449 469
@@ -483,6 +503,18 @@
Loading
483 503
						if len(n.children) > 0 {
484 504
							path = path[end:]
485 505
							n = n.children[0]
506 +
							// next node,the latestNode needs to be equal to currentNode and handle next router
507 +
							latestNode = n
508 +
							// not found router in (level 1 router and handle next node),skippedPath cannot execute
509 +
							// example:
510 +
							// * /:cc/cc
511 +
							// call /a/cc 	     expectations:match/200      Actual:match/200
512 +
							// call /a/dd 	     expectations:unmatch/404    Actual: panic
513 +
							// call /addr/dd/aa  expectations:unmatch/404    Actual: panic
514 +
							// skippedPath: It can only be executed if the secondary route is not found
515 +
							// matchNum: Go to the next level of routing tree node search,need add matchNum
516 +
							skippedPath = ""
517 +
							matchNum++
486 518
							continue walk
487 519
						}
488 520
@@ -535,6 +567,10 @@
Loading
535 567
		}
536 568
537 569
		if path == prefix {
570 +
			// level 2 router not found and latestNode.wildChild is true
571 +
			if matchNum < 1 && latestNode.wildChild {
572 +
				n = latestNode.children[len(latestNode.children)-1]
573 +
			}
538 574
			// We should have reached the node containing the handle.
539 575
			// Check if this node has a handle registered.
540 576
			if value.handlers = n.handlers; value.handlers != nil {
@@ -564,18 +600,18 @@
Loading
564 600
			return
565 601
		}
566 602
567 -
		if path != "/" && skipped != nil && strings.HasSuffix(skipped.path, path) {
568 -
			path = skipped.path
569 -
			n = skipped.paramNode
570 -
			skipped = nil
603 +
		// path != "/" && skippedPath != ""
604 +
		if len(path) != 1 && len(skippedPath) > 0 && strings.HasSuffix(skippedPath, path) {
605 +
			path = skippedPath
606 +
			n = latestNode
607 +
			skippedPath = ""
571 608
			continue walk
572 609
		}
573 610
574 611
		// Nothing found. We can recommend to redirect to the same URL with an
575 612
		// extra trailing slash if a leaf exists for that path
576 -
		value.tsr = (path == "/") ||
577 -
			(len(prefix) == len(path)+1 && prefix[len(path)] == '/' &&
578 -
				path == prefix[:len(prefix)-1] && n.handlers != nil)
613 +
		value.tsr = path == "/" ||
614 +
			(len(prefix) == len(path)+1 && n.handlers != nil)
579 615
		return
580 616
	}
581 617
}

Everything is accounted for!

No changes detected that need to be reviewed.
What changes does Codecov check for?
Lines, not adjusted in diff, that have changed coverage data.
Files that introduced coverage data that had none before.
Files that have missing coverage data that once were tracked.
Files Coverage
auth.go 100.00%
binding/binding.go 100.00%
binding/binding_nomsgpack.go 100.00%
binding/default_validator.go 100.00%
binding/form.go 100.00%
binding/form_mapping.go 100.00%
binding/header.go 100.00%
binding/json.go 100.00%
binding/msgpack.go 100.00%
binding/multipart_form_mapping.go 100.00%
binding/protobuf.go 100.00%
binding/query.go 100.00%
binding/uri.go 100.00%
binding/xml.go 100.00%
binding/yaml.go 100.00%
context.go 97.69%
debug.go 100.00%
deprecated.go 100.00%
errors.go 100.00%
fs.go 100.00%
gin.go 99.18%
logger.go 100.00%
mode.go 100.00%
path.go 100.00%
recovery.go 97.14%
render/data.go 100.00%
render/html.go 100.00%
render/json.go 85.29%
render/msgpack.go 100.00%
render/protobuf.go 100.00%
render/reader.go 100.00%
render/redirect.go 100.00%
render/render.go 100.00%
render/text.go 100.00%
render/xml.go 100.00%
render/yaml.go 100.00%
response_writer.go 93.33%
routergroup.go 100.00%
test_helpers.go 100.00%
tree.go 100.00%
utils.go 96.83%
Project Totals (41 files) 98.71%
Loading