Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
bdc01f52
B
beego
项目概览
二次元的罪恶王冠
/
beego
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
B
beego
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
bdc01f52
编写于
5月 31, 2014
作者:
A
astaxie
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #626 from mvpmvh/michael
Michael
上级
5dee6b7d
a673a85d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
74 addition
and
10 deletion
+74
-10
config/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
未找到文件。
config/json_test.go
浏览文件 @
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
浏览文件 @
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
浏览文件 @
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
浏览文件 @
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
)
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录