From ab1e479adac1bde81caf6f7ee895de16a977596f Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Thu, 30 Apr 2015 18:47:13 -0700 Subject: [PATCH] #46 Signed-off-by: Vishal Rana --- router.go | 11 +++++++++-- router_test.go | 36 +++++++++++++----------------------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/router.go b/router.go index 5e1dd385..cdbb9749 100644 --- a/router.go +++ b/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 diff --git a/router_test.go b/router_test.go index 6d425df8..ea39f750 100644 --- a/router_test.go +++ b/router_test.go @@ -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)