1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 16:30:55 +00:00
This commit is contained in:
ysqi 2016-01-23 14:53:52 +08:00
parent af346e871b
commit 51ae45a799
2 changed files with 83 additions and 9 deletions

View File

@ -27,7 +27,6 @@ import (
"strings" "strings"
"sync" "sync"
"time" "time"
"unicode"
) )
var ( var (
@ -97,9 +96,11 @@ func (ini *IniConfig) parseFile(name string) (*IniConfigContainer, error) {
} }
if bComment != nil { if bComment != nil {
line = bytes.TrimLeft(line, string(bComment)) line = bytes.TrimLeft(line, string(bComment))
line = bytes.TrimLeftFunc(line, unicode.IsSpace) // Need append to a new line if multi-line comments.
comment.Write(line) if comment.Len() > 0 {
comment.WriteByte('\n') comment.WriteByte('\n')
}
comment.Write(line)
continue continue
} }
@ -299,14 +300,35 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
} }
defer f.Close() 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) buf := bytes.NewBuffer(nil)
// Save default section at first place // Save default section at first place
if dt, ok := c.data[defaultSection]; ok { if dt, ok := c.data[defaultSection]; ok {
for key, val := range dt { for key, val := range dt {
if key != " " { if key != " " {
// Write key comments. // Write key comments.
if v, ok := c.keyComment[key]; ok { if v := getCommentStr(defaultSection, key); len(v) > 0 {
if _, err = buf.WriteString(string(bNumComment) + v + lineBreak); err != nil { if _, err = buf.WriteString(v + lineBreak); err != nil {
return err return err
} }
} }
@ -327,8 +349,8 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
for section, dt := range c.data { for section, dt := range c.data {
if section != defaultSection { if section != defaultSection {
// Write section comments. // Write section comments.
if v, ok := c.sectionComment[section]; ok { if v := getCommentStr(section, ""); len(v) > 0 {
if _, err = buf.WriteString(string(bNumComment) + v + lineBreak); err != nil { if _, err = buf.WriteString(v + lineBreak); err != nil {
return err return err
} }
} }
@ -341,8 +363,8 @@ func (c *IniConfigContainer) SaveConfigFile(filename string) (err error) {
for key, val := range dt { for key, val := range dt {
if key != " " { if key != " " {
// Write key comments. // Write key comments.
if v, ok := c.keyComment[key]; ok { if v := getCommentStr(section, key); len(v) > 0 {
if _, err = buf.WriteString(string(bNumComment) + v + lineBreak); err != nil { if _, err = buf.WriteString(v + lineBreak); err != nil {
return err return err
} }
} }

View File

@ -15,6 +15,7 @@
package config package config
import ( import (
"io/ioutil"
"os" "os"
"testing" "testing"
) )
@ -101,3 +102,54 @@ func TestIni(t *testing.T) {
} }
} }
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 if string(data) != saveResult {
t.Fatal("different after save ini config file.")
}
}