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
8e7fe8bb
authored
2013-11-29 10:17:35 +0800
by
Pengfei Xue
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
case insensitive for section and key for ini config
1 parent
0ba36763
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
2 deletions
config/ini.go
config/ini_test.go
config/ini.go
View file @
8e7fe8b
...
...
@@ -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 @
8e7fe8b
...
...
@@ -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
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