mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-18 23:12:17 +08:00
add fullPath
Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
parent
7035cc2dd7
commit
513bc5ef43
4
gin.go
4
gin.go
@ -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)
|
||||
@ -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
|
||||
|
@ -579,42 +579,42 @@ func TestRouteServeErrorWithWriteHeader(t *testing.T) {
|
||||
assert.Equal(t, 0, w.Body.Len())
|
||||
}
|
||||
|
||||
// func TestRouteContextHoldsFullPath(t *testing.T) {
|
||||
// router := New()
|
||||
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",
|
||||
// }
|
||||
// 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 {
|
||||
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)
|
||||
// }
|
||||
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())
|
||||
// })
|
||||
// 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)
|
||||
// }
|
||||
w := performRequest(router, http.MethodGet, "/not-found")
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
}
|
||||
|
34
tree.go
34
tree.go
@ -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.
|
||||
@ -141,7 +141,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
|
||||
return
|
||||
}
|
||||
|
||||
// parentFullPathIndex := 0
|
||||
parentFullPathIndex := 0
|
||||
|
||||
walk:
|
||||
for {
|
||||
@ -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)
|
||||
@ -180,7 +180,7 @@ 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
|
||||
@ -188,7 +188,7 @@ walk:
|
||||
path = path[i:]
|
||||
|
||||
if n.wildChild {
|
||||
// parentFullPathIndex += len(n.path)
|
||||
parentFullPathIndex += len(n.path)
|
||||
n = n.children[0]
|
||||
n.priority++
|
||||
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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)
|
||||
@ -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
|
||||
@ -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
|
||||
@ -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 {
|
||||
@ -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}
|
||||
|
||||
@ -403,7 +403,7 @@ 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
|
||||
@ -411,7 +411,7 @@ 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
|
||||
@ -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 {
|
||||
@ -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:
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user