Bugfix for the FullPath feature (#1919)

* worked with more complex situations
 * the original pr not work when and a short route with the same prefix
 to some already added routes
This commit is contained in:
bbiao 2019-06-28 23:54:52 +08:00 committed by Bo-Yi Wu
parent fc920dc561
commit f65018d7b1
2 changed files with 14 additions and 2 deletions

View File

@ -560,10 +560,15 @@ func TestRouteContextHoldsFullPath(t *testing.T) {
// Test routes // Test routes
routes := []string{ routes := []string{
"/",
"/simple", "/simple",
"/project/:name", "/project/:name",
"/",
"/news/home",
"/news",
"/simple-two/one",
"/simple-two/one-two",
"/project/:name/build/*params", "/project/:name/build/*params",
"/project/:name/bui",
} }
for _, route := range routes { for _, route := range routes {

View File

@ -128,6 +128,8 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
n.priority++ n.priority++
numParams := countParams(path) numParams := countParams(path)
parentFullPathIndex := 0
// non-empty tree // non-empty tree
if len(n.path) > 0 || len(n.children) > 0 { if len(n.path) > 0 || len(n.children) > 0 {
walk: walk:
@ -155,7 +157,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
children: n.children, children: n.children,
handlers: n.handlers, handlers: n.handlers,
priority: n.priority - 1, priority: n.priority - 1,
fullPath: fullPath, fullPath: n.fullPath,
} }
// Update maxParams (max of all children) // Update maxParams (max of all children)
@ -171,6 +173,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
n.path = path[:i] n.path = path[:i]
n.handlers = nil n.handlers = nil
n.wildChild = false n.wildChild = false
n.fullPath = fullPath[:parentFullPathIndex+i]
} }
// Make new node a child of this node // Make new node a child of this node
@ -178,6 +181,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
path = path[i:] path = path[i:]
if n.wildChild { if n.wildChild {
parentFullPathIndex += len(n.path)
n = n.children[0] n = n.children[0]
n.priority++ n.priority++
@ -211,6 +215,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
// slash after param // slash after param
if n.nType == param && c == '/' && len(n.children) == 1 { if n.nType == param && c == '/' && len(n.children) == 1 {
parentFullPathIndex += len(n.path)
n = n.children[0] n = n.children[0]
n.priority++ n.priority++
continue walk continue walk
@ -219,6 +224,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
// Check if a child with the next path byte exists // Check if a child with the next path byte exists
for i := 0; i < len(n.indices); i++ { for i := 0; i < len(n.indices); i++ {
if c == n.indices[i] { if c == n.indices[i] {
parentFullPathIndex += len(n.path)
i = n.incrementChildPrio(i) i = n.incrementChildPrio(i)
n = n.children[i] n = n.children[i]
continue walk continue walk
@ -369,6 +375,7 @@ func (n *node) insertChild(numParams uint8, path string, fullPath string, handle
// insert remaining path part and handle to the leaf // insert remaining path part and handle to the leaf
n.path = path[offset:] n.path = path[offset:]
n.handlers = handlers n.handlers = handlers
n.fullPath = fullPath
} }
// nodeValue holds return values of (*Node).getValue method // nodeValue holds return values of (*Node).getValue method