From 20e05a39080ca81ebe6083b45288e43e9878637a Mon Sep 17 00:00:00 2001 From: fuxiaohei Date: Sat, 12 Jul 2014 16:03:14 +0800 Subject: [PATCH 1/2] code style simplify --- config/config.go | 2 +- config/fake.go | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index 8e719562..61e33061 100644 --- a/config/config.go +++ b/config/config.go @@ -39,7 +39,7 @@ func Register(name string, adapter Config) { if adapter == nil { panic("config: Register adapter is nil") } - if _, dup := adapters[name]; dup { + if _, ok := adapters[name]; ok { panic("config: Register called twice for adapter " + name) } adapters[name] = adapter diff --git a/config/fake.go b/config/fake.go index dba1a470..561dccca 100644 --- a/config/fake.go +++ b/config/fake.go @@ -20,13 +20,11 @@ type fakeConfigContainer struct { } func (c *fakeConfigContainer) getData(key string) string { - key = strings.ToLower(key) - return c.data[key] + return c.data[strings.ToLower(key)] } func (c *fakeConfigContainer) Set(key, val string) error { - key = strings.ToLower(key) - c.data[key] = val + c.data[strings.ToLower(key)] = val return nil } @@ -55,8 +53,7 @@ func (c *fakeConfigContainer) Float(key string) (float64, error) { } func (c *fakeConfigContainer) DIY(key string) (interface{}, error) { - key = strings.ToLower(key) - if v, ok := c.data[key]; ok { + if v, ok := c.data[strings.ToLower(key)]; ok { return v, nil } return nil, errors.New("key not find") From 9457e61a0c87be90aa8777f38dad23ab0fb2896f Mon Sep 17 00:00:00 2001 From: fuxiaohei Date: Sat, 12 Jul 2014 22:12:06 +0800 Subject: [PATCH 2/2] code style simplify --- config/ini.go | 89 +++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 45 deletions(-) diff --git a/config/ini.go b/config/ini.go index ba3fd23a..f8b9d01b 100644 --- a/config/ini.go +++ b/config/ini.go @@ -83,8 +83,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 + section = strings.ToLower(string(line[1 : len(line)-1])) // section name case insensitive if comment.Len() > 0 { cfg.sectionComment[section] = comment.String() comment.Reset() @@ -92,23 +91,24 @@ func (ini *IniConfig) Parse(name string) (ConfigContainer, error) { if _, ok := cfg.data[section]; !ok { cfg.data[section] = make(map[string]string) } - } else { - if _, ok := cfg.data[section]; !ok { - cfg.data[section] = make(map[string]string) - } - keyval := bytes.SplitN(line, bEqual, 2) - val := bytes.TrimSpace(keyval[1]) - if bytes.HasPrefix(val, bDQuote) { - val = bytes.Trim(val, `"`) - } + continue + } - 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() - comment.Reset() - } + if _, ok := cfg.data[section]; !ok { + cfg.data[section] = make(map[string]string) + } + keyValue := bytes.SplitN(line, bEqual, 2) + val := bytes.TrimSpace(keyValue[1]) + if bytes.HasPrefix(val, bDQuote) { + val = bytes.Trim(val, `"`) + } + + key := string(bytes.TrimSpace(keyValue[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() + comment.Reset() } } @@ -121,38 +121,34 @@ type IniConfigContainer struct { filename string data map[string]map[string]string // section=> key:val sectionComment map[string]string // section : comment - keycomment map[string]string // id: []{comment, key...}; id 1 is for main comment. + keyComment map[string]string // id: []{comment, key...}; id 1 is for main comment. sync.RWMutex } // 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)) + return strconv.ParseBool(c.getdata(strings.ToLower(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)) + return strconv.Atoi(c.getdata(strings.ToLower(key))) } // Int64 returns the int64 value for a given key. 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(strings.ToLower(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) + return strconv.ParseFloat(c.getdata(strings.ToLower(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) + return c.getdata(strings.ToLower(key)) } // Strings returns the []string value for a given key. @@ -170,16 +166,19 @@ func (c *IniConfigContainer) Set(key, value string) error { 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] - k = sectionkey[1] + var ( + section, k string + sectionKey []string = strings.Split(key, "::") + ) + + if len(sectionKey) >= 2 { + section = sectionKey[0] + k = sectionKey[1] } else { section = DEFAULT_SECTION - k = sectionkey[0] + k = sectionKey[0] } + if _, ok := c.data[section]; !ok { c.data[section] = make(map[string]string) } @@ -189,8 +188,7 @@ func (c *IniConfigContainer) Set(key, value string) error { // DIY returns the raw value by a given key. 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[strings.ToLower(key)]; ok { return v, nil } return v, errors.New("key not find") @@ -204,18 +202,19 @@ func (c *IniConfigContainer) getdata(key string) string { return "" } - var section, k string - key = strings.ToLower(key) - sectionkey := strings.Split(key, "::") - if len(sectionkey) >= 2 { - section = sectionkey[0] - k = sectionkey[1] + var ( + section, k string + sectionKey []string = strings.Split(key, "::") + ) + if len(sectionKey) >= 2 { + section = sectionKey[0] + k = sectionKey[1] } else { section = DEFAULT_SECTION - k = sectionkey[0] + k = sectionKey[0] } if v, ok := c.data[section]; ok { - if vv, o := v[k]; o { + if vv, ok2 := v[k]; ok2 { return vv } }