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
e1f9491a
Commit
e1f9491a
authored
Jan 26, 2016
by
astaxie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1608 from ysqi/iniSaveErrorFix
Fixed #1607
parents
6aeff53d
cf055c9d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
91 additions
and
9 deletions
+91
-9
ini.go
config/ini.go
+31
-9
ini_test.go
config/ini_test.go
+60
-0
No files found.
config/ini.go
View file @
e1f9491a
...
...
@@ -27,7 +27,6 @@ import (
"strings"
"sync"
"time"
"unicode"
)
var
(
...
...
@@ -97,9 +96,11 @@ func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
}
if
bComment
!=
nil
{
line
=
bytes
.
TrimLeft
(
line
,
string
(
bComment
))
line
=
bytes
.
TrimLeftFunc
(
line
,
unicode
.
IsSpace
)
// Need append to a new line if multi-line comments.
if
comment
.
Len
()
>
0
{
comment
.
WriteByte
(
'\n'
)
}
comment
.
Write
(
line
)
comment
.
WriteByte
(
'\n'
)
continue
}
...
...
@@ -299,14 +300,35 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
}
defer
f
.
Close
()
// Get section or key comments. Fixed #1607
getCommentStr
:=
func
(
section
,
key
string
)
string
{
comment
,
ok
:=
""
,
false
if
len
(
key
)
==
0
{
comment
,
ok
=
c
.
sectionComment
[
section
]
}
else
{
comment
,
ok
=
c
.
keyComment
[
section
+
"."
+
key
]
}
if
ok
{
// Empty comment
if
len
(
comment
)
==
0
||
len
(
strings
.
TrimSpace
(
comment
))
==
0
{
return
string
(
bNumComment
)
}
prefix
:=
string
(
bNumComment
)
// Add the line head character "#"
return
prefix
+
strings
.
Replace
(
comment
,
lineBreak
,
lineBreak
+
prefix
,
-
1
)
}
return
""
}
buf
:=
bytes
.
NewBuffer
(
nil
)
// Save default section at first place
if
dt
,
ok
:=
c
.
data
[
defaultSection
];
ok
{
for
key
,
val
:=
range
dt
{
if
key
!=
" "
{
// Write key comments.
if
v
,
ok
:=
c
.
keyComment
[
key
];
ok
{
if
_
,
err
=
buf
.
WriteString
(
string
(
bNumComment
)
+
v
+
lineBreak
);
err
!=
nil
{
if
v
:=
getCommentStr
(
defaultSection
,
key
);
len
(
v
)
>
0
{
if
_
,
err
=
buf
.
WriteString
(
v
+
lineBreak
);
err
!=
nil
{
return
err
}
}
...
...
@@ -327,8 +349,8 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
for
section
,
dt
:=
range
c
.
data
{
if
section
!=
defaultSection
{
// Write section comments.
if
v
,
ok
:=
c
.
sectionComment
[
section
];
ok
{
if
_
,
err
=
buf
.
WriteString
(
string
(
bNumComment
)
+
v
+
lineBreak
);
err
!=
nil
{
if
v
:=
getCommentStr
(
section
,
""
);
len
(
v
)
>
0
{
if
_
,
err
=
buf
.
WriteString
(
v
+
lineBreak
);
err
!=
nil
{
return
err
}
}
...
...
@@ -341,8 +363,8 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
for
key
,
val
:=
range
dt
{
if
key
!=
" "
{
// Write key comments.
if
v
,
ok
:=
c
.
keyComment
[
key
];
ok
{
if
_
,
err
=
buf
.
WriteString
(
string
(
bNumComment
)
+
v
+
lineBreak
);
err
!=
nil
{
if
v
:=
getCommentStr
(
section
,
key
);
len
(
v
)
>
0
{
if
_
,
err
=
buf
.
WriteString
(
v
+
lineBreak
);
err
!=
nil
{
return
err
}
}
...
...
config/ini_test.go
View file @
e1f9491a
...
...
@@ -16,7 +16,9 @@ package config
import
(
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
)
...
...
@@ -121,3 +123,61 @@ peers = one;two;three
}
}
func
TestIniSave
(
t
*
testing
.
T
)
{
const
(
inicontext
=
`
app = app
;comment one
#comment two
# comment three
appname = beeapi
httpport = 8080
# DB Info
# enable db
[dbinfo]
# db type name
# suport mysql,sqlserver
name = mysql
`
saveResult
=
`
app=app
#comment one
#comment two
# comment three
appname=beeapi
httpport=8080
# DB Info
# enable db
[dbinfo]
# db type name
# suport mysql,sqlserver
name=mysql
`
)
cfg
,
err
:=
NewConfigData
(
"ini"
,
[]
byte
(
inicontext
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
name
:=
"newIniConfig.ini"
if
err
:=
cfg
.
SaveConfigFile
(
name
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
os
.
Remove
(
name
)
if
data
,
err
:=
ioutil
.
ReadFile
(
name
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
else
{
cfgData
:=
string
(
data
)
datas
:=
strings
.
Split
(
saveResult
,
"
\n
"
)
for
_
,
line
:=
range
datas
{
if
strings
.
Contains
(
cfgData
,
line
+
"
\n
"
)
==
false
{
t
.
Fatalf
(
"different after save ini config file. need contains %q"
,
line
)
}
}
}
}
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