beego: fix the tree for addtree & add testcase
Showing
2 changed files
with
42 additions
and
12 deletions
| ... | @@ -70,10 +70,10 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st | ... | @@ -70,10 +70,10 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st |
| 70 | if w == "." || w == ":" { | 70 | if w == "." || w == ":" { |
| 71 | continue | 71 | continue |
| 72 | } | 72 | } |
| 73 | regexpStr = "/([^/]+)" + regexpStr | 73 | regexpStr = "([^/]+)/" + regexpStr |
| 74 | } | 74 | } |
| 75 | } | 75 | } |
| 76 | reg = reg + regexpStr | 76 | reg = strings.Trim(reg+regexpStr, "/") |
| 77 | filterTreeWithPrefix(tree, append(wildcards, params...), reg) | 77 | filterTreeWithPrefix(tree, append(wildcards, params...), reg) |
| 78 | t.wildcard = tree | 78 | t.wildcard = tree |
| 79 | } else { | 79 | } else { |
| ... | @@ -99,9 +99,9 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st | ... | @@ -99,9 +99,9 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st |
| 99 | rr = rr + "([^/]+)/" | 99 | rr = rr + "([^/]+)/" |
| 100 | } | 100 | } |
| 101 | } | 101 | } |
| 102 | regexpStr = rr + regexpStr | 102 | regexpStr = rr + regexpStr + "/" |
| 103 | } else { | 103 | } else { |
| 104 | regexpStr = "/" + regexpStr | 104 | regexpStr = "/" + regexpStr + "/" |
| 105 | } | 105 | } |
| 106 | } else { | 106 | } else { |
| 107 | for _, w := range wildcards { | 107 | for _, w := range wildcards { |
| ... | @@ -109,9 +109,9 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st | ... | @@ -109,9 +109,9 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st |
| 109 | continue | 109 | continue |
| 110 | } | 110 | } |
| 111 | if w == ":splat" { | 111 | if w == ":splat" { |
| 112 | regexpStr = "/(.+)" + regexpStr | 112 | regexpStr = "(.+)/" + regexpStr |
| 113 | } else { | 113 | } else { |
| 114 | regexpStr = "/([^/]+)" + regexpStr | 114 | regexpStr = "([^/]+)/" + regexpStr |
| 115 | } | 115 | } |
| 116 | } | 116 | } |
| 117 | } | 117 | } |
| ... | @@ -132,8 +132,24 @@ func filterTreeWithPrefix(t *Tree, wildcards []string, reg string) { | ... | @@ -132,8 +132,24 @@ func filterTreeWithPrefix(t *Tree, wildcards []string, reg string) { |
| 132 | filterTreeWithPrefix(t.wildcard, wildcards, reg) | 132 | filterTreeWithPrefix(t.wildcard, wildcards, reg) |
| 133 | } | 133 | } |
| 134 | for _, l := range t.leaves { | 134 | for _, l := range t.leaves { |
| 135 | l.wildcards = append(wildcards, l.wildcards...) | ||
| 136 | if reg != "" { | 135 | if reg != "" { |
| 136 | if l.regexps != nil { | ||
| 137 | l.wildcards = append(wildcards, l.wildcards...) | ||
| 138 | l.regexps = regexp.MustCompile("^" + reg + strings.Trim(l.regexps.String(), "^$") + "$") | ||
| 139 | } else { | ||
| 140 | for _, v := range l.wildcards { | ||
| 141 | if v == ":" || v == "." { | ||
| 142 | continue | ||
| 143 | } | ||
| 144 | if v == ":splat" { | ||
| 145 | reg = reg + "/(.+)" | ||
| 146 | } else { | ||
| 147 | reg = reg + "/([^/]+)" | ||
| 148 | } | ||
| 149 | } | ||
| 150 | l.regexps = regexp.MustCompile("^" + reg + "$") | ||
| 151 | l.wildcards = append(wildcards, l.wildcards...) | ||
| 152 | } | ||
| 137 | filterCards := []string{} | 153 | filterCards := []string{} |
| 138 | for _, v := range l.wildcards { | 154 | for _, v := range l.wildcards { |
| 139 | if v == ":" || v == "." { | 155 | if v == ":" || v == "." { |
| ... | @@ -142,12 +158,8 @@ func filterTreeWithPrefix(t *Tree, wildcards []string, reg string) { | ... | @@ -142,12 +158,8 @@ func filterTreeWithPrefix(t *Tree, wildcards []string, reg string) { |
| 142 | filterCards = append(filterCards, v) | 158 | filterCards = append(filterCards, v) |
| 143 | } | 159 | } |
| 144 | l.wildcards = filterCards | 160 | l.wildcards = filterCards |
| 145 | if l.regexps != nil { | ||
| 146 | l.regexps = regexp.MustCompile("^" + reg + strings.Trim(l.regexps.String(), "^$") + "$") | ||
| 147 | } else { | ||
| 148 | l.regexps = regexp.MustCompile("^" + reg + "$") | ||
| 149 | } | ||
| 150 | } else { | 161 | } else { |
| 162 | l.wildcards = append(wildcards, l.wildcards...) | ||
| 151 | if l.regexps != nil { | 163 | if l.regexps != nil { |
| 152 | for _, w := range wildcards { | 164 | for _, w := range wildcards { |
| 153 | if w == "." || w == ":" { | 165 | if w == "." || w == ":" { | ... | ... |
| ... | @@ -122,6 +122,24 @@ func TestAddTree(t *testing.T) { | ... | @@ -122,6 +122,24 @@ func TestAddTree(t *testing.T) { |
| 122 | } | 122 | } |
| 123 | } | 123 | } |
| 124 | 124 | ||
| 125 | func TestAddTree2(t *testing.T) { | ||
| 126 | tr := NewTree() | ||
| 127 | tr.AddRouter("/shop/:id/account", "astaxie") | ||
| 128 | tr.AddRouter("/shop/:sd/ttt_:id(.+)_:page(.+).html", "astaxie") | ||
| 129 | t3 := NewTree() | ||
| 130 | t3.AddTree("/:version(v1|v2)/:prefix", tr) | ||
| 131 | obj, param := t3.Match("/v1/zl/shop/123/account") | ||
| 132 | if obj == nil || obj.(string) != "astaxie" { | ||
| 133 | t.Fatal("/:version(v1|v2)/:prefix/shop/:id/account can't get obj ") | ||
| 134 | } | ||
| 135 | if param == nil { | ||
| 136 | t.Fatal("get param error") | ||
| 137 | } | ||
| 138 | if param[":id"] != "123" || param[":prefix"] != "zl" || param[":version"] != "v1" { | ||
| 139 | t.Fatal("get :id :prefix :version param error") | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 125 | func TestSplitPath(t *testing.T) { | 143 | func TestSplitPath(t *testing.T) { |
| 126 | a := splitPath("/") | 144 | a := splitPath("/") |
| 127 | if len(a) != 0 { | 145 | if len(a) != 0 { | ... | ... |
-
Please register or sign in to post a comment