1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-20 16:40:49 +00:00

Return nil not empty []string{}

Return nil if config value does not exist or is empty
This commit is contained in:
ysqi 2016-03-03 20:03:23 +08:00
parent 8ff74e71cb
commit 19d921d3f5
7 changed files with 17 additions and 12 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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")
}
}

View File

@ -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

View File

@ -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")
}
}