1
0
mirror of https://github.com/labstack/echo.git synced 2025-04-11 11:42:01 +02:00
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-04-30 18:47:13 -07:00
parent e43aa31e01
commit ab1e479ada
2 changed files with 22 additions and 25 deletions

View File

@ -93,7 +93,7 @@ func (r *router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
} }
} else if l < pl { } else if l < pl {
// Split node // 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 cn.children = children{n} // Add to parent
// if n.typ == ptype { // if n.typ == ptype {
// cn.pchild = n // cn.pchild = n
@ -198,7 +198,7 @@ func (n *node) findPchild() *node {
return nil return nil
} }
func (n *node) findCchild() *node { func (n *node) findMchild() *node {
for _, c := range n.children { for _, c := range n.children {
if c.typ == mtype { if c.typ == mtype {
return c return c
@ -273,6 +273,13 @@ func (r *router) Find(method, path string, ctx *Context) (h HandlerFunc, echo *E
continue continue
} }
// Match-any
c = cn.findMchild()
if c != nil {
cn = c
continue
}
Up: Up:
tn := cn // Save current node tn := cn // Save current node
cn = cn.parent cn = cn.parent

View File

@ -13,15 +13,6 @@ type route struct {
path string 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 ( var (
context = &Context{pvalues: make([]string, 5)} context = &Context{pvalues: make([]string, 5)}
api = []route{ api = []route{
@ -326,18 +317,18 @@ func TestRouterTwoParam(t *testing.T) {
return nil return nil
}, nil) }, nil)
// h, _ := r.Find(GET, "/users/1/files/1", context) h, _ := r.Find(GET, "/users/1/files/1", context)
// if h == nil { if h == nil {
// t.Fatal("handler not found") t.Fatal("handler not found")
// } }
// if context.pvalues[0] != "1" { if context.pvalues[0] != "1" {
// t.Error("param uid should be 1") t.Error("param uid should be 1")
// } }
// if context.pvalues[1] != "1" { if context.pvalues[1] != "1" {
// t.Error("param fid should be 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 { if h != nil {
t.Error("should not found handler") t.Error("should not found handler")
} }
@ -354,8 +345,7 @@ func TestRouterMatchAny(t *testing.T) {
t.Fatal("handler not found") t.Fatal("handler not found")
} }
if context.pvalues[0] != "" { if context.pvalues[0] != "" {
println(context.pvalues[0]) t.Error("value should be empty")
t.Error("value should be joe")
} }
h, _ = r.Find(GET, "/users/joe", context) h, _ = r.Find(GET, "/users/joe", context)
@ -634,7 +624,7 @@ func TestRouterServeHTTP(t *testing.T) {
func (n *node) printTree(pfx string, tail bool) { func (n *node) printTree(pfx string, tail bool) {
p := prefix(tail, pfx, "└── ", "├── ") 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 children := n.children
l := len(children) l := len(children)