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
3b934bb9
Commit
3b934bb9
authored
Jun 11, 2014
by
Christoph Portmann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
config: fix error when json config starts with an array
parent
f7b01aab
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
1 deletion
+53
-1
json.go
config/json.go
+6
-1
json_test.go
config/json_test.go
+47
-0
No files found.
config/json.go
View file @
3b934bb9
...
...
@@ -35,7 +35,12 @@ func (js *JsonConfig) Parse(filename string) (ConfigContainer, error) {
}
err
=
json
.
Unmarshal
(
content
,
&
x
.
data
)
if
err
!=
nil
{
return
nil
,
err
var
wrappingArray
[]
interface
{}
err2
:=
json
.
Unmarshal
(
content
,
&
wrappingArray
)
if
err2
!=
nil
{
return
nil
,
err
}
x
.
data
[
"rootArray"
]
=
wrappingArray
}
return
x
,
nil
}
...
...
config/json_test.go
View file @
3b934bb9
...
...
@@ -33,6 +33,53 @@ var jsoncontext = `{
}
}`
var
jsoncontextwitharray
=
`[
{
"url": "user",
"serviceAPI": "http://www.test.com/user"
},
{
"url": "employee",
"serviceAPI": "http://www.test.com/employee"
}
]`
func
TestJsonStartsWithArray
(
t
*
testing
.
T
)
{
f
,
err
:=
os
.
Create
(
"testjsonWithArray.conf"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
f
.
WriteString
(
jsoncontextwitharray
)
if
err
!=
nil
{
f
.
Close
()
t
.
Fatal
(
err
)
}
f
.
Close
()
defer
os
.
Remove
(
"testjsonWithArray.conf"
)
jsonconf
,
err
:=
NewConfig
(
"json"
,
"testjsonWithArray.conf"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
rootArray
,
err
:=
jsonconf
.
DIY
(
"rootArray"
)
if
(
err
!=
nil
)
{
t
.
Error
(
"array does not exist as element"
)
}
rootArrayCasted
:=
rootArray
.
([]
interface
{})
if
(
rootArrayCasted
==
nil
)
{
t
.
Error
(
"array from root is nil"
)
}
else
{
elem
:=
rootArrayCasted
[
0
]
.
(
map
[
string
]
interface
{})
if
elem
[
"url"
]
!=
"user"
||
elem
[
"serviceAPI"
]
!=
"http://www.test.com/user"
{
t
.
Error
(
"array[0] values are not valid"
)
}
elem2
:=
rootArrayCasted
[
1
]
.
(
map
[
string
]
interface
{})
if
elem2
[
"url"
]
!=
"employee"
||
elem2
[
"serviceAPI"
]
!=
"http://www.test.com/employee"
{
t
.
Error
(
"array[1] values are not valid"
)
}
}
}
func
TestJson
(
t
*
testing
.
T
)
{
f
,
err
:=
os
.
Create
(
"testjson.conf"
)
if
err
!=
nil
{
...
...
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