Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
张磊
/
FileStorageBeego
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
f5523388
authored
2013-07-25 22:25:12 +0800
by
miraclesu
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Add ParseForm function
1 parent
23deaedd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
119 additions
and
0 deletions
utils.go
utils_test.go
utils.go
View file @
f552338
...
...
@@ -4,7 +4,10 @@ import (
"fmt"
"github.com/russross/blackfriday"
"html/template"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
...
...
@@ -179,3 +182,80 @@ func inSlice(v string, sl []string) bool {
}
return
false
}
// parse form values to struct via tag
func
ParseForm
(
form
url
.
Values
,
obj
interface
{})
error
{
objT
:=
reflect
.
TypeOf
(
obj
)
objV
:=
reflect
.
ValueOf
(
obj
)
if
!
(
objT
.
Kind
()
==
reflect
.
Ptr
&&
objT
.
Elem
()
.
Kind
()
==
reflect
.
Struct
)
{
return
fmt
.
Errorf
(
"%v must be a struct pointer"
,
obj
)
}
objT
=
objT
.
Elem
()
objV
=
objV
.
Elem
()
for
i
:=
0
;
i
<
objT
.
NumField
();
i
++
{
fieldV
:=
objV
.
Field
(
i
)
if
!
fieldV
.
CanSet
()
{
continue
}
fieldT
:=
objT
.
Field
(
i
)
tag
:=
fieldT
.
Tag
.
Get
(
"form"
)
if
len
(
tag
)
==
0
{
tag
=
fieldT
.
Name
}
value
:=
form
.
Get
(
tag
)
if
len
(
value
)
==
0
{
continue
}
switch
fieldT
.
Type
.
Kind
()
{
case
reflect
.
Bool
:
b
,
err
:=
strconv
.
ParseBool
(
value
)
if
err
!=
nil
{
return
err
}
fieldV
.
SetBool
(
b
)
case
reflect
.
Int
:
fallthrough
case
reflect
.
Int8
:
fallthrough
case
reflect
.
Int16
:
fallthrough
case
reflect
.
Int32
:
fallthrough
case
reflect
.
Int64
:
x
,
err
:=
strconv
.
ParseInt
(
value
,
10
,
64
)
if
err
!=
nil
{
return
err
}
fieldV
.
SetInt
(
x
)
case
reflect
.
Uint
:
fallthrough
case
reflect
.
Uint8
:
fallthrough
case
reflect
.
Uint16
:
fallthrough
case
reflect
.
Uint32
:
fallthrough
case
reflect
.
Uint64
:
x
,
err
:=
strconv
.
ParseUint
(
value
,
10
,
64
)
if
err
!=
nil
{
return
err
}
fieldV
.
SetUint
(
x
)
case
reflect
.
Float32
:
fallthrough
case
reflect
.
Float64
:
x
,
err
:=
strconv
.
ParseFloat
(
value
,
64
)
if
err
!=
nil
{
return
err
}
fieldV
.
SetFloat
(
x
)
case
reflect
.
Interface
:
fieldV
.
Set
(
reflect
.
ValueOf
(
value
))
case
reflect
.
String
:
fieldV
.
SetString
(
value
)
}
}
return
nil
}
...
...
utils_test.go
0 → 100644
View file @
f552338
package
beego
import
(
"net/url"
"testing"
)
func
TestParseForm
(
t
*
testing
.
T
)
{
type
user
struct
{
Id
int
tag
string
`form:tag`
Name
interface
{}
`form:"username"`
Age
int
`form:"age"`
Email
string
}
u
:=
user
{}
form
:=
url
.
Values
{
"tag"
:
[]
string
{
"no"
},
"username"
:
[]
string
{
"test"
},
"age"
:
[]
string
{
"40"
},
"Email"
:
[]
string
{
"test@gmail.com"
},
}
if
err
:=
ParseForm
(
form
,
u
);
err
==
nil
{
t
.
Fatal
(
"nothing will be changed"
)
}
if
err
:=
ParseForm
(
form
,
&
u
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
u
.
Name
.
(
string
)
!=
"test"
{
t
.
Error
(
"should be equal"
)
}
if
u
.
Age
!=
40
{
t
.
Error
(
"should be equal"
)
}
if
u
.
Email
!=
"test@gmail.com"
{
t
.
Error
(
"should be equal"
)
}
}
Write
Preview
Styling with
Markdown
is supported
Attach a file
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 post a comment