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
6a78898b
Commit
6a78898b
authored
Aug 12, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
beego: fix the tree for addtree & add testcase
parent
f201859f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
12 deletions
+42
-12
tree.go
tree.go
+24
-12
tree_test.go
tree_test.go
+18
-0
No files found.
tree.go
View file @
6a78898b
...
...
@@ -70,10 +70,10 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st
if
w
==
"."
||
w
==
":"
{
continue
}
regexpStr
=
"
/([^/]+)
"
+
regexpStr
regexpStr
=
"
([^/]+)/
"
+
regexpStr
}
}
reg
=
reg
+
regexpStr
reg
=
strings
.
Trim
(
reg
+
regexpStr
,
"/"
)
filterTreeWithPrefix
(
tree
,
append
(
wildcards
,
params
...
),
reg
)
t
.
wildcard
=
tree
}
else
{
...
...
@@ -99,9 +99,9 @@ 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
{
...
...
@@ -109,9 +109,9 @@ func (t *Tree) addtree(segments []string, tree *Tree, wildcards []string, reg st
continue
}
if
w
==
":splat"
{
regexpStr
=
"
/(.+)
"
+
regexpStr
regexpStr
=
"
(.+)/
"
+
regexpStr
}
else
{
regexpStr
=
"
/([^/]+)
"
+
regexpStr
regexpStr
=
"
([^/]+)/
"
+
regexpStr
}
}
}
...
...
@@ -132,8 +132,24 @@ func filterTreeWithPrefix(t *Tree, wildcards []string, reg string) {
filterTreeWithPrefix
(
t
.
wildcard
,
wildcards
,
reg
)
}
for
_
,
l
:=
range
t
.
leaves
{
l
.
wildcards
=
append
(
wildcards
,
l
.
wildcards
...
)
if
reg
!=
""
{
if
l
.
regexps
!=
nil
{
l
.
wildcards
=
append
(
wildcards
,
l
.
wildcards
...
)
l
.
regexps
=
regexp
.
MustCompile
(
"^"
+
reg
+
strings
.
Trim
(
l
.
regexps
.
String
(),
"^$"
)
+
"$"
)
}
else
{
for
_
,
v
:=
range
l
.
wildcards
{
if
v
==
":"
||
v
==
"."
{
continue
}
if
v
==
":splat"
{
reg
=
reg
+
"/(.+)"
}
else
{
reg
=
reg
+
"/([^/]+)"
}
}
l
.
regexps
=
regexp
.
MustCompile
(
"^"
+
reg
+
"$"
)
l
.
wildcards
=
append
(
wildcards
,
l
.
wildcards
...
)
}
filterCards
:=
[]
string
{}
for
_
,
v
:=
range
l
.
wildcards
{
if
v
==
":"
||
v
==
"."
{
...
...
@@ -142,12 +158,8 @@ func filterTreeWithPrefix(t *Tree, wildcards []string, reg string) {
filterCards
=
append
(
filterCards
,
v
)
}
l
.
wildcards
=
filterCards
if
l
.
regexps
!=
nil
{
l
.
regexps
=
regexp
.
MustCompile
(
"^"
+
reg
+
strings
.
Trim
(
l
.
regexps
.
String
(),
"^$"
)
+
"$"
)
}
else
{
l
.
regexps
=
regexp
.
MustCompile
(
"^"
+
reg
+
"$"
)
}
}
else
{
l
.
wildcards
=
append
(
wildcards
,
l
.
wildcards
...
)
if
l
.
regexps
!=
nil
{
for
_
,
w
:=
range
wildcards
{
if
w
==
"."
||
w
==
":"
{
...
...
tree_test.go
View file @
6a78898b
...
...
@@ -122,6 +122,24 @@ func TestAddTree(t *testing.T) {
}
}
func
TestAddTree2
(
t
*
testing
.
T
)
{
tr
:=
NewTree
()
tr
.
AddRouter
(
"/shop/:id/account"
,
"astaxie"
)
tr
.
AddRouter
(
"/shop/:sd/ttt_:id(.+)_:page(.+).html"
,
"astaxie"
)
t3
:=
NewTree
()
t3
.
AddTree
(
"/:version(v1|v2)/:prefix"
,
tr
)
obj
,
param
:=
t3
.
Match
(
"/v1/zl/shop/123/account"
)
if
obj
==
nil
||
obj
.
(
string
)
!=
"astaxie"
{
t
.
Fatal
(
"/:version(v1|v2)/:prefix/shop/:id/account can't get obj "
)
}
if
param
==
nil
{
t
.
Fatal
(
"get param error"
)
}
if
param
[
":id"
]
!=
"123"
||
param
[
":prefix"
]
!=
"zl"
||
param
[
":version"
]
!=
"v1"
{
t
.
Fatal
(
"get :id :prefix :version param error"
)
}
}
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