1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 18:50:55 +00:00

beego: fix the tree addTree for regexp

This commit is contained in:
astaxie 2014-08-12 00:02:27 +08:00
parent 8ed6d06572
commit 52fdfc5665

54
tree.go
View File

@ -46,22 +46,38 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st
} }
seg := segments[0] seg := segments[0]
iswild, params, regexpStr := splitSegment(seg) iswild, params, regexpStr := splitSegment(seg)
if len(segments) == 1 && seg != "" { if len(segments) == 1 {
if iswild { if iswild {
wildcards = append(wildcards, params...)
if regexpStr != "" { if regexpStr != "" {
for _, w := range params { if reg == "" {
rr := ""
for _, w := range wildcards {
if w == "." || w == ":" {
continue
}
if w == ":splat" {
rr = rr + "(.+)/"
} else {
rr = rr + "([^/]+)/"
}
}
regexpStr = rr + regexpStr
} else {
regexpStr = "/" + regexpStr
}
} else {
for _, w := range wildcards {
if w == "." || w == ":" { if w == "." || w == ":" {
continue continue
} }
regexpStr = "([^/]+)/" + regexpStr regexpStr = "/([^/]+)" + regexpStr
} }
} }
reg = reg + regexpStr reg = reg + regexpStr
filterTreeWithPrefix(tree, wildcards, reg) filterTreeWithPrefix(tree, append(wildcards, params...), reg)
t.wildcard = tree t.wildcard = tree
} else { } else {
filterTreeWithPrefix(tree, wildcards, reg) filterTreeWithPrefix(tree, append(wildcards, params...), reg)
t.fixrouters[seg] = tree t.fixrouters[seg] = tree
} }
return return
@ -70,21 +86,37 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st
if t.wildcard == nil { if t.wildcard == nil {
t.wildcard = NewTree() t.wildcard = NewTree()
} }
wildcards = append(wildcards)
if regexpStr != "" { if regexpStr != "" {
for _, w := range params { if reg == "" {
rr := ""
for _, w := range wildcards {
if w == "." || w == ":" {
continue
}
if w == ":splat" {
rr = rr + "(.+)/"
} else {
rr = rr + "([^/]+)/"
}
}
regexpStr = rr + regexpStr
} else {
regexpStr = "/" + regexpStr
}
} else {
for _, w := range wildcards {
if w == "." || w == ":" { if w == "." || w == ":" {
continue continue
} }
regexpStr = "([^/]+)/" + regexpStr regexpStr = "/([^/]+)" + regexpStr
} }
} }
reg = reg + regexpStr reg = reg + regexpStr
t.wildcard.addtree(segments[1:], tree, wildcards, reg) t.wildcard.addtree(segments[1:], tree, append(wildcards, params...), reg)
} else { } else {
subTree := NewTree() subTree := NewTree()
t.fixrouters[seg] = subTree t.fixrouters[seg] = subTree
subTree.addtree(segments[1:], tree, wildcards, reg) subTree.addtree(segments[1:], tree, append(wildcards, params...), reg)
} }
} }