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
8e7fe8bb
Commit
8e7fe8bb
authored
Nov 29, 2013
by
Pengfei Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
case insensitive for section and key for ini config
parent
0ba36763
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
2 deletions
+18
-2
ini.go
config/ini.go
+14
-2
ini_test.go
config/ini_test.go
+4
-0
No files found.
config/ini.go
View file @
8e7fe8bb
...
...
@@ -13,7 +13,7 @@ import (
)
var
(
DEFAULT_SECTION
=
"
DEFAULT
"
DEFAULT_SECTION
=
"
default
"
bNumComment
=
[]
byte
{
'#'
}
// number sign
bSemComment
=
[]
byte
{
';'
}
// semicolon
bEmpty
=
[]
byte
{}
...
...
@@ -75,6 +75,7 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
if
bytes
.
HasPrefix
(
line
,
sectionStart
)
&&
bytes
.
HasSuffix
(
line
,
sectionEnd
)
{
section
=
string
(
line
[
1
:
len
(
line
)
-
1
])
section
=
strings
.
ToLower
(
section
)
// section name case insensitive
if
comment
.
Len
()
>
0
{
cfg
.
sectionComment
[
section
]
=
comment
.
String
()
comment
.
Reset
()
...
...
@@ -92,7 +93,8 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
val
=
bytes
.
Trim
(
val
,
`"`
)
}
key
:=
string
(
bytes
.
TrimSpace
(
keyval
[
0
]))
key
:=
string
(
bytes
.
TrimSpace
(
keyval
[
0
]))
// key name case insensitive
key
=
strings
.
ToLower
(
key
)
cfg
.
data
[
section
][
key
]
=
string
(
val
)
if
comment
.
Len
()
>
0
{
cfg
.
keycomment
[
section
+
"."
+
key
]
=
comment
.
String
()
...
...
@@ -115,25 +117,30 @@ type IniConfigContainer struct {
// Bool returns the boolean value for a given key.
func
(
c
*
IniConfigContainer
)
Bool
(
key
string
)
(
bool
,
error
)
{
key
=
strings
.
ToLower
(
key
)
return
strconv
.
ParseBool
(
c
.
getdata
(
key
))
}
// Int returns the integer value for a given key.
func
(
c
*
IniConfigContainer
)
Int
(
key
string
)
(
int
,
error
)
{
key
=
strings
.
ToLower
(
key
)
return
strconv
.
Atoi
(
c
.
getdata
(
key
))
}
func
(
c
*
IniConfigContainer
)
Int64
(
key
string
)
(
int64
,
error
)
{
key
=
strings
.
ToLower
(
key
)
return
strconv
.
ParseInt
(
c
.
getdata
(
key
),
10
,
64
)
}
// Float returns the float value for a given key.
func
(
c
*
IniConfigContainer
)
Float
(
key
string
)
(
float64
,
error
)
{
key
=
strings
.
ToLower
(
key
)
return
strconv
.
ParseFloat
(
c
.
getdata
(
key
),
64
)
}
// String returns the string value for a given key.
func
(
c
*
IniConfigContainer
)
String
(
key
string
)
string
{
key
=
strings
.
ToLower
(
key
)
return
c
.
getdata
(
key
)
}
...
...
@@ -144,7 +151,9 @@ func (c *IniConfigContainer) Set(key, value string) error {
if
len
(
key
)
==
0
{
return
errors
.
New
(
"key is empty"
)
}
var
section
,
k
string
key
=
strings
.
ToLower
(
key
)
sectionkey
:=
strings
.
Split
(
key
,
"."
)
if
len
(
sectionkey
)
>=
2
{
section
=
sectionkey
[
0
]
...
...
@@ -158,6 +167,7 @@ func (c *IniConfigContainer) Set(key, value string) error {
}
func
(
c
*
IniConfigContainer
)
DIY
(
key
string
)
(
v
interface
{},
err
error
)
{
key
=
strings
.
ToLower
(
key
)
if
v
,
ok
:=
c
.
data
[
key
];
ok
{
return
v
,
nil
}
...
...
@@ -171,7 +181,9 @@ func (c *IniConfigContainer) getdata(key string) string {
if
len
(
key
)
==
0
{
return
""
}
var
section
,
k
string
key
=
strings
.
ToLower
(
key
)
sectionkey
:=
strings
.
Split
(
key
,
"."
)
if
len
(
sectionkey
)
>=
2
{
section
=
sectionkey
[
0
]
...
...
config/ini_test.go
View file @
8e7fe8bb
...
...
@@ -18,6 +18,7 @@ copyrequestbody = true
[demo]
key1="asta"
key2 = "xie"
CaseInsensitive = true
`
func
TestIni
(
t
*
testing
.
T
)
{
...
...
@@ -74,4 +75,7 @@ func TestIni(t *testing.T) {
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"
)
}
}
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