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
bdc01f52
Commit
bdc01f52
authored
May 31, 2014
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #626 from mvpmvh/michael
Michael
parents
5dee6b7d
a673a85d
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
10 deletions
+74
-10
json_test.go
config/json_test.go
+24
-0
template.go
template.go
+1
-0
templatefunc.go
templatefunc.go
+37
-0
templatefunc_test.go
templatefunc_test.go
+12
-10
No files found.
config/json_test.go
View file @
bdc01f52
...
...
@@ -100,4 +100,28 @@ func TestJson(t *testing.T) {
t
.
Fatal
(
"get host err"
)
}
}
if
_
,
err
:=
jsonconf
.
Int
(
"unknown"
);
err
==
nil
{
t
.
Error
(
"unknown keys should return an error when expecting an Int"
)
}
if
_
,
err
:=
jsonconf
.
Int64
(
"unknown"
);
err
==
nil
{
t
.
Error
(
"unknown keys should return an error when expecting an Int64"
)
}
if
_
,
err
:=
jsonconf
.
Float
(
"unknown"
);
err
==
nil
{
t
.
Error
(
"unknown keys should return an error when expecting a Float"
)
}
if
_
,
err
:=
jsonconf
.
DIY
(
"unknown"
);
err
==
nil
{
t
.
Error
(
"unknown keys should return an error when expecting an interface{}"
)
}
if
val
:=
jsonconf
.
String
(
"unknown"
);
val
!=
""
{
t
.
Error
(
"unknown keys should return an empty string when expecting a String"
)
}
if
_
,
err
:=
jsonconf
.
Bool
(
"unknown"
);
err
==
nil
{
t
.
Error
(
"unknown keys should return an error when expecting a Bool"
)
}
}
template.go
View file @
bdc01f52
...
...
@@ -44,6 +44,7 @@ func init() {
beegoTplFuncMap
[
"renderform"
]
=
RenderForm
beegoTplFuncMap
[
"assets_js"
]
=
AssetsJs
beegoTplFuncMap
[
"assets_css"
]
=
AssetsCss
beegoTplFuncMap
[
"config"
]
=
Config
// go1.2 added template funcs
// Comparisons
...
...
templatefunc.go
View file @
bdc01f52
...
...
@@ -131,6 +131,43 @@ func Compare(a, b interface{}) (equal bool) {
return
}
func
Config
(
returnType
,
key
string
,
defaultVal
interface
{})
(
value
interface
{},
err
error
)
{
switch
returnType
{
case
"String"
:
value
=
AppConfig
.
String
(
key
)
case
"Bool"
:
value
,
err
=
AppConfig
.
Bool
(
key
)
case
"Int"
:
value
,
err
=
AppConfig
.
Int
(
key
)
case
"Int64"
:
value
,
err
=
AppConfig
.
Int64
(
key
)
case
"Float"
:
value
,
err
=
AppConfig
.
Float
(
key
)
case
"DIY"
:
value
,
err
=
AppConfig
.
DIY
(
key
)
default
:
err
=
errors
.
New
(
"Config keys must be of type String, Bool, Int, Int64, Float, or DIY!"
)
}
if
err
!=
nil
{
if
reflect
.
TypeOf
(
returnType
)
!=
reflect
.
TypeOf
(
defaultVal
)
{
err
=
errors
.
New
(
"defaultVal type does not match returnType!"
)
}
else
{
value
,
err
=
defaultVal
,
nil
}
}
else
if
reflect
.
TypeOf
(
value
)
.
Kind
()
==
reflect
.
String
{
if
value
==
""
{
if
reflect
.
TypeOf
(
defaultVal
)
.
Kind
()
!=
reflect
.
String
{
err
=
errors
.
New
(
"defaultVal type must be a String if the returnType is a String"
)
}
else
{
value
=
defaultVal
.
(
string
)
}
}
}
return
}
// Convert string to template.HTML type.
func
Str2html
(
raw
string
)
template
.
HTML
{
return
template
.
HTML
(
raw
)
...
...
templatefunc_test.go
View file @
bdc01f52
...
...
@@ -36,25 +36,27 @@ func TestHtml2str(t *testing.T) {
func
TestDateFormat
(
t
*
testing
.
T
)
{
ts
:=
"Mon, 01 Jul 2013 13:27:42 CST"
tt
,
_
:=
time
.
Parse
(
time
.
RFC1123
,
ts
)
if
DateFormat
(
tt
,
"2006-01-02 15:04:05"
)
!=
"2013-07-01 13:27:42"
{
t
.
Error
(
"should be equal"
)
if
ss
:=
DateFormat
(
tt
,
"2006-01-02 15:04:05"
);
ss
!=
"2013-07-01 14:27:42"
{
t
.
Errorf
(
"2013-07-01 14:27:42 does not equal %v"
,
ss
)
}
}
func
TestDate
(
t
*
testing
.
T
)
{
ts
:=
"Mon, 01 Jul 2013 13:27:42 CST"
tt
,
_
:=
time
.
Parse
(
time
.
RFC1123
,
ts
)
if
Date
(
tt
,
"Y-m-d H:i:s"
)
!=
"2013-07-01 13:27:42"
{
t
.
Error
(
"should be equal"
)
if
ss
:=
Date
(
tt
,
"Y-m-d H:i:s"
);
ss
!=
"2013-07-01 14:27:42"
{
t
.
Errorf
(
"2013-07-01 14:27:42 does not equal %v"
,
ss
)
}
if
Date
(
tt
,
"y-n-j h:i:s A"
)
!=
"13-7-1 01
:27:42 PM"
{
t
.
Error
(
"should be equal"
)
if
ss
:=
Date
(
tt
,
"y-n-j h:i:s A"
);
ss
!=
"13-7-1 02
:27:42 PM"
{
t
.
Error
f
(
"13-7-1 02:27:42 PM does not equal %v"
,
ss
)
}
if
Date
(
tt
,
"D, d M Y g:i:s a"
)
!=
"Mon, 01 Jul 2013 1
:27:42 pm"
{
t
.
Error
(
"should be equal"
)
if
ss
:=
Date
(
tt
,
"D, d M Y g:i:s a"
);
ss
!=
"Mon, 01 Jul 2013 2
:27:42 pm"
{
t
.
Error
f
(
"Mon, 01 Jul 2013 2:27:42 pm does not equal %v"
,
ss
)
}
if
Date
(
tt
,
"l, d F Y G:i:s"
)
!=
"Monday, 01 July 2013 13
:27:42"
{
t
.
Error
(
"should be equal"
)
if
ss
:=
Date
(
tt
,
"l, d F Y G:i:s"
);
ss
!=
"Monday, 01 July 2013 14
:27:42"
{
t
.
Error
f
(
"Monday, 01 July 2013 14:27:42 does not equal %v"
,
ss
)
}
}
...
...
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