From 8e7fe8bb66d2322d75397ce6a51097f85204a837 Mon Sep 17 00:00:00 2001 From: Pengfei Xue Date: Fri, 29 Nov 2013 10:17:35 +0800 Subject: [PATCH] case insensitive for section and key for ini config --- config/ini.go | 16 ++++++++++++++-- config/ini_test.go | 4 ++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/config/ini.go b/config/ini.go index f4d5a316..c35af278 100644 --- a/config/ini.go +++ b/config/ini.go @@ -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] diff --git a/config/ini_test.go b/config/ini_test.go index 1cbbdc01..96ffd446 100644 --- a/config/ini_test.go +++ b/config/ini_test.go @@ -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") + } }