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
bc060c95
Commit
bc060c95
authored
Aug 10, 2013
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #143 from miraclesu/form
Add renderform template function
parents
115b1d03
9e1d5036
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
110 additions
and
9 deletions
+110
-9
template.go
template.go
+1
-0
utils.go
utils.go
+71
-3
utils_test.go
utils_test.go
+38
-6
No files found.
template.go
View file @
bc060c95
...
...
@@ -31,6 +31,7 @@ func init() {
beegoTplFuncMap
[
"str2html"
]
=
Str2html
beegoTplFuncMap
[
"htmlquote"
]
=
Htmlquote
beegoTplFuncMap
[
"htmlunquote"
]
=
Htmlunquote
beegoTplFuncMap
[
"renderform"
]
=
RenderForm
}
// AddFuncMap let user to register a func in the template
...
...
utils.go
View file @
bc060c95
...
...
@@ -178,7 +178,7 @@ func inSlice(v string, sl []string) bool {
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
)
{
if
!
isStructPtr
(
objT
)
{
return
fmt
.
Errorf
(
"%v must be a struct pointer"
,
obj
)
}
objT
=
objT
.
Elem
()
...
...
@@ -189,11 +189,16 @@ func ParseForm(form url.Values, obj interface{}) error {
if
!
fieldV
.
CanSet
()
{
continue
}
fieldT
:=
objT
.
Field
(
i
)
tag
:=
fieldT
.
Tag
.
Get
(
"form"
)
if
len
(
tag
)
==
0
{
tags
:=
strings
.
Split
(
fieldT
.
Tag
.
Get
(
"form"
),
","
)
var
tag
string
if
len
(
tags
)
==
0
||
len
(
tags
[
0
])
==
0
{
tag
=
fieldT
.
Name
}
else
{
tag
=
tags
[
0
]
}
value
:=
form
.
Get
(
tag
)
if
len
(
value
)
==
0
{
continue
...
...
@@ -233,6 +238,69 @@ func ParseForm(form url.Values, obj interface{}) error {
return
nil
}
// form types for RenderForm function
var
FormType
=
map
[
string
]
bool
{
"text"
:
true
,
"textarea"
:
true
,
"hidden"
:
true
,
"password"
:
true
,
}
var
unKind
=
map
[
reflect
.
Kind
]
bool
{
reflect
.
Uintptr
:
true
,
reflect
.
Complex64
:
true
,
reflect
.
Complex128
:
true
,
reflect
.
Array
:
true
,
reflect
.
Chan
:
true
,
reflect
.
Func
:
true
,
reflect
.
Map
:
true
,
reflect
.
Ptr
:
true
,
reflect
.
Slice
:
true
,
reflect
.
Struct
:
true
,
reflect
.
UnsafePointer
:
true
,
}
// obj must be a struct pointer
func
RenderForm
(
obj
interface
{})
template
.
HTML
{
objT
:=
reflect
.
TypeOf
(
obj
)
objV
:=
reflect
.
ValueOf
(
obj
)
if
!
isStructPtr
(
objT
)
{
return
template
.
HTML
(
""
)
}
objT
=
objT
.
Elem
()
objV
=
objV
.
Elem
()
var
raw
[]
string
for
i
:=
0
;
i
<
objT
.
NumField
();
i
++
{
fieldV
:=
objV
.
Field
(
i
)
if
!
fieldV
.
CanSet
()
||
unKind
[
fieldV
.
Kind
()]
{
continue
}
fieldT
:=
objT
.
Field
(
i
)
tags
:=
strings
.
Split
(
fieldT
.
Tag
.
Get
(
"form"
),
","
)
name
:=
fieldT
.
Name
if
len
(
tags
)
<
2
{
if
len
(
tags
)
==
1
&&
len
(
tags
[
0
])
>
0
{
name
=
tags
[
0
]
}
raw
=
append
(
raw
,
fmt
.
Sprintf
(
`%v: <input name="%v" type="text" value="%v">`
,
fieldT
.
Name
,
name
,
fieldV
.
Interface
()))
}
else
{
if
len
(
tags
[
0
])
>
0
{
name
=
tags
[
0
]
}
raw
=
append
(
raw
,
fmt
.
Sprintf
(
`%v: <input name="%v" type="%v" value="%v">`
,
fieldT
.
Name
,
name
,
tags
[
1
],
fieldV
.
Interface
()))
}
}
return
template
.
HTML
(
strings
.
Join
(
raw
,
"</br>"
))
}
func
isStructPtr
(
t
reflect
.
Type
)
bool
{
return
t
.
Kind
()
==
reflect
.
Ptr
&&
t
.
Elem
()
.
Kind
()
==
reflect
.
Struct
}
func
stringsToJson
(
str
string
)
string
{
rs
:=
[]
rune
(
str
)
jsons
:=
""
...
...
utils_test.go
View file @
bc060c95
package
beego
import
(
"html/template"
"net/url"
"testing"
"time"
...
...
@@ -106,8 +107,9 @@ func TestParseForm(t *testing.T) {
Id
int
tag
string
`form:tag`
Name
interface
{}
`form:"username"`
Age
int
`form:"age"`
Age
int
`form:"age
,text
"`
Email
string
Intro
string
`form:",textarea"`
}
u
:=
user
{}
...
...
@@ -116,6 +118,7 @@ func TestParseForm(t *testing.T) {
"username"
:
[]
string
{
"test"
},
"age"
:
[]
string
{
"40"
},
"Email"
:
[]
string
{
"test@gmail.com"
},
"Intro"
:
[]
string
{
"I am an engineer!"
},
}
if
err
:=
ParseForm
(
form
,
u
);
err
==
nil
{
t
.
Fatal
(
"nothing will be changed"
)
...
...
@@ -124,18 +127,47 @@ func TestParseForm(t *testing.T) {
t
.
Fatal
(
err
)
}
if
u
.
Id
!=
0
{
t
.
Error
(
"Id should not be changed"
)
t
.
Error
f
(
"Id should equal 0 but got %v"
,
u
.
Id
)
}
if
len
(
u
.
tag
)
!=
0
{
t
.
Error
(
"tag should not be changed"
)
t
.
Error
f
(
"tag's length should equal 0 but got %v"
,
len
(
u
.
tag
)
)
}
if
u
.
Name
.
(
string
)
!=
"test"
{
t
.
Error
(
"should be equal"
)
t
.
Error
f
(
"Name should equal `test` but got `%v`"
,
u
.
Name
.
(
string
)
)
}
if
u
.
Age
!=
40
{
t
.
Error
(
"should be equal"
)
t
.
Error
f
(
"Age should equal 40 but got %v"
,
u
.
Age
)
}
if
u
.
Email
!=
"test@gmail.com"
{
t
.
Error
(
"should be equal"
)
t
.
Errorf
(
"Email should equal `test@gmail.com` but got `%v`"
,
u
.
Email
)
}
if
u
.
Intro
!=
"I am an engineer!"
{
t
.
Errorf
(
"Intro should equal `I am an engineer!` but got `%v`"
,
u
.
Intro
)
}
}
func
TestRenderForm
(
t
*
testing
.
T
)
{
type
user
struct
{
Id
int
tag
string
`form:tag`
Name
interface
{}
`form:"username"`
Age
int
`form:"age,text"`
Email
[]
string
Intro
string
`form:",textarea"`
}
u
:=
user
{
Name
:
"test"
}
output
:=
RenderForm
(
u
)
if
output
!=
template
.
HTML
(
""
)
{
t
.
Errorf
(
"output should be empty but got %v"
,
output
)
}
output
=
RenderForm
(
&
u
)
result
:=
template
.
HTML
(
`Id: <input name="Id" type="text" value="0"></br>`
+
`Name: <input name="username" type="text" value="test"></br>`
+
`Age: <input name="age" type="text" value="0"></br>`
+
`Intro: <input name="Intro" type="textarea" value="">`
)
if
output
!=
result
{
t
.
Errorf
(
"output should equal `%v` but got `%v`"
,
result
,
output
)
}
}
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