mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 23:01:28 +00:00
fix #1112
This commit is contained in:
parent
5c84ada389
commit
e0e8b98622
42
tree.go
42
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 {
|
||||
|
44
tree_test.go
44
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 {
|
||||
|
Loading…
Reference in New Issue
Block a user