9457e61a by fuxiaohei

code style simplify

1 parent 20e05a39
...@@ -83,8 +83,7 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) { ...@@ -83,8 +83,7 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
83 } 83 }
84 84
85 if bytes.HasPrefix(line, sectionStart) && bytes.HasSuffix(line, sectionEnd) { 85 if bytes.HasPrefix(line, sectionStart) && bytes.HasSuffix(line, sectionEnd) {
86 section = string(line[1 : len(line)-1]) 86 section = strings.ToLower(string(line[1 : len(line)-1])) // section name case insensitive
87 section = strings.ToLower(section) // section name case insensitive
88 if comment.Len() > 0 { 87 if comment.Len() > 0 {
89 cfg.sectionComment[section] = comment.String() 88 cfg.sectionComment[section] = comment.String()
90 comment.Reset() 89 comment.Reset()
...@@ -92,24 +91,25 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) { ...@@ -92,24 +91,25 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) {
92 if _, ok := cfg.data[section]; !ok { 91 if _, ok := cfg.data[section]; !ok {
93 cfg.data[section] = make(map[string]string) 92 cfg.data[section] = make(map[string]string)
94 } 93 }
95 } else { 94 continue
95 }
96
96 if _, ok := cfg.data[section]; !ok { 97 if _, ok := cfg.data[section]; !ok {
97 cfg.data[section] = make(map[string]string) 98 cfg.data[section] = make(map[string]string)
98 } 99 }
99 keyval := bytes.SplitN(line, bEqual, 2) 100 keyValue := bytes.SplitN(line, bEqual, 2)
100 val := bytes.TrimSpace(keyval[1]) 101 val := bytes.TrimSpace(keyValue[1])
101 if bytes.HasPrefix(val, bDQuote) { 102 if bytes.HasPrefix(val, bDQuote) {
102 val = bytes.Trim(val, `"`) 103 val = bytes.Trim(val, `"`)
103 } 104 }
104 105
105 key := string(bytes.TrimSpace(keyval[0])) // key name case insensitive 106 key := string(bytes.TrimSpace(keyValue[0])) // key name case insensitive
106 key = strings.ToLower(key) 107 key = strings.ToLower(key)
107 cfg.data[section][key] = string(val) 108 cfg.data[section][key] = string(val)
108 if comment.Len() > 0 { 109 if comment.Len() > 0 {
109 cfg.keycomment[section+"."+key] = comment.String() 110 cfg.keyComment[section+"."+key] = comment.String()
110 comment.Reset() 111 comment.Reset()
111 } 112 }
112 }
113 113
114 } 114 }
115 return cfg, nil 115 return cfg, nil
...@@ -121,38 +121,34 @@ type IniConfigContainer struct { ...@@ -121,38 +121,34 @@ type IniConfigContainer struct {
121 filename string 121 filename string
122 data map[string]map[string]string // section=> key:val 122 data map[string]map[string]string // section=> key:val
123 sectionComment map[string]string // section : comment 123 sectionComment map[string]string // section : comment
124 keycomment map[string]string // id: []{comment, key...}; id 1 is for main comment. 124 keyComment map[string]string // id: []{comment, key...}; id 1 is for main comment.
125 sync.RWMutex 125 sync.RWMutex
126 } 126 }
127 127
128 // Bool returns the boolean value for a given key. 128 // Bool returns the boolean value for a given key.
129 func (c *IniConfigContainer) Bool(key string) (bool, error) { 129 func (c *IniConfigContainer) Bool(key string) (bool, error) {
130 key = strings.ToLower(key) 130 return strconv.ParseBool(c.getdata(strings.ToLower(key)))
131 return strconv.ParseBool(c.getdata(key))
132 } 131 }
133 132
134 // Int returns the integer value for a given key. 133 // Int returns the integer value for a given key.
135 func (c *IniConfigContainer) Int(key string) (int, error) { 134 func (c *IniConfigContainer) Int(key string) (int, error) {
136 key = strings.ToLower(key) 135 return strconv.Atoi(c.getdata(strings.ToLower(key)))
137 return strconv.Atoi(c.getdata(key))
138 } 136 }
139 137
140 // Int64 returns the int64 value for a given key. 138 // Int64 returns the int64 value for a given key.
141 func (c *IniConfigContainer) Int64(key string) (int64, error) { 139 func (c *IniConfigContainer) Int64(key string) (int64, error) {
142 key = strings.ToLower(key) 140 return strconv.ParseInt(c.getdata(strings.ToLower(key)), 10, 64)
143 return strconv.ParseInt(c.getdata(key), 10, 64)
144 } 141 }
145 142
146 // Float returns the float value for a given key. 143 // Float returns the float value for a given key.
147 func (c *IniConfigContainer) Float(key string) (float64, error) { 144 func (c *IniConfigContainer) Float(key string) (float64, error) {
148 key = strings.ToLower(key) 145 return strconv.ParseFloat(c.getdata(strings.ToLower(key)), 64)
149 return strconv.ParseFloat(c.getdata(key), 64)
150 } 146 }
151 147
152 // String returns the string value for a given key. 148 // String returns the string value for a given key.
153 func (c *IniConfigContainer) String(key string) string { 149 func (c *IniConfigContainer) String(key string) string {
154 key = strings.ToLower(key) 150 key = strings.ToLower(key)
155 return c.getdata(key) 151 return c.getdata(strings.ToLower(key))
156 } 152 }
157 153
158 // Strings returns the []string value for a given key. 154 // Strings returns the []string value for a given key.
...@@ -170,16 +166,19 @@ func (c *IniConfigContainer) Set(key, value string) error { ...@@ -170,16 +166,19 @@ func (c *IniConfigContainer) Set(key, value string) error {
170 return errors.New("key is empty") 166 return errors.New("key is empty")
171 } 167 }
172 168
173 var section, k string 169 var (
174 key = strings.ToLower(key) 170 section, k string
175 sectionkey := strings.Split(key, "::") 171 sectionKey []string = strings.Split(key, "::")
176 if len(sectionkey) >= 2 { 172 )
177 section = sectionkey[0] 173
178 k = sectionkey[1] 174 if len(sectionKey) >= 2 {
175 section = sectionKey[0]
176 k = sectionKey[1]
179 } else { 177 } else {
180 section = DEFAULT_SECTION 178 section = DEFAULT_SECTION
181 k = sectionkey[0] 179 k = sectionKey[0]
182 } 180 }
181
183 if _, ok := c.data[section]; !ok { 182 if _, ok := c.data[section]; !ok {
184 c.data[section] = make(map[string]string) 183 c.data[section] = make(map[string]string)
185 } 184 }
...@@ -189,8 +188,7 @@ func (c *IniConfigContainer) Set(key, value string) error { ...@@ -189,8 +188,7 @@ func (c *IniConfigContainer) Set(key, value string) error {
189 188
190 // DIY returns the raw value by a given key. 189 // DIY returns the raw value by a given key.
191 func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) { 190 func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) {
192 key = strings.ToLower(key) 191 if v, ok := c.data[strings.ToLower(key)]; ok {
193 if v, ok := c.data[key]; ok {
194 return v, nil 192 return v, nil
195 } 193 }
196 return v, errors.New("key not find") 194 return v, errors.New("key not find")
...@@ -204,18 +202,19 @@ func (c *IniConfigContainer) getdata(key string) string { ...@@ -204,18 +202,19 @@ func (c *IniConfigContainer) getdata(key string) string {
204 return "" 202 return ""
205 } 203 }
206 204
207 var section, k string 205 var (
208 key = strings.ToLower(key) 206 section, k string
209 sectionkey := strings.Split(key, "::") 207 sectionKey []string = strings.Split(key, "::")
210 if len(sectionkey) >= 2 { 208 )
211 section = sectionkey[0] 209 if len(sectionKey) >= 2 {
212 k = sectionkey[1] 210 section = sectionKey[0]
211 k = sectionKey[1]
213 } else { 212 } else {
214 section = DEFAULT_SECTION 213 section = DEFAULT_SECTION
215 k = sectionkey[0] 214 k = sectionKey[0]
216 } 215 }
217 if v, ok := c.data[section]; ok { 216 if v, ok := c.data[section]; ok {
218 if vv, o := v[k]; o { 217 if vv, ok2 := v[k]; ok2 {
219 return vv 218 return vv
220 } 219 }
221 } 220 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!