From 8cc57e2fc8928c52b154b063bf70fb452c5b1eb2 Mon Sep 17 00:00:00 2001 From: astaxie Date: Sun, 5 Apr 2015 23:11:50 +0800 Subject: [PATCH] fix #1112 --- tree.go | 42 +++++++++++++++++++++++------------------- tree_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/tree.go b/tree.go index 93e87470..e0d9c1c2 100644 --- a/tree.go +++ b/tree.go @@ -70,18 +70,23 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st } else { regexpStr = "/" + regexpStr } - } else { - for _, w := range wildcards { - if w == "." || w == ":" { - continue + } else if reg != "" { + if seg == "*.*" { + regexpStr = "([^.]+).(.+)" + } else { + for _, w := range params { + if w == "." || w == ":" { + continue + } + regexpStr = "([^/]+)/" + regexpStr } - regexpStr = "([^/]+)/" + regexpStr } } - reg = strings.Trim(reg+regexpStr, "/") + reg = strings.Trim(reg+"/"+regexpStr, "/") filterTreeWithPrefix(tree, append(wildcards, params...), reg) t.wildcard = tree } else { + reg = strings.Trim(reg+"/"+regexpStr, "/") filterTreeWithPrefix(tree, append(wildcards, params...), reg) t.fixrouters[seg] = tree } @@ -104,23 +109,23 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st rr = rr + "([^/]+)/" } } - regexpStr = rr + regexpStr + "/" + regexpStr = rr + regexpStr } else { - regexpStr = "/" + regexpStr + "/" + regexpStr = "/" + regexpStr } - } else { - for _, w := range wildcards { - if w == "." || w == ":" { - continue - } - if w == ":splat" { - regexpStr = "(.+)/" + regexpStr - } else { + } else if reg != "" { + if seg == "*.*" { + regexpStr = "([^.]+).(.+)" + } else { + for _, w := range params { + if w == "." || w == ":" { + continue + } regexpStr = "([^/]+)/" + regexpStr } } } - reg = reg + regexpStr + reg = strings.TrimRight(strings.TrimRight(reg, "/")+"/"+regexpStr, "/") t.wildcard.addtree(segments[1:], tree, append(wildcards, params...), reg) } else { subTree := NewTree() @@ -140,7 +145,7 @@ func filterTreeWithPrefix(t *Tree, wildcards []string, reg string) { if reg != "" { if l.regexps != nil { l.wildcards = append(wildcards, l.wildcards...) - l.regexps = regexp.MustCompile("^" + reg + strings.Trim(l.regexps.String(), "^$") + "$") + l.regexps = regexp.MustCompile("^" + reg + "/" + strings.Trim(l.regexps.String(), "^$") + "$") } else { for _, v := range l.wildcards { if v == ":" || v == "." { @@ -248,7 +253,6 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string, regexpStr = "/([^/]+)" + regexpStr } } - } t.wildcard.addseg(segments[1:], route, append(wildcards, params...), reg+regexpStr) } else { diff --git a/tree_test.go b/tree_test.go index fa289716..1d2890e1 100644 --- a/tree_test.go +++ b/tree_test.go @@ -148,6 +148,50 @@ func TestAddTree2(t *testing.T) { } } +func TestAddTree3(t *testing.T) { + tr := NewTree() + tr.AddRouter("/create", "astaxie") + tr.AddRouter("/shop/:sd/account", "astaxie") + t3 := NewTree() + t3.AddTree("/table/:num", tr) + obj, param := t3.Match("/table/123/shop/123/account") + if obj == nil || obj.(string) != "astaxie" { + t.Fatal("/table/:num/shop/:sd/account can't get obj ") + } + if param == nil { + t.Fatal("get param error") + } + if param[":num"] != "123" || param[":sd"] != "123" { + t.Fatal("get :num :sd param error") + } + obj, param = t3.Match("/table/123/create") + if obj == nil || obj.(string) != "astaxie" { + t.Fatal("/table/:num/create can't get obj ") + } +} + +func TestAddTree4(t *testing.T) { + tr := NewTree() + tr.AddRouter("/create", "astaxie") + tr.AddRouter("/shop/:sd/:account", "astaxie") + t4 := NewTree() + t4.AddTree("/:info:int/:num/:id", tr) + obj, param := t4.Match("/12/123/456/shop/123/account") + if obj == nil || obj.(string) != "astaxie" { + t.Fatal("/:info:int/:num/:id/shop/:sd/:account can't get obj ") + } + if param == nil { + t.Fatal("get param error") + } + if param[":info"] != "12" || param[":num"] != "123" || param[":id"] != "456" || param[":sd"] != "123" || param[":account"] != "account" { + t.Fatal("get :info :num :id :sd :account param error") + } + obj, param = t4.Match("/12/123/456/create") + if obj == nil || obj.(string) != "astaxie" { + t.Fatal("/:info:int/:num/:id/create can't get obj ") + } +} + func TestSplitPath(t *testing.T) { a := splitPath("") if len(a) != 0 {