Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
二次元的罪恶王冠
beego
提交
cf055c9d
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,发现更多精彩内容 >>
提交
cf055c9d
编写于
1月 24, 2016
作者:
Y
ysqi
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'astaxie/develop' into iniSaveErrorFix
# Conflicts: # config/ini_test.go
上级
51ae45a7
57d522a9
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
219 addition
and
123 deletion
+219
-123
config/config.go
config/config.go
+38
-0
config/fake.go
config/fake.go
+1
-1
config/ini.go
config/ini.go
+1
-1
config/ini_test.go
config/ini_test.go
+74
-46
config/json.go
config/json.go
+3
-5
config/json_test.go
config/json_test.go
+88
-65
config/xml/xml.go
config/xml/xml.go
+5
-1
config/yaml/yaml.go
config/yaml/yaml.go
+4
-3
tree.go
tree.go
+5
-1
未找到文件。
config/config.go
浏览文件 @
cf055c9d
...
...
@@ -106,3 +106,41 @@ func NewConfigData(adapterName string, data []byte) (Configer, error) {
}
return
adapter
.
ParseData
(
data
)
}
// ParseBool returns the boolean value represented by the string.
//
// It accepts 1, 1.0, t, T, TRUE, true, True, YES, yes, Yes,Y, y, ON, on, On,
// 0, 0.0, f, F, FALSE, false, False, NO, no, No, N,n, OFF, off, Off.
// Any other value returns an error.
func
ParseBool
(
val
interface
{})
(
value
bool
,
err
error
)
{
if
val
!=
nil
{
switch
v
:=
val
.
(
type
)
{
case
bool
:
return
v
,
nil
case
string
:
switch
v
{
case
"1"
,
"t"
,
"T"
,
"true"
,
"TRUE"
,
"True"
,
"YES"
,
"yes"
,
"Yes"
,
"Y"
,
"y"
,
"ON"
,
"on"
,
"On"
:
return
true
,
nil
case
"0"
,
"f"
,
"F"
,
"false"
,
"FALSE"
,
"False"
,
"NO"
,
"no"
,
"No"
,
"N"
,
"n"
,
"OFF"
,
"off"
,
"Off"
:
return
false
,
nil
}
case
int8
,
int32
,
int64
:
strV
:=
fmt
.
Sprintf
(
"%s"
,
v
)
if
strV
==
"1"
{
return
true
,
nil
}
else
if
strV
==
"0"
{
return
false
,
nil
}
case
float64
:
if
v
==
1
{
return
true
,
nil
}
else
if
v
==
0
{
return
false
,
nil
}
}
return
false
,
fmt
.
Errorf
(
"parsing %q: invalid syntax"
,
val
)
}
else
{
return
false
,
fmt
.
Errorf
(
"parsing <nil>: invalid syntax"
)
}
}
config/fake.go
浏览文件 @
cf055c9d
...
...
@@ -82,7 +82,7 @@ func (c *fakeConfigContainer) DefaultInt64(key string, defaultval int64) int64 {
}
func
(
c
*
fakeConfigContainer
)
Bool
(
key
string
)
(
bool
,
error
)
{
return
strconv
.
ParseBool
(
c
.
getData
(
key
))
return
ParseBool
(
c
.
getData
(
key
))
}
func
(
c
*
fakeConfigContainer
)
DefaultBool
(
key
string
,
defaultval
bool
)
bool
{
...
...
config/ini.go
浏览文件 @
cf055c9d
...
...
@@ -195,7 +195,7 @@ type IniConfigContainer struct {
// Bool returns the boolean value for a given key.
func
(
c
*
IniConfigContainer
)
Bool
(
key
string
)
(
bool
,
error
)
{
return
strconv
.
ParseBool
(
c
.
getdata
(
key
))
return
ParseBool
(
c
.
getdata
(
key
))
}
// DefaultBool returns the boolean value for a given key.
...
...
config/ini_test.go
浏览文件 @
cf055c9d
...
...
@@ -15,12 +15,17 @@
package
config
import
(
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
)
var
inicontext
=
`
func
TestIni
(
t
*
testing
.
T
)
{
var
(
inicontext
=
`
;comment one
#comment two
appname = beeapi
...
...
@@ -30,6 +35,13 @@ PI = 3.1415976
runmode = "dev"
autorender = false
copyrequestbody = true
session= on
cookieon= off
newreg = OFF
needlogin = ON
enableSession = Y
enableCookie = N
flag = 1
[demo]
key1="asta"
key2 = "xie"
...
...
@@ -37,7 +49,31 @@ CaseInsensitive = true
peers = one;two;three
`
func
TestIni
(
t
*
testing
.
T
)
{
keyValue
=
map
[
string
]
interface
{}{
"appname"
:
"beeapi"
,
"httpport"
:
8080
,
"mysqlport"
:
int64
(
3600
),
"pi"
:
3.1415976
,
"runmode"
:
"dev"
,
"autorender"
:
false
,
"copyrequestbody"
:
true
,
"session"
:
true
,
"cookieon"
:
false
,
"newreg"
:
false
,
"needlogin"
:
true
,
"enableSession"
:
true
,
"enableCookie"
:
false
,
"flag"
:
true
,
"demo::key1"
:
"asta"
,
"demo::key2"
:
"xie"
,
"demo::CaseInsensitive"
:
true
,
"demo::peers"
:
[]
string
{
"one"
,
"two"
,
"three"
},
"null"
:
""
,
"demo2::key1"
:
""
,
"error"
:
""
,
}
)
f
,
err
:=
os
.
Create
(
"testini.conf"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -53,31 +89,31 @@ func TestIni(t *testing.T) {
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
iniconf
.
String
(
"appname"
)
!=
"beeapi"
{
t
.
Fatal
(
"appname not equal to beeapi"
)
}
if
port
,
err
:=
iniconf
.
Int
(
"httpport"
);
err
!=
nil
||
port
!=
8080
{
t
.
Error
(
port
)
t
.
Fatal
(
err
)
}
if
port
,
err
:=
iniconf
.
Int64
(
"mysqlport"
);
err
!=
nil
||
port
!=
3600
{
t
.
Error
(
port
)
t
.
Fatal
(
err
)
}
if
pi
,
err
:=
iniconf
.
Float
(
"PI"
);
err
!=
nil
||
pi
!=
3.1415976
{
t
.
Error
(
pi
)
t
.
Fatal
(
err
)
}
if
iniconf
.
String
(
"runmode"
)
!=
"dev"
{
t
.
Fatal
(
"runmode not equal to dev"
)
}
if
v
,
err
:=
iniconf
.
Bool
(
"autorender"
);
err
!=
nil
||
v
!=
false
{
t
.
Error
(
v
)
t
.
Fatal
(
err
)
}
if
v
,
err
:=
iniconf
.
Bool
(
"copyrequestbody"
);
err
!=
nil
||
v
!=
true
{
t
.
Error
(
v
)
t
.
Fatal
(
err
)
for
k
,
v
:=
range
keyValue
{
var
err
error
var
value
interface
{
}
switch
v
.
(
type
)
{
case
int
:
value
,
err
=
iniconf
.
Int
(
k
)
case
int64
:
value
,
err
=
iniconf
.
Int64
(
k
)
case
float64
:
value
,
err
=
iniconf
.
Float
(
k
)
case
bool
:
value
,
err
=
iniconf
.
Bool
(
k
)
case
[]
string
:
value
=
iniconf
.
Strings
(
k
)
case
string
:
value
=
iniconf
.
String
(
k
)
default
:
value
,
err
=
iniconf
.
DIY
(
k
)
}
if
err
!=
nil
{
t
.
Fatalf
(
"get key %q value fail,err %s"
,
k
,
err
)
}
else
if
fmt
.
Sprintf
(
"%v"
,
v
)
!=
fmt
.
Sprintf
(
"%v"
,
value
)
{
t
.
Fatalf
(
"get key %q value, want %v got %v ."
,
k
,
v
,
value
)
}
}
if
err
=
iniconf
.
Set
(
"name"
,
"astaxie"
);
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -85,21 +121,6 @@ func TestIni(t *testing.T) {
if
iniconf
.
String
(
"name"
)
!=
"astaxie"
{
t
.
Fatal
(
"get name error"
)
}
if
iniconf
.
String
(
"demo::key1"
)
!=
"asta"
{
t
.
Fatal
(
"get demo.key1 error"
)
}
if
iniconf
.
String
(
"demo::key2"
)
!=
"xie"
{
t
.
Fatal
(
"get demo.key2 error"
)
}
if
v
,
err
:=
iniconf
.
Bool
(
"demo::caseinsensitive"
);
err
!=
nil
||
v
!=
true
{
t
.
Fatal
(
"get demo.caseinsensitive error"
)
}
if
data
:=
iniconf
.
Strings
(
"demo::peers"
);
len
(
data
)
!=
3
{
t
.
Fatal
(
"get strings error"
,
data
)
}
else
if
data
[
0
]
!=
"one"
{
t
.
Fatal
(
"get first params error not equat to one"
)
}
}
...
...
@@ -121,7 +142,8 @@ httpport = 8080
name = mysql
`
saveResult
=
`app=app
saveResult
=
`
app=app
#comment one
#comment two
# comment three
...
...
@@ -134,7 +156,6 @@ httpport=8080
# db type name
# suport mysql,sqlserver
name=mysql
`
)
cfg
,
err
:=
NewConfigData
(
"ini"
,
[]
byte
(
inicontext
))
...
...
@@ -149,7 +170,14 @@ name=mysql
if
data
,
err
:=
ioutil
.
ReadFile
(
name
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
else
if
string
(
data
)
!=
saveResult
{
t
.
Fatal
(
"different after save ini config file."
)
}
else
{
cfgData
:=
string
(
data
)
datas
:=
strings
.
Split
(
saveResult
,
"
\n
"
)
for
_
,
line
:=
range
datas
{
if
strings
.
Contains
(
cfgData
,
line
+
"
\n
"
)
==
false
{
t
.
Fatalf
(
"different after save ini config file. need contains %q"
,
line
)
}
}
}
}
config/json.go
浏览文件 @
cf055c9d
...
...
@@ -17,6 +17,7 @@ package config
import
(
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
...
...
@@ -70,12 +71,9 @@ type JSONConfigContainer struct {
func
(
c
*
JSONConfigContainer
)
Bool
(
key
string
)
(
bool
,
error
)
{
val
:=
c
.
getData
(
key
)
if
val
!=
nil
{
if
v
,
ok
:=
val
.
(
bool
);
ok
{
return
v
,
nil
}
return
false
,
errors
.
New
(
"not bool value"
)
return
ParseBool
(
val
)
}
return
false
,
errors
.
New
(
"not exist key:"
+
key
)
return
false
,
fmt
.
Errorf
(
"not exist key: %q"
,
key
)
}
// DefaultBool return the bool value if has no error
...
...
config/json_test.go
浏览文件 @
cf055c9d
...
...
@@ -15,34 +15,14 @@
package
config
import
(
"fmt"
"os"
"testing"
)
var
jsoncontext
=
`{
"appname": "beeapi",
"testnames": "foo;bar",
"httpport": 8080,
"mysqlport": 3600,
"PI": 3.1415976,
"runmode": "dev",
"autorender": false,
"copyrequestbody": true,
"database": {
"host": "host",
"port": "port",
"database": "database",
"username": "username",
"password": "password",
"conns":{
"maxconnection":12,
"autoconnect":true,
"connectioninfo":"info"
}
}
}`
func
TestJsonStartsWithArray
(
t
*
testing
.
T
)
{
var
jsoncontextwitharray
=
`[
const
jsoncontextwitharray
=
`[
{
"url": "user",
"serviceAPI": "http://www.test.com/user"
...
...
@@ -52,8 +32,6 @@ var jsoncontextwitharray = `[
"serviceAPI": "http://www.test.com/employee"
}
]`
func
TestJsonStartsWithArray
(
t
*
testing
.
T
)
{
f
,
err
:=
os
.
Create
(
"testjsonWithArray.conf"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -90,6 +68,64 @@ func TestJsonStartsWithArray(t *testing.T) {
}
func
TestJson
(
t
*
testing
.
T
)
{
var
(
jsoncontext
=
`{
"appname": "beeapi",
"testnames": "foo;bar",
"httpport": 8080,
"mysqlport": 3600,
"PI": 3.1415976,
"runmode": "dev",
"autorender": false,
"copyrequestbody": true,
"session": "on",
"cookieon": "off",
"newreg": "OFF",
"needlogin": "ON",
"enableSession": "Y",
"enableCookie": "N",
"flag": 1,
"database": {
"host": "host",
"port": "port",
"database": "database",
"username": "username",
"password": "password",
"conns":{
"maxconnection":12,
"autoconnect":true,
"connectioninfo":"info"
}
}
}`
keyValue
=
map
[
string
]
interface
{}{
"appname"
:
"beeapi"
,
"testnames"
:
[]
string
{
"foo"
,
"bar"
},
"httpport"
:
8080
,
"mysqlport"
:
int64
(
3600
),
"PI"
:
3.1415976
,
"runmode"
:
"dev"
,
"autorender"
:
false
,
"copyrequestbody"
:
true
,
"session"
:
true
,
"cookieon"
:
false
,
"newreg"
:
false
,
"needlogin"
:
true
,
"enableSession"
:
true
,
"enableCookie"
:
false
,
"flag"
:
true
,
"database::host"
:
"host"
,
"database::port"
:
"port"
,
"database::database"
:
"database"
,
"database::password"
:
"password"
,
"database::conns::maxconnection"
:
12
,
"database::conns::autoconnect"
:
true
,
"database::conns::connectioninfo"
:
"info"
,
"unknown"
:
""
,
}
)
f
,
err
:=
os
.
Create
(
"testjson.conf"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -105,37 +141,32 @@ func TestJson(t *testing.T) {
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
jsonconf
.
String
(
"appname"
)
!=
"beeapi"
{
t
.
Fatal
(
"appname not equal to beeapi"
)
}
if
port
,
err
:=
jsonconf
.
Int
(
"httpport"
);
err
!=
nil
||
port
!=
8080
{
t
.
Error
(
port
)
t
.
Fatal
(
err
)
}
if
port
,
err
:=
jsonconf
.
Int64
(
"mysqlport"
);
err
!=
nil
||
port
!=
3600
{
t
.
Error
(
port
)
t
.
Fatal
(
err
)
}
if
pi
,
err
:=
jsonconf
.
Float
(
"PI"
);
err
!=
nil
||
pi
!=
3.1415976
{
t
.
Error
(
pi
)
t
.
Fatal
(
err
)
}
if
jsonconf
.
String
(
"runmode"
)
!=
"dev"
{
t
.
Fatal
(
"runmode not equal to dev"
)
}
if
v
:=
jsonconf
.
Strings
(
"unknown"
);
len
(
v
)
>
0
{
t
.
Fatal
(
"unknown strings, the length should be 0"
)
}
if
v
:=
jsonconf
.
Strings
(
"testnames"
);
len
(
v
)
!=
2
{
t
.
Fatal
(
"testnames length should be 2"
)
}
if
v
,
err
:=
jsonconf
.
Bool
(
"autorender"
);
err
!=
nil
||
v
!=
false
{
t
.
Error
(
v
)
t
.
Fatal
(
err
)
}
if
v
,
err
:=
jsonconf
.
Bool
(
"copyrequestbody"
);
err
!=
nil
||
v
!=
true
{
t
.
Error
(
v
)
t
.
Fatal
(
err
)
for
k
,
v
:=
range
keyValue
{
var
err
error
var
value
interface
{}
switch
v
.
(
type
)
{
case
int
:
value
,
err
=
jsonconf
.
Int
(
k
)
case
int64
:
value
,
err
=
jsonconf
.
Int64
(
k
)
case
float64
:
value
,
err
=
jsonconf
.
Float
(
k
)
case
bool
:
value
,
err
=
jsonconf
.
Bool
(
k
)
case
[]
string
:
value
=
jsonconf
.
Strings
(
k
)
case
string
:
value
=
jsonconf
.
String
(
k
)
default
:
value
,
err
=
jsonconf
.
DIY
(
k
)
}
if
err
!=
nil
{
t
.
Fatalf
(
"get key %q value fatal,%V err %s"
,
k
,
v
,
err
)
}
else
if
fmt
.
Sprintf
(
"%v"
,
v
)
!=
fmt
.
Sprintf
(
"%v"
,
value
)
{
t
.
Fatalf
(
"get key %q value, want %v got %v ."
,
k
,
v
,
value
)
}
}
if
err
=
jsonconf
.
Set
(
"name"
,
"astaxie"
);
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -143,15 +174,7 @@ func TestJson(t *testing.T) {
if
jsonconf
.
String
(
"name"
)
!=
"astaxie"
{
t
.
Fatal
(
"get name error"
)
}
if
jsonconf
.
String
(
"database::host"
)
!=
"host"
{
t
.
Fatal
(
"get database::host error"
)
}
if
jsonconf
.
String
(
"database::conns::connectioninfo"
)
!=
"info"
{
t
.
Fatal
(
"get database::conns::connectioninfo error"
)
}
if
maxconnection
,
err
:=
jsonconf
.
Int
(
"database::conns::maxconnection"
);
err
!=
nil
||
maxconnection
!=
12
{
t
.
Fatal
(
"get database::conns::maxconnection error"
)
}
if
db
,
err
:=
jsonconf
.
DIY
(
"database"
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
else
if
m
,
ok
:=
db
.
(
map
[
string
]
interface
{});
!
ok
{
...
...
config/xml/xml.go
浏览文件 @
cf055c9d
...
...
@@ -92,7 +92,11 @@ type ConfigContainer struct {
// Bool returns the boolean value for a given key.
func
(
c
*
ConfigContainer
)
Bool
(
key
string
)
(
bool
,
error
)
{
return
strconv
.
ParseBool
(
c
.
data
[
key
]
.
(
string
))
if
v
,
ok
:=
c
.
data
[
key
];
ok
{
return
config
.
ParseBool
(
v
)
}
else
{
return
false
,
fmt
.
Errorf
(
"not exist key: %q"
,
key
)
}
}
// DefaultBool return the bool value if has no error
...
...
config/yaml/yaml.go
浏览文件 @
cf055c9d
...
...
@@ -121,10 +121,11 @@ type ConfigContainer struct {
// Bool returns the boolean value for a given key.
func
(
c
*
ConfigContainer
)
Bool
(
key
string
)
(
bool
,
error
)
{
if
v
,
ok
:=
c
.
data
[
key
]
.
(
bool
);
ok
{
return
v
,
nil
if
v
,
ok
:=
c
.
data
[
key
];
ok
{
return
config
.
ParseBool
(
v
)
}
else
{
return
false
,
fmt
.
Errorf
(
"not exist key: %q"
,
key
)
}
return
false
,
errors
.
New
(
"not bool value"
)
}
// DefaultBool return the bool value if has no error
...
...
tree.go
浏览文件 @
cf055c9d
...
...
@@ -420,7 +420,11 @@ func (leaf *leafInfo) match(wildcardValues []string, ctx *context.Context) (ok b
if
len
(
strs
)
==
2
{
ctx
.
Input
.
SetParam
(
":ext"
,
strs
[
1
])
}
ctx
.
Input
.
SetParam
(
":path"
,
path
.
Join
(
path
.
Join
(
wildcardValues
[
index
:
len
(
wildcardValues
)
-
1
]
...
),
strs
[
0
]))
if
index
>
(
len
(
wildcardValues
)
-
1
)
{
ctx
.
Input
.
SetParam
(
":path"
,
""
)
}
else
{
ctx
.
Input
.
SetParam
(
":path"
,
path
.
Join
(
path
.
Join
(
wildcardValues
[
index
:
len
(
wildcardValues
)
-
1
]
...
),
strs
[
0
]))
}
return
true
}
// match :id
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录