From 8ff74e71cbd61556878fd822e23ba061de6bb841 Mon Sep 17 00:00:00 2001 From: ysqi Date: Wed, 2 Mar 2016 22:44:20 +0800 Subject: [PATCH 1/2] Fixed #1735 Return empty []string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Need return empty []string if config value is empty. split `“”` ==> []string{}, Not []string{“”} --- config/ini.go | 6 +++++- config/ini_test.go | 1 + config/xml/xml.go | 6 +++++- config/xml/xml_test.go | 3 +++ config/yaml/yaml.go | 6 +++++- config/yaml/yaml_test.go | 4 ++++ 6 files changed, 23 insertions(+), 3 deletions(-) diff --git a/config/ini.go b/config/ini.go index da6f2b3a..a690ede0 100644 --- a/config/ini.go +++ b/config/ini.go @@ -270,7 +270,11 @@ func (c *IniConfigContainer) DefaultString(key string, defaultval string) string // Strings returns the []string value for a given key. func (c *IniConfigContainer) Strings(key string) []string { - return strings.Split(c.String(key), ";") + v := c.String(key) + if v == "" { + return []string{} + } + return strings.Split(v, ";") } // DefaultStrings returns the []string value for a given key. diff --git a/config/ini_test.go b/config/ini_test.go index 11063d99..93fce61f 100644 --- a/config/ini_test.go +++ b/config/ini_test.go @@ -71,6 +71,7 @@ peers = one;two;three "null": "", "demo2::key1": "", "error": "", + "emptystrings": []string{}, } ) diff --git a/config/xml/xml.go b/config/xml/xml.go index ffb32862..2757b0bc 100644 --- a/config/xml/xml.go +++ b/config/xml/xml.go @@ -174,7 +174,11 @@ func (c *ConfigContainer) DefaultString(key string, defaultval string) string { // Strings returns the []string value for a given key. func (c *ConfigContainer) Strings(key string) []string { - return strings.Split(c.String(key), ";") + v := c.String(key) + if v == "" { + return []string{} + } + return strings.Split(v, ";") } // DefaultStrings returns the []string value for a given key. diff --git a/config/xml/xml_test.go b/config/xml/xml_test.go index fa3c17f1..308e257d 100644 --- a/config/xml/xml_test.go +++ b/config/xml/xml_test.go @@ -82,4 +82,7 @@ func TestXML(t *testing.T) { if xmlconf.String("name") != "astaxie" { t.Fatal("get name error") } + if len(xmlconf.Strings("emptystrings")) != 0 { + t.Fatal("get emtpy strings error") + } } diff --git a/config/yaml/yaml.go b/config/yaml/yaml.go index 9a96ac92..9a4fb2e9 100644 --- a/config/yaml/yaml.go +++ b/config/yaml/yaml.go @@ -211,7 +211,11 @@ func (c *ConfigContainer) DefaultString(key string, defaultval string) string { // Strings returns the []string value for a given key. func (c *ConfigContainer) Strings(key string) []string { - return strings.Split(c.String(key), ";") + v := c.String(key) + if v == "" { + return []string{} + } + return strings.Split(v, ";") } // DefaultStrings returns the []string value for a given key. diff --git a/config/yaml/yaml_test.go b/config/yaml/yaml_test.go index 19ecdca1..3bbaaa32 100644 --- a/config/yaml/yaml_test.go +++ b/config/yaml/yaml_test.go @@ -79,4 +79,8 @@ func TestYaml(t *testing.T) { if yamlconf.String("name") != "astaxie" { t.Fatal("get name error") } + + if len(yamlconf.Strings("emptystrings")) != 0 { + t.Fatal("get emtpy strings error") + } } From 19d921d3f51321851ec1f301b56748f5e209077b Mon Sep 17 00:00:00 2001 From: ysqi Date: Thu, 3 Mar 2016 20:03:23 +0800 Subject: [PATCH 2/2] Return nil not empty []string{} Return nil if config value does not exist or is empty --- config/fake.go | 8 ++++++-- config/ini.go | 5 +++-- config/json.go | 4 ++-- config/xml/xml.go | 4 ++-- config/xml/xml_test.go | 2 +- config/yaml/yaml.go | 4 ++-- config/yaml/yaml_test.go | 2 +- 7 files changed, 17 insertions(+), 12 deletions(-) diff --git a/config/fake.go b/config/fake.go index 6daaca2c..7e362608 100644 --- a/config/fake.go +++ b/config/fake.go @@ -46,12 +46,16 @@ func (c *fakeConfigContainer) DefaultString(key string, defaultval string) strin } func (c *fakeConfigContainer) Strings(key string) []string { - return strings.Split(c.getData(key), ";") + v := c.getData(key) + if v == "" { + return nil + } + return strings.Split(v, ";") } func (c *fakeConfigContainer) DefaultStrings(key string, defaultval []string) []string { v := c.Strings(key) - if len(v) == 0 { + if v == nil { return defaultval } return v diff --git a/config/ini.go b/config/ini.go index a690ede0..9c19b9b1 100644 --- a/config/ini.go +++ b/config/ini.go @@ -269,10 +269,11 @@ func (c *IniConfigContainer) DefaultString(key string, defaultval string) string } // Strings returns the []string value for a given key. +// Return nil if config value does not exist or is empty. func (c *IniConfigContainer) Strings(key string) []string { v := c.String(key) if v == "" { - return []string{} + return nil } return strings.Split(v, ";") } @@ -281,7 +282,7 @@ func (c *IniConfigContainer) Strings(key string) []string { // if err != nil return defaltval func (c *IniConfigContainer) DefaultStrings(key string, defaultval []string) []string { v := c.Strings(key) - if len(v) == 0 { + if v == nil { return defaultval } return v diff --git a/config/json.go b/config/json.go index 0bc1d456..fce517eb 100644 --- a/config/json.go +++ b/config/json.go @@ -173,7 +173,7 @@ func (c *JSONConfigContainer) DefaultString(key string, defaultval string) strin func (c *JSONConfigContainer) Strings(key string) []string { stringVal := c.String(key) if stringVal == "" { - return []string{} + return nil } return strings.Split(c.String(key), ";") } @@ -181,7 +181,7 @@ func (c *JSONConfigContainer) Strings(key string) []string { // DefaultStrings returns the []string value for a given key. // if err != nil return defaltval func (c *JSONConfigContainer) DefaultStrings(key string, defaultval []string) []string { - if v := c.Strings(key); len(v) > 0 { + if v := c.Strings(key); v != nil { return v } return defaultval diff --git a/config/xml/xml.go b/config/xml/xml.go index 2757b0bc..b5291bf4 100644 --- a/config/xml/xml.go +++ b/config/xml/xml.go @@ -176,7 +176,7 @@ func (c *ConfigContainer) DefaultString(key string, defaultval string) string { func (c *ConfigContainer) Strings(key string) []string { v := c.String(key) if v == "" { - return []string{} + return nil } return strings.Split(v, ";") } @@ -185,7 +185,7 @@ func (c *ConfigContainer) Strings(key string) []string { // if err != nil return defaltval func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []string { v := c.Strings(key) - if len(v) == 0 { + if v == nil { return defaultval } return v diff --git a/config/xml/xml_test.go b/config/xml/xml_test.go index 308e257d..60dcba54 100644 --- a/config/xml/xml_test.go +++ b/config/xml/xml_test.go @@ -82,7 +82,7 @@ func TestXML(t *testing.T) { if xmlconf.String("name") != "astaxie" { t.Fatal("get name error") } - if len(xmlconf.Strings("emptystrings")) != 0 { + if xmlconf.Strings("emptystrings") != nil { t.Fatal("get emtpy strings error") } } diff --git a/config/yaml/yaml.go b/config/yaml/yaml.go index 9a4fb2e9..7e1d0426 100644 --- a/config/yaml/yaml.go +++ b/config/yaml/yaml.go @@ -213,7 +213,7 @@ func (c *ConfigContainer) DefaultString(key string, defaultval string) string { func (c *ConfigContainer) Strings(key string) []string { v := c.String(key) if v == "" { - return []string{} + return nil } return strings.Split(v, ";") } @@ -222,7 +222,7 @@ func (c *ConfigContainer) Strings(key string) []string { // if err != nil return defaltval func (c *ConfigContainer) DefaultStrings(key string, defaultval []string) []string { v := c.Strings(key) - if len(v) == 0 { + if v == nil { return defaultval } return v diff --git a/config/yaml/yaml_test.go b/config/yaml/yaml_test.go index 3bbaaa32..80cbb8fe 100644 --- a/config/yaml/yaml_test.go +++ b/config/yaml/yaml_test.go @@ -80,7 +80,7 @@ func TestYaml(t *testing.T) { t.Fatal("get name error") } - if len(yamlconf.Strings("emptystrings")) != 0 { + if yamlconf.Strings("emptystrings") != nil { t.Fatal("get emtpy strings error") } }