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
4c6de379
Commit
4c6de379
authored
Mar 05, 2017
by
astaxie
Committed by
GitHub
Mar 05, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2480 from ysqi/fix
Fixed #2456 and strengthen bind
parents
3d629e73
6d997366
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
87 additions
and
62 deletions
+87
-62
input.go
context/input.go
+10
-1
input_test.go
context/input_test.go
+77
-61
No files found.
context/input.go
View file @
4c6de379
...
...
@@ -413,7 +413,13 @@ func (input *BeegoInput) Bind(dest interface{}, key string) error {
if
!
value
.
CanSet
()
{
return
errors
.
New
(
"beego: non-settable variable passed to Bind: "
+
key
)
}
rv
:=
input
.
bind
(
key
,
value
.
Type
())
typ
:=
value
.
Type
()
// Get real type if dest define with interface{}.
// e.g var dest interface{} dest=1.0
if
value
.
Kind
()
==
reflect
.
Interface
{
typ
=
value
.
Elem
()
.
Type
()
}
rv
:=
input
.
bind
(
key
,
typ
)
if
!
rv
.
IsValid
()
{
return
errors
.
New
(
"beego: reflect value is empty"
)
}
...
...
@@ -422,6 +428,9 @@ func (input *BeegoInput) Bind(dest interface{}, key string) error {
}
func
(
input
*
BeegoInput
)
bind
(
key
string
,
typ
reflect
.
Type
)
reflect
.
Value
{
if
input
.
Context
.
Request
.
Form
==
nil
{
input
.
Context
.
Request
.
ParseForm
()
}
rv
:=
reflect
.
Zero
(
typ
)
switch
typ
.
Kind
()
{
case
reflect
.
Int
,
reflect
.
Int8
,
reflect
.
Int16
,
reflect
.
Int32
,
reflect
.
Int64
:
...
...
context/input_test.go
View file @
4c6de379
...
...
@@ -15,81 +15,97 @@
package
context
import
(
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func
TestParse
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&user.Name=astaxie"
,
nil
)
func
TestBind
(
t
*
testing
.
T
)
{
type
testItem
struct
{
field
string
empty
interface
{}
want
interface
{}
}
type
Human
struct
{
ID
int
Nick
string
Pwd
string
Ms
bool
}
cases
:=
[]
struct
{
request
string
valueGp
[]
testItem
}{
{
"/?p=str"
,
[]
testItem
{{
"p"
,
interface
{}(
""
),
interface
{}(
"str"
)}}},
{
"/?p="
,
[]
testItem
{{
"p"
,
""
,
""
}}},
{
"/?p=str"
,
[]
testItem
{{
"p"
,
""
,
"str"
}}},
{
"/?p=123"
,
[]
testItem
{{
"p"
,
0
,
123
}}},
{
"/?p=123"
,
[]
testItem
{{
"p"
,
uint
(
0
),
uint
(
123
)}}},
{
"/?p=1.0"
,
[]
testItem
{{
"p"
,
0.0
,
1.0
}}},
{
"/?p=1"
,
[]
testItem
{{
"p"
,
false
,
true
}}},
{
"/?p=true"
,
[]
testItem
{{
"p"
,
false
,
true
}}},
{
"/?p=ON"
,
[]
testItem
{{
"p"
,
false
,
true
}}},
{
"/?p=on"
,
[]
testItem
{{
"p"
,
false
,
true
}}},
{
"/?p=1"
,
[]
testItem
{{
"p"
,
false
,
true
}}},
{
"/?p=2"
,
[]
testItem
{{
"p"
,
false
,
false
}}},
{
"/?p=false"
,
[]
testItem
{{
"p"
,
false
,
false
}}},
{
"/?p[a]=1&p[b]=2&p[c]=3"
,
[]
testItem
{{
"p"
,
map
[
string
]
int
{},
map
[
string
]
int
{
"a"
:
1
,
"b"
:
2
,
"c"
:
3
}}}},
{
"/?p[a]=v1&p[b]=v2&p[c]=v3"
,
[]
testItem
{{
"p"
,
map
[
string
]
string
{},
map
[
string
]
string
{
"a"
:
"v1"
,
"b"
:
"v2"
,
"c"
:
"v3"
}}}},
{
"/?p[]=8&p[]=9&p[]=10"
,
[]
testItem
{{
"p"
,
[]
int
{},
[]
int
{
8
,
9
,
10
}}}},
{
"/?p[0]=8&p[1]=9&p[2]=10"
,
[]
testItem
{{
"p"
,
[]
int
{},
[]
int
{
8
,
9
,
10
}}}},
{
"/?p[0]=8&p[1]=9&p[2]=10&p[5]=14"
,
[]
testItem
{{
"p"
,
[]
int
{},
[]
int
{
8
,
9
,
10
,
0
,
0
,
14
}}}},
{
"/?p[0]=8.0&p[1]=9.0&p[2]=10.0"
,
[]
testItem
{{
"p"
,
[]
float64
{},
[]
float64
{
8.0
,
9.0
,
10.0
}}}},
{
"/?p[]=10&p[]=9&p[]=8"
,
[]
testItem
{{
"p"
,
[]
string
{},
[]
string
{
"10"
,
"9"
,
"8"
}}}},
{
"/?p[0]=8&p[1]=9&p[2]=10"
,
[]
testItem
{{
"p"
,
[]
string
{},
[]
string
{
"8"
,
"9"
,
"10"
}}}},
{
"/?p[0]=true&p[1]=false&p[2]=true&p[5]=1&p[6]=ON&p[7]=other"
,
[]
testItem
{{
"p"
,
[]
bool
{},
[]
bool
{
true
,
false
,
true
,
false
,
false
,
true
,
true
,
false
}}}},
{
"/?human.Nick=astaxie"
,
[]
testItem
{{
"human"
,
Human
{},
Human
{
Nick
:
"astaxie"
}}}},
{
"/?human.ID=888&human.Nick=astaxie&human.Ms=true&human[Pwd]=pass"
,
[]
testItem
{{
"human"
,
Human
{},
Human
{
ID
:
888
,
Nick
:
"astaxie"
,
Ms
:
true
,
Pwd
:
"pass"
}}}},
{
"/?human[0].ID=888&human[0].Nick=astaxie&human[0].Ms=true&human[0][Pwd]=pass01&human[1].ID=999&human[1].Nick=ysqi&human[1].Ms=On&human[1].Pwd=pass02"
,
[]
testItem
{{
"human"
,
[]
Human
{},
[]
Human
{
Human
{
ID
:
888
,
Nick
:
"astaxie"
,
Ms
:
true
,
Pwd
:
"pass01"
},
Human
{
ID
:
999
,
Nick
:
"ysqi"
,
Ms
:
true
,
Pwd
:
"pass02"
},
}}}},
{
"/?id=123&isok=true&ft=1.2&ol[0]=1&ol[1]=2&ul[]=str&ul[]=array&human.Nick=astaxie"
,
[]
testItem
{
{
"id"
,
0
,
123
},
{
"isok"
,
false
,
true
},
{
"ft"
,
0.0
,
1.2
},
{
"ol"
,
[]
int
{},
[]
int
{
1
,
2
}},
{
"ul"
,
[]
string
{},
[]
string
{
"str"
,
"array"
}},
{
"human"
,
Human
{},
Human
{
Nick
:
"astaxie"
}},
},
},
}
for
_
,
c
:=
range
cases
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
c
.
request
,
nil
)
beegoInput
:=
NewInput
()
beegoInput
.
Context
=
NewContext
()
beegoInput
.
Context
.
Reset
(
httptest
.
NewRecorder
(),
r
)
beegoInput
.
ParseFormOrMulitForm
(
1
<<
20
)
var
id
int
err
:=
beegoInput
.
Bind
(
&
id
,
"id"
)
if
id
!=
123
||
err
!=
nil
{
t
.
Fatal
(
"id should has int value"
)
for
_
,
item
:=
range
c
.
valueGp
{
got
:=
item
.
empty
err
:=
beegoInput
.
Bind
(
&
got
,
item
.
field
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
fmt
.
Println
(
id
)
var
isok
bool
err
=
beegoInput
.
Bind
(
&
isok
,
"isok"
)
if
!
isok
||
err
!=
nil
{
t
.
Fatal
(
"isok should be true"
)
}
fmt
.
Println
(
isok
)
var
float
float64
err
=
beegoInput
.
Bind
(
&
float
,
"ft"
)
if
float
!=
1.2
||
err
!=
nil
{
t
.
Fatal
(
"float should be equal to 1.2"
)
if
!
reflect
.
DeepEqual
(
got
,
item
.
want
)
{
t
.
Fatalf
(
"Bind %q error,should be:
\n
%#v
\n
got:
\n
%#v"
,
item
.
field
,
item
.
want
,
got
)
}
fmt
.
Println
(
float
)
ol
:=
make
([]
int
,
0
,
2
)
err
=
beegoInput
.
Bind
(
&
ol
,
"ol"
)
if
len
(
ol
)
!=
2
||
err
!=
nil
||
ol
[
0
]
!=
1
||
ol
[
1
]
!=
2
{
t
.
Fatal
(
"ol should has two elements"
)
}
fmt
.
Println
(
ol
)
ul
:=
make
([]
string
,
0
,
2
)
err
=
beegoInput
.
Bind
(
&
ul
,
"ul"
)
if
len
(
ul
)
!=
2
||
err
!=
nil
||
ul
[
0
]
!=
"str"
||
ul
[
1
]
!=
"array"
{
t
.
Fatal
(
"ul should has two elements"
)
}
fmt
.
Println
(
ul
)
type
User
struct
{
Name
string
}
user
:=
User
{}
err
=
beegoInput
.
Bind
(
&
user
,
"user"
)
if
err
!=
nil
||
user
.
Name
!=
"astaxie"
{
t
.
Fatal
(
"user should has name"
)
}
fmt
.
Println
(
user
)
}
func
TestParse2
(
t
*
testing
.
T
)
{
r
,
_
:=
http
.
NewRequest
(
"GET"
,
"/?user[0][Username]=Raph&user[1].Username=Leo&user[0].Password=123456&user[1][Password]=654321"
,
nil
)
beegoInput
:=
NewInput
()
beegoInput
.
Context
=
NewContext
()
beegoInput
.
Context
.
Reset
(
httptest
.
NewRecorder
(),
r
)
beegoInput
.
ParseFormOrMulitForm
(
1
<<
20
)
type
User
struct
{
Username
string
Password
string
}
var
users
[]
User
err
:=
beegoInput
.
Bind
(
&
users
,
"user"
)
fmt
.
Println
(
users
)
if
err
!=
nil
||
users
[
0
]
.
Username
!=
"Raph"
||
users
[
0
]
.
Password
!=
"123456"
||
users
[
1
]
.
Username
!=
"Leo"
||
users
[
1
]
.
Password
!=
"654321"
{
t
.
Fatal
(
"users info wrong"
)
}
}
...
...
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