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
73f0f4f3
Commit
73f0f4f3
authored
Jun 12, 2013
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #59 from lunny/master
Add a basic map for url params to controller properties.
parents
b2abb4eb
f20ad091
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
2 deletions
+89
-2
router.go
router.go
+89
-2
No files found.
router.go
View file @
73f0f4f3
...
...
@@ -7,9 +7,14 @@ import (
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
)
var
(
sc
*
Controller
=
&
Controller
{}
)
type
controllerInfo
struct
{
pattern
string
regex
*
regexp
.
Regexp
...
...
@@ -44,7 +49,7 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface) {
if
strings
.
HasPrefix
(
part
,
":"
)
{
expr
:=
"(.+)"
//a user may choose to override the defult expression
// similar to expressjs: ‘/user/:id([0-9]+)’
// similar to expressjs: ‘/user/:id([0-9]+)’
if
index
:=
strings
.
Index
(
part
,
"("
);
index
!=
-
1
{
expr
=
part
[
index
:
]
part
=
part
[
:
index
]
...
...
@@ -118,7 +123,7 @@ func (p *ControllerRegistor) AddHandler(pattern string, c http.Handler) {
if
strings
.
HasPrefix
(
part
,
":"
)
{
expr
:=
"([^/]+)"
//a user may choose to override the defult expression
// similar to expressjs: ‘/user/:id([0-9]+)’
// similar to expressjs: ‘/user/:id([0-9]+)’
if
index
:=
strings
.
Index
(
part
,
"("
);
index
!=
-
1
{
expr
=
part
[
index
:
]
part
=
part
[
:
index
]
...
...
@@ -183,6 +188,85 @@ func (p *ControllerRegistor) FilterPrefixPath(path string, filter http.HandlerFu
})
}
func
StructMap
(
vc
reflect
.
Value
,
r
*
http
.
Request
)
error
{
for
k
,
t
:=
range
r
.
Form
{
v
:=
t
[
0
]
names
:=
strings
.
Split
(
k
,
"."
)
var
value
reflect
.
Value
=
vc
for
i
,
name
:=
range
names
{
name
=
strings
.
Title
(
name
)
if
i
==
0
{
if
reflect
.
ValueOf
(
sc
)
.
Elem
()
.
FieldByName
(
name
)
.
IsValid
()
{
Trace
(
"Controller's property should not be changed by mapper."
)
break
}
}
if
value
.
Kind
()
!=
reflect
.
Struct
{
Trace
(
fmt
.
Sprintf
(
"arg error, value kind is %v"
,
value
.
Kind
()))
break
}
if
i
!=
len
(
names
)
-
1
{
value
=
value
.
FieldByName
(
name
)
if
!
value
.
IsValid
()
{
Trace
(
fmt
.
Sprintf
(
"(%v value is not valid %v)"
,
name
,
value
))
break
}
}
else
{
tv
:=
value
.
FieldByName
(
name
)
if
!
tv
.
IsValid
()
{
Trace
(
fmt
.
Sprintf
(
"struct %v has no field named %v"
,
value
,
name
))
break
}
if
!
tv
.
CanSet
()
{
Trace
(
"can not set "
+
k
)
break
}
var
l
interface
{}
switch
k
:=
tv
.
Kind
();
k
{
case
reflect
.
String
:
l
=
v
case
reflect
.
Bool
:
l
=
(
v
==
"true"
)
case
reflect
.
Int
,
reflect
.
Int8
,
reflect
.
Int16
,
reflect
.
Int32
:
x
,
err
:=
strconv
.
Atoi
(
v
)
if
err
!=
nil
{
Trace
(
"arg "
+
v
+
" as int: "
+
err
.
Error
())
break
}
l
=
x
case
reflect
.
Int64
:
x
,
err
:=
strconv
.
ParseInt
(
v
,
10
,
64
)
if
err
!=
nil
{
Trace
(
"arg "
+
v
+
" as int: "
+
err
.
Error
())
break
}
l
=
x
case
reflect
.
Float32
,
reflect
.
Float64
:
x
,
err
:=
strconv
.
ParseFloat
(
v
,
64
)
if
err
!=
nil
{
Trace
(
"arg "
+
v
+
" as float64: "
+
err
.
Error
())
break
}
l
=
x
case
reflect
.
Uint8
,
reflect
.
Uint16
,
reflect
.
Uint32
,
reflect
.
Uint64
:
x
,
err
:=
strconv
.
ParseUint
(
v
,
10
,
64
)
if
err
!=
nil
{
Trace
(
"arg "
+
v
+
" as int: "
+
err
.
Error
())
break
}
l
=
x
case
reflect
.
Struct
:
Trace
(
"can not set an struct"
)
}
tv
.
Set
(
reflect
.
ValueOf
(
l
))
}
}
}
return
nil
}
// AutoRoute
func
(
p
*
ControllerRegistor
)
ServeHTTP
(
rw
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
defer
func
()
{
...
...
@@ -347,10 +431,13 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
//Invoke the request handler
vc
:=
reflect
.
New
(
runrouter
.
controllerType
)
StructMap
(
vc
.
Elem
(),
r
)
//call the controller init function
init
:=
vc
.
MethodByName
(
"Init"
)
in
:=
make
([]
reflect
.
Value
,
2
)
ct
:=
&
Context
{
ResponseWriter
:
w
,
Request
:
r
,
Params
:
params
}
in
[
0
]
=
reflect
.
ValueOf
(
ct
)
in
[
1
]
=
reflect
.
ValueOf
(
runrouter
.
controllerType
.
Name
())
init
.
Call
(
in
)
...
...
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