Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
8cc57e2f
Commit
8cc57e2f
authored
Apr 05, 2015
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix #1112
parent
322b2085
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
19 deletions
+67
-19
tree.go
tree.go
+23
-19
tree_test.go
tree_test.go
+44
-0
No files found.
tree.go
View file @
8cc57e2f
...
...
@@ -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
{
...
...
tree_test.go
View file @
8cc57e2f
...
...
@@ -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
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment