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
3a0b2e3b
authored
2013-12-10 18:09:58 +0800
by
astaxie
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
beego config module json support get data like key:
:key
1 parent
34ba7a8e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
98 additions
and
20 deletions
config/json.go
config/json_test.go
config/json.go
View file @
3a0b2e3
...
...
@@ -5,6 +5,7 @@ import (
"errors"
"io/ioutil"
"os"
"strings"
"sync"
)
...
...
@@ -33,42 +34,73 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
type
JsonConfigContainer
struct
{
data
map
[
string
]
interface
{}
sync
.
Mutex
sync
.
RW
Mutex
}
func
(
c
*
JsonConfigContainer
)
Bool
(
key
string
)
(
bool
,
error
)
{
if
v
,
ok
:=
c
.
data
[
key
]
.
(
bool
);
ok
{
return
v
,
nil
val
:=
c
.
getdata
(
key
)
if
val
!=
nil
{
if
v
,
ok
:=
val
.
(
bool
);
ok
{
return
v
,
nil
}
else
{
return
false
,
errors
.
New
(
"not bool value"
)
}
}
else
{
return
false
,
errors
.
New
(
"not exist key:"
+
key
)
}
return
false
,
errors
.
New
(
"not bool value"
)
}
func
(
c
*
JsonConfigContainer
)
Int
(
key
string
)
(
int
,
error
)
{
if
v
,
ok
:=
c
.
data
[
key
]
.
(
float64
);
ok
{
return
int
(
v
),
nil
val
:=
c
.
getdata
(
key
)
if
val
!=
nil
{
if
v
,
ok
:=
val
.
(
float64
);
ok
{
return
int
(
v
),
nil
}
else
{
return
0
,
errors
.
New
(
"not int value"
)
}
}
else
{
return
0
,
errors
.
New
(
"not exist key:"
+
key
)
}
return
0
,
errors
.
New
(
"not int value"
)
}
func
(
c
*
JsonConfigContainer
)
Int64
(
key
string
)
(
int64
,
error
)
{
if
v
,
ok
:=
c
.
data
[
key
]
.
(
float64
);
ok
{
return
int64
(
v
),
nil
val
:=
c
.
getdata
(
key
)
if
val
!=
nil
{
if
v
,
ok
:=
val
.
(
float64
);
ok
{
return
int64
(
v
),
nil
}
else
{
return
0
,
errors
.
New
(
"not int64 value"
)
}
}
else
{
return
0
,
errors
.
New
(
"not exist key:"
+
key
)
}
return
0
,
errors
.
New
(
"not int64 value"
)
}
func
(
c
*
JsonConfigContainer
)
Float
(
key
string
)
(
float64
,
error
)
{
if
v
,
ok
:=
c
.
data
[
key
]
.
(
float64
);
ok
{
return
v
,
nil
val
:=
c
.
getdata
(
key
)
if
val
!=
nil
{
if
v
,
ok
:=
val
.
(
float64
);
ok
{
return
v
,
nil
}
else
{
return
0.0
,
errors
.
New
(
"not float64 value"
)
}
}
else
{
return
0.0
,
errors
.
New
(
"not exist key:"
+
key
)
}
return
0.0
,
errors
.
New
(
"not float64 value"
)
}
func
(
c
*
JsonConfigContainer
)
String
(
key
string
)
string
{
if
v
,
ok
:=
c
.
data
[
key
]
.
(
string
);
ok
{
return
v
val
:=
c
.
getdata
(
key
)
if
val
!=
nil
{
if
v
,
ok
:=
val
.
(
string
);
ok
{
return
v
}
else
{
return
""
}
}
else
{
return
""
}
return
""
}
func
(
c
*
JsonConfigContainer
)
Set
(
key
,
val
string
)
error
{
...
...
@@ -79,10 +111,41 @@ func (c *JsonConfigContainer) Set(key, val string) error {
}
func
(
c
*
JsonConfigContainer
)
DIY
(
key
string
)
(
v
interface
{},
err
error
)
{
if
v
,
ok
:=
c
.
data
[
key
];
ok
{
return
v
,
nil
val
:=
c
.
getdata
(
key
)
if
val
!=
nil
{
return
val
,
nil
}
else
{
return
nil
,
errors
.
New
(
"not exist key"
)
}
}
//section.key or key
func
(
c
*
JsonConfigContainer
)
getdata
(
key
string
)
interface
{}
{
c
.
RLock
()
defer
c
.
RUnlock
()
if
len
(
key
)
==
0
{
return
nil
}
sectionkey
:=
strings
.
Split
(
key
,
"::"
)
if
len
(
sectionkey
)
>=
2
{
cruval
,
ok
:=
c
.
data
[
sectionkey
[
0
]]
if
!
ok
{
return
nil
}
for
_
,
key
:=
range
sectionkey
[
1
:
]
{
if
v
,
ok
:=
cruval
.
(
map
[
string
]
interface
{});
!
ok
{
return
nil
}
else
if
cruval
,
ok
=
v
[
key
];
!
ok
{
return
nil
}
}
return
cruval
}
else
{
if
v
,
ok
:=
c
.
data
[
key
];
ok
{
return
v
}
}
return
nil
,
errors
.
New
(
"not exist key"
)
return
nil
}
func
init
()
{
...
...
config/json_test.go
View file @
3a0b2e3
...
...
@@ -18,7 +18,12 @@ var jsoncontext = `{
"port": "port",
"database": "database",
"username": "username",
"password": "password"
"password": "password",
"conns":{
"maxconnection":12,
"autoconnect":true,
"connectioninfo":"info"
}
}
}`
...
...
@@ -70,9 +75,19 @@ 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
{
t
.
Log
(
db
)
t
.
Fatal
(
"db not map[string]interface{}"
)
}
else
{
if
m
[
"host"
]
.
(
string
)
!=
"host"
{
...
...
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