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
2dee3018
Commit
2dee3018
authored
May 28, 2013
by
Lunny Xiao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add structmap function for controller
parent
4fc49b58
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
0 deletions
+68
-0
router.go
router.go
+68
-0
No files found.
router.go
View file @
2dee3018
package
beego
import
(
"errors"
"fmt"
"net/http"
"net/url"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
)
...
...
@@ -183,6 +185,69 @@ func (p *ControllerRegistor) FilterPrefixPath(path string, filter http.HandlerFu
})
}
func
structMap
(
vc
reflect
.
Value
,
params
*
map
[
string
]
string
)
error
{
for
k
,
v
:=
range
*
params
{
names
:=
strings
.
Split
(
k
,
"."
)
var
value
reflect
.
Value
=
vc
for
i
,
name
:=
range
names
{
if
i
!=
len
(
names
)
-
1
{
if
value
.
Kind
()
!=
reflect
.
Struct
{
return
errors
.
New
(
"arg error"
)
}
value
:=
value
.
FieldByName
(
name
)
if
!
value
.
IsValid
()
{
return
errors
.
New
(
"arg error"
)
}
}
else
{
tv
:=
value
.
FieldByName
(
name
)
fmt
.
Println
(
name
,
tv
,
tv
.
Kind
())
if
!
tv
.
IsValid
()
{
return
errors
.
New
(
"arg error"
)
}
if
!
tv
.
CanSet
()
{
return
errors
.
New
(
"can not set "
+
name
)
}
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
{
return
errors
.
New
(
"arg "
+
v
+
" as int: "
+
err
.
Error
())
}
l
=
x
case
reflect
.
Int64
:
x
,
err
:=
strconv
.
ParseInt
(
v
,
10
,
64
)
if
err
!=
nil
{
return
errors
.
New
(
"arg "
+
v
+
" as int: "
+
err
.
Error
())
}
l
=
x
case
reflect
.
Float32
,
reflect
.
Float64
:
x
,
err
:=
strconv
.
ParseFloat
(
v
,
64
)
if
err
!=
nil
{
return
errors
.
New
(
"arg "
+
v
+
" as float64: "
+
err
.
Error
())
}
l
=
x
case
reflect
.
Uint8
,
reflect
.
Uint16
,
reflect
.
Uint32
,
reflect
.
Uint64
:
x
,
err
:=
strconv
.
ParseUint
(
v
,
10
,
64
)
if
err
!=
nil
{
return
errors
.
New
(
"arg "
+
v
+
" as int: "
+
err
.
Error
())
}
l
=
x
case
reflect
.
Struct
:
fmt
.
Println
(
"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
()
{
...
...
@@ -351,6 +416,9 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
init
:=
vc
.
MethodByName
(
"Init"
)
in
:=
make
([]
reflect
.
Value
,
2
)
ct
:=
&
Context
{
ResponseWriter
:
w
,
Request
:
r
,
Params
:
params
}
structMap
(
vc
.
Elem
(),
&
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