fix #1112
Showing
2 changed files
with
67 additions
and
19 deletions
| ... | @@ -70,18 +70,23 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st | ... | @@ -70,18 +70,23 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st |
| 70 | } else { | 70 | } else { |
| 71 | regexpStr = "/" + regexpStr | 71 | regexpStr = "/" + regexpStr |
| 72 | } | 72 | } |
| 73 | } else { | 73 | } else if reg != "" { |
| 74 | for _, w := range wildcards { | 74 | if seg == "*.*" { |
| 75 | if w == "." || w == ":" { | 75 | regexpStr = "([^.]+).(.+)" |
| 76 | continue | 76 | } else { |
| 77 | for _, w := range params { | ||
| 78 | if w == "." || w == ":" { | ||
| 79 | continue | ||
| 80 | } | ||
| 81 | regexpStr = "([^/]+)/" + regexpStr | ||
| 77 | } | 82 | } |
| 78 | regexpStr = "([^/]+)/" + regexpStr | ||
| 79 | } | 83 | } |
| 80 | } | 84 | } |
| 81 | reg = strings.Trim(reg+regexpStr, "/") | 85 | reg = strings.Trim(reg+"/"+regexpStr, "/") |
| 82 | filterTreeWithPrefix(tree, append(wildcards, params...), reg) | 86 | filterTreeWithPrefix(tree, append(wildcards, params...), reg) |
| 83 | t.wildcard = tree | 87 | t.wildcard = tree |
| 84 | } else { | 88 | } else { |
| 89 | reg = strings.Trim(reg+"/"+regexpStr, "/") | ||
| 85 | filterTreeWithPrefix(tree, append(wildcards, params...), reg) | 90 | filterTreeWithPrefix(tree, append(wildcards, params...), reg) |
| 86 | t.fixrouters[seg] = tree | 91 | t.fixrouters[seg] = tree |
| 87 | } | 92 | } |
| ... | @@ -104,23 +109,23 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st | ... | @@ -104,23 +109,23 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st |
| 104 | rr = rr + "([^/]+)/" | 109 | rr = rr + "([^/]+)/" |
| 105 | } | 110 | } |
| 106 | } | 111 | } |
| 107 | regexpStr = rr + regexpStr + "/" | 112 | regexpStr = rr + regexpStr |
| 108 | } else { | 113 | } else { |
| 109 | regexpStr = "/" + regexpStr + "/" | 114 | regexpStr = "/" + regexpStr |
| 110 | } | 115 | } |
| 111 | } else { | 116 | } else if reg != "" { |
| 112 | for _, w := range wildcards { | 117 | if seg == "*.*" { |
| 113 | if w == "." || w == ":" { | 118 | regexpStr = "([^.]+).(.+)" |
| 114 | continue | 119 | } else { |
| 115 | } | 120 | for _, w := range params { |
| 116 | if w == ":splat" { | 121 | if w == "." || w == ":" { |
| 117 | regexpStr = "(.+)/" + regexpStr | 122 | continue |
| 118 | } else { | 123 | } |
| 119 | regexpStr = "([^/]+)/" + regexpStr | 124 | regexpStr = "([^/]+)/" + regexpStr |
| 120 | } | 125 | } |
| 121 | } | 126 | } |
| 122 | } | 127 | } |
| 123 | reg = reg + regexpStr | 128 | reg = strings.TrimRight(strings.TrimRight(reg, "/")+"/"+regexpStr, "/") |
| 124 | t.wildcard.addtree(segments[1:], tree, append(wildcards, params...), reg) | 129 | t.wildcard.addtree(segments[1:], tree, append(wildcards, params...), reg) |
| 125 | } else { | 130 | } else { |
| 126 | subTree := NewTree() | 131 | subTree := NewTree() |
| ... | @@ -140,7 +145,7 @@ func filterTreeWithPrefix(t *Tree, wildcards []string, reg string) { | ... | @@ -140,7 +145,7 @@ func filterTreeWithPrefix(t *Tree, wildcards []string, reg string) { |
| 140 | if reg != "" { | 145 | if reg != "" { |
| 141 | if l.regexps != nil { | 146 | if l.regexps != nil { |
| 142 | l.wildcards = append(wildcards, l.wildcards...) | 147 | l.wildcards = append(wildcards, l.wildcards...) |
| 143 | l.regexps = regexp.MustCompile("^" + reg + strings.Trim(l.regexps.String(), "^$") + "$") | 148 | l.regexps = regexp.MustCompile("^" + reg + "/" + strings.Trim(l.regexps.String(), "^$") + "$") |
| 144 | } else { | 149 | } else { |
| 145 | for _, v := range l.wildcards { | 150 | for _, v := range l.wildcards { |
| 146 | if v == ":" || v == "." { | 151 | if v == ":" || v == "." { |
| ... | @@ -248,7 +253,6 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string, | ... | @@ -248,7 +253,6 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string, |
| 248 | regexpStr = "/([^/]+)" + regexpStr | 253 | regexpStr = "/([^/]+)" + regexpStr |
| 249 | } | 254 | } |
| 250 | } | 255 | } |
| 251 | |||
| 252 | } | 256 | } |
| 253 | t.wildcard.addseg(segments[1:], route, append(wildcards, params...), reg+regexpStr) | 257 | t.wildcard.addseg(segments[1:], route, append(wildcards, params...), reg+regexpStr) |
| 254 | } else { | 258 | } else { | ... | ... |
| ... | @@ -148,6 +148,50 @@ func TestAddTree2(t *testing.T) { | ... | @@ -148,6 +148,50 @@ func TestAddTree2(t *testing.T) { |
| 148 | } | 148 | } |
| 149 | } | 149 | } |
| 150 | 150 | ||
| 151 | func TestAddTree3(t *testing.T) { | ||
| 152 | tr := NewTree() | ||
| 153 | tr.AddRouter("/create", "astaxie") | ||
| 154 | tr.AddRouter("/shop/:sd/account", "astaxie") | ||
| 155 | t3 := NewTree() | ||
| 156 | t3.AddTree("/table/:num", tr) | ||
| 157 | obj, param := t3.Match("/table/123/shop/123/account") | ||
| 158 | if obj == nil || obj.(string) != "astaxie" { | ||
| 159 | t.Fatal("/table/:num/shop/:sd/account can't get obj ") | ||
| 160 | } | ||
| 161 | if param == nil { | ||
| 162 | t.Fatal("get param error") | ||
| 163 | } | ||
| 164 | if param[":num"] != "123" || param[":sd"] != "123" { | ||
| 165 | t.Fatal("get :num :sd param error") | ||
| 166 | } | ||
| 167 | obj, param = t3.Match("/table/123/create") | ||
| 168 | if obj == nil || obj.(string) != "astaxie" { | ||
| 169 | t.Fatal("/table/:num/create can't get obj ") | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | func TestAddTree4(t *testing.T) { | ||
| 174 | tr := NewTree() | ||
| 175 | tr.AddRouter("/create", "astaxie") | ||
| 176 | tr.AddRouter("/shop/:sd/:account", "astaxie") | ||
| 177 | t4 := NewTree() | ||
| 178 | t4.AddTree("/:info:int/:num/:id", tr) | ||
| 179 | obj, param := t4.Match("/12/123/456/shop/123/account") | ||
| 180 | if obj == nil || obj.(string) != "astaxie" { | ||
| 181 | t.Fatal("/:info:int/:num/:id/shop/:sd/:account can't get obj ") | ||
| 182 | } | ||
| 183 | if param == nil { | ||
| 184 | t.Fatal("get param error") | ||
| 185 | } | ||
| 186 | if param[":info"] != "12" || param[":num"] != "123" || param[":id"] != "456" || param[":sd"] != "123" || param[":account"] != "account" { | ||
| 187 | t.Fatal("get :info :num :id :sd :account param error") | ||
| 188 | } | ||
| 189 | obj, param = t4.Match("/12/123/456/create") | ||
| 190 | if obj == nil || obj.(string) != "astaxie" { | ||
| 191 | t.Fatal("/:info:int/:num/:id/create can't get obj ") | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 151 | func TestSplitPath(t *testing.T) { | 195 | func TestSplitPath(t *testing.T) { |
| 152 | a := splitPath("") | 196 | a := splitPath("") |
| 153 | if len(a) != 0 { | 197 | if len(a) != 0 { | ... | ... |
-
Please register or sign in to post a comment