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
5af26446
Commit
5af26446
authored
Nov 30, 2016
by
astaxie
Committed by
GitHub
Nov 30, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2299 from amrfaissal/fix-2294
Fix xml.GetSection() function which causes a panic
parents
24015e9a
39d40ba8
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
4 deletions
+59
-4
config.go
config/config.go
+36
-0
xml.go
config/xml/xml.go
+8
-4
xml_test.go
config/xml/xml_test.go
+15
-0
No files found.
config/config.go
View file @
5af26446
...
...
@@ -43,6 +43,8 @@ package config
import
(
"fmt"
"os"
"reflect"
"time"
)
// Configer defines how to get and set value from configuration raw data.
...
...
@@ -204,3 +206,37 @@ func ParseBool(val interface{}) (value bool, err error) {
}
return
false
,
fmt
.
Errorf
(
"parsing <nil>: invalid syntax"
)
}
// ToString converts values of any type to string.
func
ToString
(
x
interface
{})
string
{
switch
y
:=
x
.
(
type
)
{
// Handle dates with special logic
// This needs to come above the fmt.Stringer
// test since time.Time's have a .String()
// method
case
time
.
Time
:
return
y
.
Format
(
"A Monday"
)
// Handle type string
case
string
:
return
y
// Handle type with .String() method
case
fmt
.
Stringer
:
return
y
.
String
()
// Handle type with .Error() method
case
error
:
return
y
.
Error
()
}
// Handle named string type
if
v
:=
reflect
.
ValueOf
(
x
);
v
.
Kind
()
==
reflect
.
String
{
return
v
.
String
()
}
// Fallback to fmt package for anything else like numeric types
return
fmt
.
Sprint
(
x
)
}
config/xml/xml.go
View file @
5af26446
...
...
@@ -193,10 +193,14 @@ func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []stri
// GetSection returns map for the given section
func
(
c
*
ConfigContainer
)
GetSection
(
section
string
)
(
map
[
string
]
string
,
error
)
{
if
v
,
ok
:=
c
.
data
[
section
];
ok
{
return
v
.
(
map
[
string
]
string
),
nil
}
return
nil
,
errors
.
New
(
"not exist setction"
)
if
v
,
ok
:=
c
.
data
[
section
]
.
(
map
[
string
]
interface
{});
ok
{
mapstr
:=
make
(
map
[
string
]
string
)
for
k
,
val
:=
range
v
{
mapstr
[
k
]
=
config
.
ToString
(
val
)
}
return
mapstr
,
nil
}
return
nil
,
fmt
.
Errorf
(
"section '%s' not found"
,
section
)
}
// SaveConfigFile save the config into file
...
...
config/xml/xml_test.go
View file @
5af26446
...
...
@@ -37,6 +37,10 @@ func TestXML(t *testing.T) {
<copyrequestbody>true</copyrequestbody>
<path1>${GOPATH}</path1>
<path2>${GOPATH||/home/go}</path2>
<mysection>
<id>1</id>
<name>MySection</name>
</mysection>
</config>
`
keyValue
=
map
[
string
]
interface
{}{
...
...
@@ -65,11 +69,22 @@ func TestXML(t *testing.T) {
}
f
.
Close
()
defer
os
.
Remove
(
"testxml.conf"
)
xmlconf
,
err
:=
config
.
NewConfig
(
"xml"
,
"testxml.conf"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
var
xmlsection
map
[
string
]
string
xmlsection
,
err
=
xmlconf
.
GetSection
(
"mysection"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
len
(
xmlsection
)
==
0
{
t
.
Error
(
"section should not be empty"
)
}
for
k
,
v
:=
range
keyValue
{
var
(
...
...
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