1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-16 14:40:56 +00:00

Merge pull request #323 from pengfei-xue/devel

case insensitive for section and key for ini config
This commit is contained in:
astaxie 2013-11-28 19:29:37 -08:00
commit 9c4414f995
2 changed files with 18 additions and 2 deletions

View File

@ -13,7 +13,7 @@ import (
) )
var ( var (
DEFAULT_SECTION = "DEFAULT" DEFAULT_SECTION = "default"
bNumComment = []byte{'#'} // number sign bNumComment = []byte{'#'} // number sign
bSemComment = []byte{';'} // semicolon bSemComment = []byte{';'} // semicolon
bEmpty = []byte{} bEmpty = []byte{}
@ -75,6 +75,7 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
if bytes.HasPrefix(line, sectionStart) && bytes.HasSuffix(line, sectionEnd) { if bytes.HasPrefix(line, sectionStart) && bytes.HasSuffix(line, sectionEnd) {
section = string(line[1 : len(line)-1]) section = string(line[1 : len(line)-1])
section = strings.ToLower(section) // section name case insensitive
if comment.Len() > 0 { if comment.Len() > 0 {
cfg.sectionComment[section] = comment.String() cfg.sectionComment[section] = comment.String()
comment.Reset() comment.Reset()
@ -92,7 +93,8 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
val = bytes.Trim(val, `"`) 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) cfg.data[section][key] = string(val)
if comment.Len() > 0 { if comment.Len() > 0 {
cfg.keycomment[section+"."+key] = comment.String() cfg.keycomment[section+"."+key] = comment.String()
@ -115,25 +117,30 @@ type IniConfigContainer struct {
// Bool returns the boolean value for a given key. // Bool returns the boolean value for a given key.
func (c *IniConfigContainer) Bool(key string) (bool, error) { func (c *IniConfigContainer) Bool(key string) (bool, error) {
key = strings.ToLower(key)
return strconv.ParseBool(c.getdata(key)) return strconv.ParseBool(c.getdata(key))
} }
// Int returns the integer value for a given key. // Int returns the integer value for a given key.
func (c *IniConfigContainer) Int(key string) (int, error) { func (c *IniConfigContainer) Int(key string) (int, error) {
key = strings.ToLower(key)
return strconv.Atoi(c.getdata(key)) return strconv.Atoi(c.getdata(key))
} }
func (c *IniConfigContainer) Int64(key string) (int64, error) { func (c *IniConfigContainer) Int64(key string) (int64, error) {
key = strings.ToLower(key)
return strconv.ParseInt(c.getdata(key), 10, 64) return strconv.ParseInt(c.getdata(key), 10, 64)
} }
// Float returns the float value for a given key. // Float returns the float value for a given key.
func (c *IniConfigContainer) Float(key string) (float64, error) { func (c *IniConfigContainer) Float(key string) (float64, error) {
key = strings.ToLower(key)
return strconv.ParseFloat(c.getdata(key), 64) return strconv.ParseFloat(c.getdata(key), 64)
} }
// String returns the string value for a given key. // String returns the string value for a given key.
func (c *IniConfigContainer) String(key string) string { func (c *IniConfigContainer) String(key string) string {
key = strings.ToLower(key)
return c.getdata(key) return c.getdata(key)
} }
@ -144,7 +151,9 @@ func (c *IniConfigContainer) Set(key, value string) error {
if len(key) == 0 { if len(key) == 0 {
return errors.New("key is empty") return errors.New("key is empty")
} }
var section, k string var section, k string
key = strings.ToLower(key)
sectionkey := strings.Split(key, ".") sectionkey := strings.Split(key, ".")
if len(sectionkey) >= 2 { if len(sectionkey) >= 2 {
section = sectionkey[0] 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) { func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) {
key = strings.ToLower(key)
if v, ok := c.data[key]; ok { if v, ok := c.data[key]; ok {
return v, nil return v, nil
} }
@ -171,7 +181,9 @@ func (c *IniConfigContainer) getdata(key string) string {
if len(key) == 0 { if len(key) == 0 {
return "" return ""
} }
var section, k string var section, k string
key = strings.ToLower(key)
sectionkey := strings.Split(key, ".") sectionkey := strings.Split(key, ".")
if len(sectionkey) >= 2 { if len(sectionkey) >= 2 {
section = sectionkey[0] section = sectionkey[0]

View File

@ -18,6 +18,7 @@ copyrequestbody = true
[demo] [demo]
key1="asta" key1="asta"
key2 = "xie" key2 = "xie"
CaseInsensitive = true
` `
func TestIni(t *testing.T) { func TestIni(t *testing.T) {
@ -74,4 +75,7 @@ func TestIni(t *testing.T) {
if iniconf.String("demo.key2") != "xie" { if iniconf.String("demo.key2") != "xie" {
t.Fatal("get demo.key2 error") t.Fatal("get demo.key2 error")
} }
if v, err := iniconf.Bool("demo.caseinsensitive"); err != nil || v != true {
t.Fatal("get demo.caseinsensitive error")
}
} }