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
8b7cba03
Commit
8b7cba03
authored
May 28, 2013
by
Lunny Xiao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add url params -> controller's member feature
parent
2dee3018
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
20 deletions
+41
-20
router.go
router.go
+41
-20
No files found.
router.go
View file @
8b7cba03
package
beego
import
(
"errors"
"fmt"
"net/http"
"net/url"
...
...
@@ -12,6 +11,10 @@ import (
"strings"
)
var
(
sc
*
Controller
=
&
Controller
{}
)
type
controllerInfo
struct
{
pattern
string
regex
*
regexp
.
Regexp
...
...
@@ -46,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
]
...
...
@@ -120,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
]
...
...
@@ -185,27 +188,40 @@ func (p *ControllerRegistor) FilterPrefixPath(path string, filter http.HandlerFu
})
}
func
structMap
(
vc
reflect
.
Value
,
params
*
map
[
string
]
string
)
error
{
for
k
,
v
:=
range
*
params
{
func
StructMap
(
vc
reflect
.
Value
,
params
*
url
.
Values
)
error
{
for
k
,
t
:=
range
*
params
{
v
:=
t
[
0
]
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"
)
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
}
value
:=
value
.
FieldByName
(
name
)
}
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
()
{
return
errors
.
New
(
"arg error"
)
Trace
(
fmt
.
Sprintf
(
"(%v value is not valid %v)"
,
name
,
value
))
break
}
}
else
{
tv
:=
value
.
FieldByName
(
name
)
fmt
.
Println
(
name
,
tv
,
tv
.
Kind
())
if
!
tv
.
IsValid
()
{
return
errors
.
New
(
"arg error"
)
Trace
(
fmt
.
Sprintf
(
"struct %v has no field named %v"
,
value
,
name
))
break
}
if
!
tv
.
CanSet
()
{
return
errors
.
New
(
"can not set "
+
name
)
Trace
(
"can not set "
+
k
)
break
}
var
l
interface
{}
switch
k
:=
tv
.
Kind
();
k
{
...
...
@@ -216,29 +232,33 @@ func structMap(vc reflect.Value, params *map[string]string) error {
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
())
Trace
(
"arg "
+
v
+
" as int: "
+
err
.
Error
())
break
}
l
=
x
case
reflect
.
Int64
:
x
,
err
:=
strconv
.
ParseInt
(
v
,
10
,
64
)
if
err
!=
nil
{
return
errors
.
New
(
"arg "
+
v
+
" as int: "
+
err
.
Error
())
Trace
(
"arg "
+
v
+
" as int: "
+
err
.
Error
())
break
}
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
())
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
{
return
errors
.
New
(
"arg "
+
v
+
" as int: "
+
err
.
Error
())
Trace
(
"arg "
+
v
+
" as int: "
+
err
.
Error
())
break
}
l
=
x
case
reflect
.
Struct
:
fmt
.
Println
(
"can not set an struct"
)
Trace
(
"can not set an struct"
)
}
tv
.
Set
(
reflect
.
ValueOf
(
l
))
...
...
@@ -412,13 +432,14 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
//Invoke the request handler
vc
:=
reflect
.
New
(
runrouter
.
controllerType
)
r
.
ParseForm
()
StructMap
(
vc
.
Elem
(),
&
r
.
Form
)
//call the controller init function
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