Skip to content

Commit

Permalink
add fullPath
Browse files Browse the repository at this point in the history
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Loading branch information
appleboy committed May 8, 2020
1 parent f11ccce commit a53322c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 58 deletions.
4 changes: 2 additions & 2 deletions gin.go
Expand Up @@ -265,7 +265,7 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
root := engine.trees.get(method)
if root == nil {
root = new(node)
// root.fullPath = "/"
root.fullPath = "/"
engine.trees = append(engine.trees, methodTree{method: method, root: root})
}
root.addRoute(path, handlers)
Expand Down Expand Up @@ -448,7 +448,7 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
// if value.params != nil {
// c.Params = *value.params
// }
// c.fullPath = value.fullPath
c.fullPath = value.fullPath
c.Next()
c.writermem.WriteHeaderNow()
return
Expand Down
78 changes: 39 additions & 39 deletions routes_test.go
Expand Up @@ -579,42 +579,42 @@ func TestRouteServeErrorWithWriteHeader(t *testing.T) {
assert.Equal(t, 0, w.Body.Len())
}

// func TestRouteContextHoldsFullPath(t *testing.T) {
// router := New()

// // Test routes
// routes := []string{
// "/simple",
// "/project/:name",
// "/",
// "/news/home",
// "/news",
// "/simple-two/one",
// "/simple-two/one-two",
// "/project/:name/build/*params",
// "/project/:name/bui",
// }

// for _, route := range routes {
// actualRoute := route
// router.GET(route, func(c *Context) {
// // For each defined route context should contain its full path
// assert.Equal(t, actualRoute, c.FullPath())
// c.AbortWithStatus(http.StatusOK)
// })
// }

// for _, route := range routes {
// w := performRequest(router, http.MethodGet, route)
// assert.Equal(t, http.StatusOK, w.Code)
// }

// // Test not found
// router.Use(func(c *Context) {
// // For not found routes full path is empty
// assert.Equal(t, "", c.FullPath())
// })

// w := performRequest(router, http.MethodGet, "/not-found")
// assert.Equal(t, http.StatusNotFound, w.Code)
// }
func TestRouteContextHoldsFullPath(t *testing.T) {
router := New()

// Test routes
routes := []string{
"/simple",
"/project/:name",
"/",
"/news/home",
"/news",
"/simple-two/one",
"/simple-two/one-two",
"/project/:name/build/*params",
"/project/:name/bui",
}

for _, route := range routes {
actualRoute := route
router.GET(route, func(c *Context) {
// For each defined route context should contain its full path
assert.Equal(t, actualRoute, c.FullPath())
c.AbortWithStatus(http.StatusOK)
})
}

for _, route := range routes {
w := performRequest(router, http.MethodGet, route)
assert.Equal(t, http.StatusOK, w.Code)
}

// Test not found
router.Use(func(c *Context) {
// For not found routes full path is empty
assert.Equal(t, "", c.FullPath())
})

w := performRequest(router, http.MethodGet, "/not-found")
assert.Equal(t, http.StatusNotFound, w.Code)
}
34 changes: 17 additions & 17 deletions tree.go
Expand Up @@ -101,7 +101,7 @@ type node struct {
nType nodeType
maxParams uint16
wildChild bool
// fullPath string
fullPath string
}

// increments priority of the given child and reorders if necessary.
Expand Down Expand Up @@ -141,7 +141,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
return
}

// parentFullPathIndex := 0
parentFullPathIndex := 0

walk:
for {
Expand All @@ -164,7 +164,7 @@ walk:
children: n.children,
handlers: n.handlers,
priority: n.priority - 1,
// fullPath: n.fullPath,
fullPath: n.fullPath,
}

// Update maxParams (max of all children)
Expand All @@ -180,15 +180,15 @@ walk:
n.path = path[:i]
n.handlers = nil
n.wildChild = false
// n.fullPath = fullPath[:parentFullPathIndex+i]
n.fullPath = fullPath[:parentFullPathIndex+i]
}

// Make new node a child of this node
if i < len(path) {
path = path[i:]

if n.wildChild {
// parentFullPathIndex += len(n.path)
parentFullPathIndex += len(n.path)
n = n.children[0]
n.priority++

Expand Down Expand Up @@ -222,7 +222,7 @@ walk:

// slash after param
if n.nType == param && c == '/' && len(n.children) == 1 {
// parentFullPathIndex += len(n.path)
parentFullPathIndex += len(n.path)
n = n.children[0]
n.priority++
continue walk
Expand All @@ -231,7 +231,7 @@ walk:
// Check if a child with the next path byte exists
for i, max := 0, len(n.indices); i < max; i++ {
if c == n.indices[i] {
// parentFullPathIndex += len(n.path)
parentFullPathIndex += len(n.path)
i = n.incrementChildPrio(i)
n = n.children[i]
continue walk
Expand All @@ -244,7 +244,7 @@ walk:
n.indices += string([]byte{c})
child := &node{
// maxParams: numParams,
// fullPath: fullPath,
fullPath: fullPath,
}
n.children = append(n.children, child)
n.incrementChildPrio(len(n.indices) - 1)
Expand Down Expand Up @@ -326,7 +326,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain)
nType: param,
path: wildcard,
// maxParams: numParams,
// fullPath: fullPath,
fullPath: fullPath,
}
n.children = []*node{child}
n = child
Expand All @@ -341,7 +341,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain)
child := &node{
// maxParams: numParams,
priority: 1,
// fullPath: fullPath,
fullPath: fullPath,
}
n.children = []*node{child}
n = child
Expand Down Expand Up @@ -375,7 +375,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain)
wildChild: true,
nType: catchAll,
// maxParams: 1,
// fullPath: fullPath,
fullPath: fullPath,
}
// update maxParams of the parent node
// if n.maxParams < 1 {
Expand All @@ -393,7 +393,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain)
handlers: handlers,
priority: 1,
// maxParams: 1,
// fullPath: fullPath,
fullPath: fullPath,
}
n.children = []*node{child}

Expand All @@ -403,15 +403,15 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain)
// If no wildcard was found, simply insert the path and handle
n.path = path
n.handlers = handlers
// n.fullPath = fullPath
n.fullPath = fullPath
}

// nodeValue holds return values of (*Node).getValue method
type nodeValue struct {
handlers HandlersChain
params *Params
tsr bool
// fullPath string
fullPath string
}

// getValue returns the handle registered with the given path (key). The values of
Expand Down Expand Up @@ -505,7 +505,7 @@ walk: // Outer loop for walking the tree
}

if value.handlers = n.handlers; value.handlers != nil {
// value.fullPath = n.fullPath
value.fullPath = n.fullPath
return
}
if len(n.children) == 1 {
Expand Down Expand Up @@ -554,7 +554,7 @@ walk: // Outer loop for walking the tree
}

value.handlers = n.handlers
// value.fullPath = n.fullPath
value.fullPath = n.fullPath
return

default:
Expand All @@ -565,7 +565,7 @@ walk: // Outer loop for walking the tree
// We should have reached the node containing the handle.
// Check if this node has a handle registered.
if value.handlers = n.handlers; value.handlers != nil {
// value.fullPath = n.fullPath
value.fullPath = n.fullPath
return
}

Expand Down

0 comments on commit a53322c

Please sign in to comment.