beego:fix the some regexp routes to different func
Showing
4 changed files
with
62 additions
and
43 deletions
| ... | @@ -256,11 +256,12 @@ func addPrefix(t *Tree, prefix string) { | ... | @@ -256,11 +256,12 @@ func addPrefix(t *Tree, prefix string) { |
| 256 | if t.wildcard != nil { | 256 | if t.wildcard != nil { |
| 257 | addPrefix(t.wildcard, prefix) | 257 | addPrefix(t.wildcard, prefix) |
| 258 | } | 258 | } |
| 259 | if t.leaf != nil { | 259 | for _, l := range t.leaves { |
| 260 | if c, ok := t.leaf.runObject.(*controllerInfo); ok { | 260 | if c, ok := l.runObject.(*controllerInfo); ok { |
| 261 | c.pattern = prefix + c.pattern | 261 | c.pattern = prefix + c.pattern |
| 262 | } | 262 | } |
| 263 | } | 263 | } |
| 264 | |||
| 264 | } | 265 | } |
| 265 | 266 | ||
| 266 | // Namespace Condition | 267 | // Namespace Condition | ... | ... |
| ... | @@ -423,8 +423,8 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin | ... | @@ -423,8 +423,8 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin |
| 423 | return ok, u | 423 | return ok, u |
| 424 | } | 424 | } |
| 425 | } | 425 | } |
| 426 | if t.leaf != nil { | 426 | for _, l := range t.leaves { |
| 427 | if c, ok := t.leaf.runObject.(*controllerInfo); ok { | 427 | if c, ok := l.runObject.(*controllerInfo); ok { |
| 428 | if c.routerType == routerTypeBeego && c.controllerType.Name() == controllName { | 428 | if c.routerType == routerTypeBeego && c.controllerType.Name() == controllName { |
| 429 | find := false | 429 | find := false |
| 430 | if _, ok := HTTPMETHOD[strings.ToUpper(methodName)]; ok { | 430 | if _, ok := HTTPMETHOD[strings.ToUpper(methodName)]; ok { |
| ... | @@ -442,20 +442,20 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin | ... | @@ -442,20 +442,20 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin |
| 442 | } | 442 | } |
| 443 | } | 443 | } |
| 444 | if find { | 444 | if find { |
| 445 | if t.leaf.regexps == nil { | 445 | if l.regexps == nil { |
| 446 | if len(t.leaf.wildcards) == 0 { | 446 | if len(l.wildcards) == 0 { |
| 447 | return true, url | 447 | return true, url |
| 448 | } | 448 | } |
| 449 | if len(t.leaf.wildcards) == 1 { | 449 | if len(l.wildcards) == 1 { |
| 450 | if v, ok := params[t.leaf.wildcards[0]]; ok { | 450 | if v, ok := params[l.wildcards[0]]; ok { |
| 451 | delete(params, t.leaf.wildcards[0]) | 451 | delete(params, l.wildcards[0]) |
| 452 | return true, url + "/" + v + tourl(params) | 452 | return true, url + "/" + v + tourl(params) |
| 453 | } | 453 | } |
| 454 | if t.leaf.wildcards[0] == ":splat" { | 454 | if l.wildcards[0] == ":splat" { |
| 455 | return true, url + tourl(params) | 455 | return true, url + tourl(params) |
| 456 | } | 456 | } |
| 457 | } | 457 | } |
| 458 | if len(t.leaf.wildcards) == 3 && t.leaf.wildcards[0] == "." { | 458 | if len(l.wildcards) == 3 && l.wildcards[0] == "." { |
| 459 | if p, ok := params[":path"]; ok { | 459 | if p, ok := params[":path"]; ok { |
| 460 | if e, isok := params[":ext"]; isok { | 460 | if e, isok := params[":ext"]; isok { |
| 461 | delete(params, ":path") | 461 | delete(params, ":path") |
| ... | @@ -465,7 +465,7 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin | ... | @@ -465,7 +465,7 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin |
| 465 | } | 465 | } |
| 466 | } | 466 | } |
| 467 | canskip := false | 467 | canskip := false |
| 468 | for _, v := range t.leaf.wildcards { | 468 | for _, v := range l.wildcards { |
| 469 | if v == ":" { | 469 | if v == ":" { |
| 470 | canskip = true | 470 | canskip = true |
| 471 | continue | 471 | continue |
| ... | @@ -485,33 +485,33 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin | ... | @@ -485,33 +485,33 @@ func (p *ControllerRegistor) geturl(t *Tree, url, controllName, methodName strin |
| 485 | } else { | 485 | } else { |
| 486 | var i int | 486 | var i int |
| 487 | var startreg bool | 487 | var startreg bool |
| 488 | url = url + "/" | 488 | regurl := "" |
| 489 | for _, v := range t.leaf.regexps.String() { | 489 | for _, v := range strings.Trim(l.regexps.String(), "^$") { |
| 490 | if v == '(' { | 490 | if v == '(' { |
| 491 | startreg = true | 491 | startreg = true |
| 492 | continue | 492 | continue |
| 493 | } else if v == ')' { | 493 | } else if v == ')' { |
| 494 | startreg = false | 494 | startreg = false |
| 495 | if v, ok := params[t.leaf.wildcards[i]]; ok { | 495 | if v, ok := params[l.wildcards[i]]; ok { |
| 496 | url = url + v | 496 | delete(params, l.wildcards[i]) |
| 497 | regurl = regurl + v | ||
| 497 | i++ | 498 | i++ |
| 498 | } else { | 499 | } else { |
| 499 | break | 500 | break |
| 500 | } | 501 | } |
| 501 | } else if !startreg { | 502 | } else if !startreg { |
| 502 | url = string(append([]rune(url), v)) | 503 | regurl = string(append([]rune(regurl), v)) |
| 503 | } | 504 | } |
| 504 | } | 505 | } |
| 505 | if t.leaf.regexps.MatchString(url) { | 506 | if l.regexps.MatchString(regurl) { |
| 506 | return true, url | 507 | return true, url + "/" + regurl + tourl(params) |
| 507 | } | 508 | } |
| 508 | } | 509 | } |
| 509 | } else { | ||
| 510 | return false, "" | ||
| 511 | } | 510 | } |
| 512 | } | 511 | } |
| 513 | } | 512 | } |
| 514 | } | 513 | } |
| 514 | |||
| 515 | return false, "" | 515 | return false, "" |
| 516 | } | 516 | } |
| 517 | 517 | ... | ... |
| ... | @@ -95,6 +95,22 @@ func TestUrlFor(t *testing.T) { | ... | @@ -95,6 +95,22 @@ func TestUrlFor(t *testing.T) { |
| 95 | } | 95 | } |
| 96 | } | 96 | } |
| 97 | 97 | ||
| 98 | func TestUrlFor2(t *testing.T) { | ||
| 99 | handler := NewControllerRegister() | ||
| 100 | handler.Add("/v1/:v/cms_:id(.+)_:page(.+).html", &TestController{}, "*:List") | ||
| 101 | handler.Add("/v1/:v(.+)_cms/ttt_:id(.+)_:page(.+).html", &TestController{}, "*:Param") | ||
| 102 | if handler.UrlFor("TestController.List", ":v", "za", ":id", "12", ":page", "123") != | ||
| 103 | "/v1/za/cms_12_123.html" { | ||
| 104 | Info(handler.UrlFor("TestController.List")) | ||
| 105 | t.Errorf("TestController.List must equal to /v1/za/cms_12_123.html") | ||
| 106 | } | ||
| 107 | if handler.UrlFor("TestController.Param", ":v", "za", ":id", "12", ":page", "123") != | ||
| 108 | "/v1/za_cms/ttt_12_123.html" { | ||
| 109 | Info(handler.UrlFor("TestController.Param")) | ||
| 110 | t.Errorf("TestController.List must equal to /v1/za_cms/ttt_12_123.html") | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 98 | func TestUserFunc(t *testing.T) { | 114 | func TestUserFunc(t *testing.T) { |
| 99 | r, _ := http.NewRequest("GET", "/api/list", nil) | 115 | r, _ := http.NewRequest("GET", "/api/list", nil) |
| 100 | w := httptest.NewRecorder() | 116 | w := httptest.NewRecorder() | ... | ... |
| ... | @@ -16,7 +16,7 @@ type Tree struct { | ... | @@ -16,7 +16,7 @@ type Tree struct { |
| 16 | wildcard *Tree | 16 | wildcard *Tree |
| 17 | 17 | ||
| 18 | //if set, failure to match wildcard search | 18 | //if set, failure to match wildcard search |
| 19 | leaf *leafInfo | 19 | leaves []*leafInfo |
| 20 | } | 20 | } |
| 21 | 21 | ||
| 22 | func NewTree() *Tree { | 22 | func NewTree() *Tree { |
| ... | @@ -86,35 +86,35 @@ func filterTreeWithPrefix(t *Tree, wildcards []string, reg string) { | ... | @@ -86,35 +86,35 @@ func filterTreeWithPrefix(t *Tree, wildcards []string, reg string) { |
| 86 | if t.wildcard != nil { | 86 | if t.wildcard != nil { |
| 87 | filterTreeWithPrefix(t.wildcard, wildcards, reg) | 87 | filterTreeWithPrefix(t.wildcard, wildcards, reg) |
| 88 | } | 88 | } |
| 89 | if t.leaf != nil { | 89 | for _, l := range t.leaves { |
| 90 | t.leaf.wildcards = append(wildcards, t.leaf.wildcards...) | 90 | l.wildcards = append(wildcards, l.wildcards...) |
| 91 | if reg != "" { | 91 | if reg != "" { |
| 92 | filterCards := []string{} | 92 | filterCards := []string{} |
| 93 | for _, v := range t.leaf.wildcards { | 93 | for _, v := range l.wildcards { |
| 94 | if v == ":" || v == "." { | 94 | if v == ":" || v == "." { |
| 95 | continue | 95 | continue |
| 96 | } | 96 | } |
| 97 | filterCards = append(filterCards, v) | 97 | filterCards = append(filterCards, v) |
| 98 | } | 98 | } |
| 99 | t.leaf.wildcards = filterCards | 99 | l.wildcards = filterCards |
| 100 | t.leaf.regexps = regexp.MustCompile("^" + reg + strings.Trim(t.leaf.regexps.String(), "^$") + "$") | 100 | l.regexps = regexp.MustCompile("^" + reg + strings.Trim(l.regexps.String(), "^$") + "$") |
| 101 | } else { | 101 | } else { |
| 102 | if t.leaf.regexps != nil { | 102 | if l.regexps != nil { |
| 103 | filterCards := []string{} | 103 | filterCards := []string{} |
| 104 | for _, v := range t.leaf.wildcards { | 104 | for _, v := range l.wildcards { |
| 105 | if v == ":" || v == "." { | 105 | if v == ":" || v == "." { |
| 106 | continue | 106 | continue |
| 107 | } | 107 | } |
| 108 | filterCards = append(filterCards, v) | 108 | filterCards = append(filterCards, v) |
| 109 | } | 109 | } |
| 110 | t.leaf.wildcards = filterCards | 110 | l.wildcards = filterCards |
| 111 | for _, w := range wildcards { | 111 | for _, w := range wildcards { |
| 112 | if w == "." || w == ":" { | 112 | if w == "." || w == ":" { |
| 113 | continue | 113 | continue |
| 114 | } | 114 | } |
| 115 | reg = "([^/]+)/" + reg | 115 | reg = "([^/]+)/" + reg |
| 116 | } | 116 | } |
| 117 | t.leaf.regexps = regexp.MustCompile("^" + reg + strings.Trim(t.leaf.regexps.String(), "^$") + "$") | 117 | l.regexps = regexp.MustCompile("^" + reg + strings.Trim(l.regexps.String(), "^$") + "$") |
| 118 | } | 118 | } |
| 119 | } | 119 | } |
| 120 | } | 120 | } |
| ... | @@ -137,9 +137,9 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string, | ... | @@ -137,9 +137,9 @@ func (t *Tree) addseg(segments []string, route interface{}, wildcards []string, |
| 137 | } | 137 | } |
| 138 | filterCards = append(filterCards, v) | 138 | filterCards = append(filterCards, v) |
| 139 | } | 139 | } |
| 140 | t.leaf = &leafInfo{runObject: route, wildcards: wildcards, regexps: regexp.MustCompile("^" + reg + "$")} | 140 | t.leaves = append(t.leaves, &leafInfo{runObject: route, wildcards: wildcards, regexps: regexp.MustCompile("^" + reg + "$")}) |
| 141 | } else { | 141 | } else { |
| 142 | t.leaf = &leafInfo{runObject: route, wildcards: wildcards} | 142 | t.leaves = append(t.leaves, &leafInfo{runObject: route, wildcards: wildcards}) |
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | } else { | 145 | } else { |
| ... | @@ -185,15 +185,18 @@ func (t *Tree) Match(pattern string) (runObject interface{}, params map[string]s | ... | @@ -185,15 +185,18 @@ func (t *Tree) Match(pattern string) (runObject interface{}, params map[string]s |
| 185 | func (t *Tree) match(segments []string, wildcardValues []string) (runObject interface{}, params map[string]string) { | 185 | func (t *Tree) match(segments []string, wildcardValues []string) (runObject interface{}, params map[string]string) { |
| 186 | // Handle leaf nodes: | 186 | // Handle leaf nodes: |
| 187 | if len(segments) == 0 { | 187 | if len(segments) == 0 { |
| 188 | if t.leaf != nil { | 188 | for _, l := range t.leaves { |
| 189 | if ok, pa := t.leaf.match(wildcardValues); ok { | 189 | if ok, pa := l.match(wildcardValues); ok { |
| 190 | return t.leaf.runObject, pa | 190 | return l.runObject, pa |
| 191 | } | 191 | } |
| 192 | } | 192 | } |
| 193 | if t.wildcard != nil && t.wildcard.leaf != nil { | 193 | if t.wildcard != nil { |
| 194 | if ok, pa := t.wildcard.leaf.match(wildcardValues); ok { | 194 | for _, l := range t.wildcard.leaves { |
| 195 | return t.wildcard.leaf.runObject, pa | 195 | if ok, pa := l.match(wildcardValues); ok { |
| 196 | return l.runObject, pa | ||
| 197 | } | ||
| 196 | } | 198 | } |
| 199 | |||
| 197 | } | 200 | } |
| 198 | return nil, nil | 201 | return nil, nil |
| 199 | } | 202 | } |
| ... | @@ -222,13 +225,12 @@ func (t *Tree) match(segments []string, wildcardValues []string) (runObject inte | ... | @@ -222,13 +225,12 @@ func (t *Tree) match(segments []string, wildcardValues []string) (runObject inte |
| 222 | runObject, params = t.wildcard.match(segs, append(wildcardValues, seg)) | 225 | runObject, params = t.wildcard.match(segs, append(wildcardValues, seg)) |
| 223 | } | 226 | } |
| 224 | if runObject == nil { | 227 | if runObject == nil { |
| 225 | if t.leaf != nil { | 228 | for _, l := range t.leaves { |
| 226 | if ok, pa := t.leaf.match(append(wildcardValues, segments...)); ok { | 229 | if ok, pa := l.match(append(wildcardValues, segments...)); ok { |
| 227 | return t.leaf.runObject, pa | 230 | return l.runObject, pa |
| 228 | } | 231 | } |
| 229 | } | 232 | } |
| 230 | } | 233 | } |
| 231 | |||
| 232 | return runObject, params | 234 | return runObject, params |
| 233 | } | 235 | } |
| 234 | 236 | ... | ... |
-
Please register or sign in to post a comment