case insensitive for section and key for ini config
Showing
2 changed files
with
18 additions
and
2 deletions
| ... | @@ -13,7 +13,7 @@ import ( | ... | @@ -13,7 +13,7 @@ import ( |
| 13 | ) | 13 | ) |
| 14 | 14 | ||
| 15 | var ( | 15 | var ( |
| 16 | DEFAULT_SECTION = "DEFAULT" | 16 | DEFAULT_SECTION = "default" |
| 17 | bNumComment = []byte{'#'} // number sign | 17 | bNumComment = []byte{'#'} // number sign |
| 18 | bSemComment = []byte{';'} // semicolon | 18 | bSemComment = []byte{';'} // semicolon |
| 19 | bEmpty = []byte{} | 19 | bEmpty = []byte{} |
| ... | @@ -75,6 +75,7 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) { | ... | @@ -75,6 +75,7 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) { |
| 75 | 75 | ||
| 76 | if bytes.HasPrefix(line, sectionStart) && bytes.HasSuffix(line, sectionEnd) { | 76 | if bytes.HasPrefix(line, sectionStart) && bytes.HasSuffix(line, sectionEnd) { |
| 77 | section = string(line[1 : len(line)-1]) | 77 | section = string(line[1 : len(line)-1]) |
| 78 | section = strings.ToLower(section) // section name case insensitive | ||
| 78 | if comment.Len() > 0 { | 79 | if comment.Len() > 0 { |
| 79 | cfg.sectionComment[section] = comment.String() | 80 | cfg.sectionComment[section] = comment.String() |
| 80 | comment.Reset() | 81 | comment.Reset() |
| ... | @@ -92,7 +93,8 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) { | ... | @@ -92,7 +93,8 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) { |
| 92 | val = bytes.Trim(val, `"`) | 93 | val = bytes.Trim(val, `"`) |
| 93 | } | 94 | } |
| 94 | 95 | ||
| 95 | key := string(bytes.TrimSpace(keyval[0])) | 96 | key := string(bytes.TrimSpace(keyval[0])) // key name case insensitive |
| 97 | key = strings.ToLower(key) | ||
| 96 | cfg.data[section][key] = string(val) | 98 | cfg.data[section][key] = string(val) |
| 97 | if comment.Len() > 0 { | 99 | if comment.Len() > 0 { |
| 98 | cfg.keycomment[section+"."+key] = comment.String() | 100 | cfg.keycomment[section+"."+key] = comment.String() |
| ... | @@ -115,25 +117,30 @@ type IniConfigContainer struct { | ... | @@ -115,25 +117,30 @@ type IniConfigContainer struct { |
| 115 | 117 | ||
| 116 | // Bool returns the boolean value for a given key. | 118 | // Bool returns the boolean value for a given key. |
| 117 | func (c *IniConfigContainer) Bool(key string) (bool, error) { | 119 | func (c *IniConfigContainer) Bool(key string) (bool, error) { |
| 120 | key = strings.ToLower(key) | ||
| 118 | return strconv.ParseBool(c.getdata(key)) | 121 | return strconv.ParseBool(c.getdata(key)) |
| 119 | } | 122 | } |
| 120 | 123 | ||
| 121 | // Int returns the integer value for a given key. | 124 | // Int returns the integer value for a given key. |
| 122 | func (c *IniConfigContainer) Int(key string) (int, error) { | 125 | func (c *IniConfigContainer) Int(key string) (int, error) { |
| 126 | key = strings.ToLower(key) | ||
| 123 | return strconv.Atoi(c.getdata(key)) | 127 | return strconv.Atoi(c.getdata(key)) |
| 124 | } | 128 | } |
| 125 | 129 | ||
| 126 | func (c *IniConfigContainer) Int64(key string) (int64, error) { | 130 | func (c *IniConfigContainer) Int64(key string) (int64, error) { |
| 131 | key = strings.ToLower(key) | ||
| 127 | return strconv.ParseInt(c.getdata(key), 10, 64) | 132 | return strconv.ParseInt(c.getdata(key), 10, 64) |
| 128 | } | 133 | } |
| 129 | 134 | ||
| 130 | // Float returns the float value for a given key. | 135 | // Float returns the float value for a given key. |
| 131 | func (c *IniConfigContainer) Float(key string) (float64, error) { | 136 | func (c *IniConfigContainer) Float(key string) (float64, error) { |
| 137 | key = strings.ToLower(key) | ||
| 132 | return strconv.ParseFloat(c.getdata(key), 64) | 138 | return strconv.ParseFloat(c.getdata(key), 64) |
| 133 | } | 139 | } |
| 134 | 140 | ||
| 135 | // String returns the string value for a given key. | 141 | // String returns the string value for a given key. |
| 136 | func (c *IniConfigContainer) String(key string) string { | 142 | func (c *IniConfigContainer) String(key string) string { |
| 143 | key = strings.ToLower(key) | ||
| 137 | return c.getdata(key) | 144 | return c.getdata(key) |
| 138 | } | 145 | } |
| 139 | 146 | ||
| ... | @@ -144,7 +151,9 @@ func (c *IniConfigContainer) Set(key, value string) error { | ... | @@ -144,7 +151,9 @@ func (c *IniConfigContainer) Set(key, value string) error { |
| 144 | if len(key) == 0 { | 151 | if len(key) == 0 { |
| 145 | return errors.New("key is empty") | 152 | return errors.New("key is empty") |
| 146 | } | 153 | } |
| 154 | |||
| 147 | var section, k string | 155 | var section, k string |
| 156 | key = strings.ToLower(key) | ||
| 148 | sectionkey := strings.Split(key, ".") | 157 | sectionkey := strings.Split(key, ".") |
| 149 | if len(sectionkey) >= 2 { | 158 | if len(sectionkey) >= 2 { |
| 150 | section = sectionkey[0] | 159 | section = sectionkey[0] |
| ... | @@ -158,6 +167,7 @@ func (c *IniConfigContainer) Set(key, value string) error { | ... | @@ -158,6 +167,7 @@ func (c *IniConfigContainer) Set(key, value string) error { |
| 158 | } | 167 | } |
| 159 | 168 | ||
| 160 | func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) { | 169 | func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) { |
| 170 | key = strings.ToLower(key) | ||
| 161 | if v, ok := c.data[key]; ok { | 171 | if v, ok := c.data[key]; ok { |
| 162 | return v, nil | 172 | return v, nil |
| 163 | } | 173 | } |
| ... | @@ -171,7 +181,9 @@ func (c *IniConfigContainer) getdata(key string) string { | ... | @@ -171,7 +181,9 @@ func (c *IniConfigContainer) getdata(key string) string { |
| 171 | if len(key) == 0 { | 181 | if len(key) == 0 { |
| 172 | return "" | 182 | return "" |
| 173 | } | 183 | } |
| 184 | |||
| 174 | var section, k string | 185 | var section, k string |
| 186 | key = strings.ToLower(key) | ||
| 175 | sectionkey := strings.Split(key, ".") | 187 | sectionkey := strings.Split(key, ".") |
| 176 | if len(sectionkey) >= 2 { | 188 | if len(sectionkey) >= 2 { |
| 177 | section = sectionkey[0] | 189 | section = sectionkey[0] | ... | ... |
| ... | @@ -18,6 +18,7 @@ copyrequestbody = true | ... | @@ -18,6 +18,7 @@ copyrequestbody = true |
| 18 | [demo] | 18 | [demo] |
| 19 | key1="asta" | 19 | key1="asta" |
| 20 | key2 = "xie" | 20 | key2 = "xie" |
| 21 | CaseInsensitive = true | ||
| 21 | ` | 22 | ` |
| 22 | 23 | ||
| 23 | func TestIni(t *testing.T) { | 24 | func TestIni(t *testing.T) { |
| ... | @@ -74,4 +75,7 @@ func TestIni(t *testing.T) { | ... | @@ -74,4 +75,7 @@ func TestIni(t *testing.T) { |
| 74 | if iniconf.String("demo.key2") != "xie" { | 75 | if iniconf.String("demo.key2") != "xie" { |
| 75 | t.Fatal("get demo.key2 error") | 76 | t.Fatal("get demo.key2 error") |
| 76 | } | 77 | } |
| 78 | if v, err := iniconf.Bool("demo.caseinsensitive"); err != nil || v != true { | ||
| 79 | t.Fatal("get demo.caseinsensitive error") | ||
| 80 | } | ||
| 77 | } | 81 | } | ... | ... |
-
Please register or sign in to post a comment