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)
|
root := engine.trees.get(method)
|
||||||
if root == nil {
|
if root == nil {
|
||||||
root = new(node)
|
root = new(node)
|
||||||
// root.fullPath = "/"
|
root.fullPath = "/"
|
||||||
engine.trees = append(engine.trees, methodTree{method: method, root: root})
|
engine.trees = append(engine.trees, methodTree{method: method, root: root})
|
||||||
}
|
}
|
||||||
root.addRoute(path, handlers)
|
root.addRoute(path, handlers)
|
||||||
@ -448,7 +448,7 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
|
|||||||
// if value.params != nil {
|
// if value.params != nil {
|
||||||
// c.Params = *value.params
|
// c.Params = *value.params
|
||||||
// }
|
// }
|
||||||
// c.fullPath = value.fullPath
|
c.fullPath = value.fullPath
|
||||||
c.Next()
|
c.Next()
|
||||||
c.writermem.WriteHeaderNow()
|
c.writermem.WriteHeaderNow()
|
||||||
return
|
return
|
||||||
|
@ -579,42 +579,42 @@ func TestRouteServeErrorWithWriteHeader(t *testing.T) {
|
|||||||
assert.Equal(t, 0, w.Body.Len())
|
assert.Equal(t, 0, w.Body.Len())
|
||||||
}
|
}
|
||||||
|
|
||||||
// func TestRouteContextHoldsFullPath(t *testing.T) {
|
func TestRouteContextHoldsFullPath(t *testing.T) {
|
||||||
// router := New()
|
router := New()
|
||||||
|
|
||||||
// // Test routes
|
// Test routes
|
||||||
// routes := []string{
|
routes := []string{
|
||||||
// "/simple",
|
"/simple",
|
||||||
// "/project/:name",
|
"/project/:name",
|
||||||
// "/",
|
"/",
|
||||||
// "/news/home",
|
"/news/home",
|
||||||
// "/news",
|
"/news",
|
||||||
// "/simple-two/one",
|
"/simple-two/one",
|
||||||
// "/simple-two/one-two",
|
"/simple-two/one-two",
|
||||||
// "/project/:name/build/*params",
|
"/project/:name/build/*params",
|
||||||
// "/project/:name/bui",
|
"/project/:name/bui",
|
||||||
// }
|
}
|
||||||
|
|
||||||
// for _, route := range routes {
|
for _, route := range routes {
|
||||||
// actualRoute := route
|
actualRoute := route
|
||||||
// router.GET(route, func(c *Context) {
|
router.GET(route, func(c *Context) {
|
||||||
// // For each defined route context should contain its full path
|
// For each defined route context should contain its full path
|
||||||
// assert.Equal(t, actualRoute, c.FullPath())
|
assert.Equal(t, actualRoute, c.FullPath())
|
||||||
// c.AbortWithStatus(http.StatusOK)
|
c.AbortWithStatus(http.StatusOK)
|
||||||
// })
|
})
|
||||||
// }
|
}
|
||||||
|
|
||||||
// for _, route := range routes {
|
for _, route := range routes {
|
||||||
// w := performRequest(router, http.MethodGet, route)
|
w := performRequest(router, http.MethodGet, route)
|
||||||
// assert.Equal(t, http.StatusOK, w.Code)
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// // Test not found
|
// Test not found
|
||||||
// router.Use(func(c *Context) {
|
router.Use(func(c *Context) {
|
||||||
// // For not found routes full path is empty
|
// For not found routes full path is empty
|
||||||
// assert.Equal(t, "", c.FullPath())
|
assert.Equal(t, "", c.FullPath())
|
||||||
// })
|
})
|
||||||
|
|
||||||
// w := performRequest(router, http.MethodGet, "/not-found")
|
w := performRequest(router, http.MethodGet, "/not-found")
|
||||||
// assert.Equal(t, http.StatusNotFound, w.Code)
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||||
// }
|
}
|
||||||
|
34
tree.go
34
tree.go
@ -101,7 +101,7 @@ type node struct {
|
|||||||
nType nodeType
|
nType nodeType
|
||||||
maxParams uint16
|
maxParams uint16
|
||||||
wildChild bool
|
wildChild bool
|
||||||
// fullPath string
|
fullPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
// increments priority of the given child and reorders if necessary.
|
// increments priority of the given child and reorders if necessary.
|
||||||
@ -141,7 +141,7 @@ func (n *node) addRoute(path string, handlers HandlersChain) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// parentFullPathIndex := 0
|
parentFullPathIndex := 0
|
||||||
|
|
||||||
walk:
|
walk:
|
||||||
for {
|
for {
|
||||||
@ -164,7 +164,7 @@ walk:
|
|||||||
children: n.children,
|
children: n.children,
|
||||||
handlers: n.handlers,
|
handlers: n.handlers,
|
||||||
priority: n.priority - 1,
|
priority: n.priority - 1,
|
||||||
// fullPath: n.fullPath,
|
fullPath: n.fullPath,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update maxParams (max of all children)
|
// Update maxParams (max of all children)
|
||||||
@ -180,7 +180,7 @@ walk:
|
|||||||
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]
|
n.fullPath = fullPath[:parentFullPathIndex+i]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make new node a child of this node
|
// Make new node a child of this node
|
||||||
@ -188,7 +188,7 @@ walk:
|
|||||||
path = path[i:]
|
path = path[i:]
|
||||||
|
|
||||||
if n.wildChild {
|
if n.wildChild {
|
||||||
// parentFullPathIndex += len(n.path)
|
parentFullPathIndex += len(n.path)
|
||||||
n = n.children[0]
|
n = n.children[0]
|
||||||
n.priority++
|
n.priority++
|
||||||
|
|
||||||
@ -222,7 +222,7 @@ walk:
|
|||||||
|
|
||||||
// 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)
|
parentFullPathIndex += len(n.path)
|
||||||
n = n.children[0]
|
n = n.children[0]
|
||||||
n.priority++
|
n.priority++
|
||||||
continue walk
|
continue walk
|
||||||
@ -231,7 +231,7 @@ walk:
|
|||||||
// Check if a child with the next path byte exists
|
// Check if a child with the next path byte exists
|
||||||
for i, max := 0, len(n.indices); i < max; i++ {
|
for i, max := 0, len(n.indices); i < max; i++ {
|
||||||
if c == n.indices[i] {
|
if c == n.indices[i] {
|
||||||
// parentFullPathIndex += len(n.path)
|
parentFullPathIndex += len(n.path)
|
||||||
i = n.incrementChildPrio(i)
|
i = n.incrementChildPrio(i)
|
||||||
n = n.children[i]
|
n = n.children[i]
|
||||||
continue walk
|
continue walk
|
||||||
@ -244,7 +244,7 @@ walk:
|
|||||||
n.indices += string([]byte{c})
|
n.indices += string([]byte{c})
|
||||||
child := &node{
|
child := &node{
|
||||||
// maxParams: numParams,
|
// maxParams: numParams,
|
||||||
// fullPath: fullPath,
|
fullPath: fullPath,
|
||||||
}
|
}
|
||||||
n.children = append(n.children, child)
|
n.children = append(n.children, child)
|
||||||
n.incrementChildPrio(len(n.indices) - 1)
|
n.incrementChildPrio(len(n.indices) - 1)
|
||||||
@ -326,7 +326,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain)
|
|||||||
nType: param,
|
nType: param,
|
||||||
path: wildcard,
|
path: wildcard,
|
||||||
// maxParams: numParams,
|
// maxParams: numParams,
|
||||||
// fullPath: fullPath,
|
fullPath: fullPath,
|
||||||
}
|
}
|
||||||
n.children = []*node{child}
|
n.children = []*node{child}
|
||||||
n = child
|
n = child
|
||||||
@ -341,7 +341,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain)
|
|||||||
child := &node{
|
child := &node{
|
||||||
// maxParams: numParams,
|
// maxParams: numParams,
|
||||||
priority: 1,
|
priority: 1,
|
||||||
// fullPath: fullPath,
|
fullPath: fullPath,
|
||||||
}
|
}
|
||||||
n.children = []*node{child}
|
n.children = []*node{child}
|
||||||
n = child
|
n = child
|
||||||
@ -375,7 +375,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain)
|
|||||||
wildChild: true,
|
wildChild: true,
|
||||||
nType: catchAll,
|
nType: catchAll,
|
||||||
// maxParams: 1,
|
// maxParams: 1,
|
||||||
// fullPath: fullPath,
|
fullPath: fullPath,
|
||||||
}
|
}
|
||||||
// update maxParams of the parent node
|
// update maxParams of the parent node
|
||||||
// if n.maxParams < 1 {
|
// if n.maxParams < 1 {
|
||||||
@ -393,7 +393,7 @@ func (n *node) insertChild(path string, fullPath string, handlers HandlersChain)
|
|||||||
handlers: handlers,
|
handlers: handlers,
|
||||||
priority: 1,
|
priority: 1,
|
||||||
// maxParams: 1,
|
// maxParams: 1,
|
||||||
// fullPath: fullPath,
|
fullPath: fullPath,
|
||||||
}
|
}
|
||||||
n.children = []*node{child}
|
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
|
// If no wildcard was found, simply insert the path and handle
|
||||||
n.path = path
|
n.path = path
|
||||||
n.handlers = handlers
|
n.handlers = handlers
|
||||||
// n.fullPath = fullPath
|
n.fullPath = fullPath
|
||||||
}
|
}
|
||||||
|
|
||||||
// nodeValue holds return values of (*Node).getValue method
|
// nodeValue holds return values of (*Node).getValue method
|
||||||
@ -411,7 +411,7 @@ type nodeValue struct {
|
|||||||
handlers HandlersChain
|
handlers HandlersChain
|
||||||
params *Params
|
params *Params
|
||||||
tsr bool
|
tsr bool
|
||||||
// fullPath string
|
fullPath string
|
||||||
}
|
}
|
||||||
|
|
||||||
// getValue returns the handle registered with the given path (key). The values of
|
// 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 {
|
if value.handlers = n.handlers; value.handlers != nil {
|
||||||
// value.fullPath = n.fullPath
|
value.fullPath = n.fullPath
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(n.children) == 1 {
|
if len(n.children) == 1 {
|
||||||
@ -554,7 +554,7 @@ walk: // Outer loop for walking the tree
|
|||||||
}
|
}
|
||||||
|
|
||||||
value.handlers = n.handlers
|
value.handlers = n.handlers
|
||||||
// value.fullPath = n.fullPath
|
value.fullPath = n.fullPath
|
||||||
return
|
return
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@ -565,7 +565,7 @@ walk: // Outer loop for walking the tree
|
|||||||
// We should have reached the node containing the handle.
|
// We should have reached the node containing the handle.
|
||||||
// Check if this node has a handle registered.
|
// Check if this node has a handle registered.
|
||||||
if value.handlers = n.handlers; value.handlers != nil {
|
if value.handlers = n.handlers; value.handlers != nil {
|
||||||
// value.fullPath = n.fullPath
|
value.fullPath = n.fullPath
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user