mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
parent
e43aa31e01
commit
ab1e479ada
11
router.go
11
router.go
@ -93,7 +93,7 @@ func (r *router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
|
||||
}
|
||||
} else if l < pl {
|
||||
// Split node
|
||||
n := newNode(t, cn.prefix[l:], cn, cn.children, cn.handler, cn.pnames, cn.echo)
|
||||
n := newNode(cn.typ, cn.prefix[l:], cn, cn.children, cn.handler, cn.pnames, cn.echo)
|
||||
cn.children = children{n} // Add to parent
|
||||
// if n.typ == ptype {
|
||||
// cn.pchild = n
|
||||
@ -198,7 +198,7 @@ func (n *node) findPchild() *node {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *node) findCchild() *node {
|
||||
func (n *node) findMchild() *node {
|
||||
for _, c := range n.children {
|
||||
if c.typ == mtype {
|
||||
return c
|
||||
@ -273,6 +273,13 @@ func (r *router) Find(method, path string, ctx *Context) (h HandlerFunc, echo *E
|
||||
continue
|
||||
}
|
||||
|
||||
// Match-any
|
||||
c = cn.findMchild()
|
||||
if c != nil {
|
||||
cn = c
|
||||
continue
|
||||
}
|
||||
|
||||
Up:
|
||||
tn := cn // Save current node
|
||||
cn = cn.parent
|
||||
|
@ -13,15 +13,6 @@ type route struct {
|
||||
path string
|
||||
}
|
||||
|
||||
var (
|
||||
api2 = []route{
|
||||
// Issues
|
||||
{"DELETE", "/repos/:owner/:repo/labels/:name"},
|
||||
{"GET", "/repos/:owner/:repo/issues/:number/labels"},
|
||||
{"POST", "/repos/:owner/:repo/issues/:number/labels"},
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
context = &Context{pvalues: make([]string, 5)}
|
||||
api = []route{
|
||||
@ -326,18 +317,18 @@ func TestRouterTwoParam(t *testing.T) {
|
||||
return nil
|
||||
}, nil)
|
||||
|
||||
// h, _ := r.Find(GET, "/users/1/files/1", context)
|
||||
// if h == nil {
|
||||
// t.Fatal("handler not found")
|
||||
// }
|
||||
// if context.pvalues[0] != "1" {
|
||||
// t.Error("param uid should be 1")
|
||||
// }
|
||||
// if context.pvalues[1] != "1" {
|
||||
// t.Error("param fid should be 1")
|
||||
// }
|
||||
h, _ := r.Find(GET, "/users/1/files/1", context)
|
||||
if h == nil {
|
||||
t.Fatal("handler not found")
|
||||
}
|
||||
if context.pvalues[0] != "1" {
|
||||
t.Error("param uid should be 1")
|
||||
}
|
||||
if context.pvalues[1] != "1" {
|
||||
t.Error("param fid should be 1")
|
||||
}
|
||||
|
||||
h, _ := r.Find(GET, "/users/1", context)
|
||||
h, _ = r.Find(GET, "/users/1", context)
|
||||
if h != nil {
|
||||
t.Error("should not found handler")
|
||||
}
|
||||
@ -354,8 +345,7 @@ func TestRouterMatchAny(t *testing.T) {
|
||||
t.Fatal("handler not found")
|
||||
}
|
||||
if context.pvalues[0] != "" {
|
||||
println(context.pvalues[0])
|
||||
t.Error("value should be joe")
|
||||
t.Error("value should be empty")
|
||||
}
|
||||
|
||||
h, _ = r.Find(GET, "/users/joe", context)
|
||||
@ -634,7 +624,7 @@ func TestRouterServeHTTP(t *testing.T) {
|
||||
|
||||
func (n *node) printTree(pfx string, tail bool) {
|
||||
p := prefix(tail, pfx, "└── ", "├── ")
|
||||
fmt.Printf("%s%s, %p: parent=%p, handler=%v, echo=%v\n", p, n.prefix, n, n.parent, n.handler, n.echo)
|
||||
fmt.Printf("%s%s, %p: type=%d, parent=%p, handler=%v\n", p, n.prefix, n, n.typ, n.parent, n.handler)
|
||||
|
||||
children := n.children
|
||||
l := len(children)
|
||||
|
Loading…
Reference in New Issue
Block a user