From 19d921d3f51321851ec1f301b56748f5e209077b Mon Sep 17 00:00:00 2001 From: ysqi Date: Thu, 3 Mar 2016 20:03:23 +0800 Subject: [PATCH] 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") } }