diff --git a/config/config.go b/config/config.go index 5fb0dd8144f1ff874472c1c7a417fdbcd56ac51c..5e4c2e9c4620bc4521c7e210fc380d162ca29283 100644 --- a/config/config.go +++ b/config/config.go @@ -6,8 +6,9 @@ import ( // ConfigContainer defines how to get and set value from configuration raw data. type ConfigContainer interface { - Set(key, val string) error // support section::key type in given key when using ini type. - String(key string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same. + Set(key, val string) error // support section::key type in given key when using ini type. + String(key string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same. + Strings(key string) []string //get string slice Int(key string) (int, error) Int64(key string) (int64, error) Bool(key string) (bool, error) diff --git a/config/fake.go b/config/fake.go index 05279932b164d47c2f75d56718b36e7803d78852..26a9f4301bab02878372a534d316b725a6b79327 100644 --- a/config/fake.go +++ b/config/fake.go @@ -25,6 +25,10 @@ func (c *fakeConfigContainer) String(key string) string { return c.getData(key) } +func (c *fakeConfigContainer) Strings(key string) []string { + return strings.Split(c.getData(key), ";") +} + func (c *fakeConfigContainer) Int(key string) (int, error) { return strconv.Atoi(c.getData(key)) } diff --git a/config/ini.go b/config/ini.go index 22c23f405864d4354ab3fc099d7805f233db2941..75e6486c49d8fcb980a6b06f59cffabee6684bac 100644 --- a/config/ini.go +++ b/config/ini.go @@ -146,6 +146,11 @@ func (c *IniConfigContainer) String(key string) string { return c.getdata(key) } +// Strings returns the []string value for a given key. +func (c *IniConfigContainer) Strings(key string) []string { + return strings.Split(c.String(key), ";") +} + // WriteValue writes a new value for key. // if write to one section, the key need be "section::key". // if the section is not existed, it panics. diff --git a/config/ini_test.go b/config/ini_test.go index cf87e77c41190afaee252817c6d7dc5acf342165..08a69e500c20e689a55a452fe037f85416b50b4e 100644 --- a/config/ini_test.go +++ b/config/ini_test.go @@ -19,6 +19,7 @@ copyrequestbody = true key1="asta" key2 = "xie" CaseInsensitive = true +peers = one;two;three ` func TestIni(t *testing.T) { @@ -78,4 +79,11 @@ func TestIni(t *testing.T) { if v, err := iniconf.Bool("demo::caseinsensitive"); err != nil || v != true { t.Fatal("get demo.caseinsensitive error") } + + if data := iniconf.Strings("demo::peers"); len(data) != 3 { + t.Fatal("get strings error", data) + } else if data[0] != "one" { + t.Fatal("get first params error not equat to one") + } + } diff --git a/config/json.go b/config/json.go index 883e0674ea78e52f33c9d5a49fbdad9527215d2f..24874e8a026417fb5f544a4a4abb315930034fe8 100644 --- a/config/json.go +++ b/config/json.go @@ -116,6 +116,11 @@ func (c *JsonConfigContainer) String(key string) string { return "" } +// Strings returns the []string value for a given key. +func (c *JsonConfigContainer) Strings(key string) []string { + return strings.Split(c.String(key), ";") +} + // WriteValue writes a new value for key. func (c *JsonConfigContainer) Set(key, val string) error { c.Lock() diff --git a/config/xml.go b/config/xml.go index 35f19336d9dc11b1247a5008199d783fa8b08866..7943d8fe5c4f112adec04e7a2a0cd09589a73ec3 100644 --- a/config/xml.go +++ b/config/xml.go @@ -5,6 +5,7 @@ import ( "io/ioutil" "os" "strconv" + "strings" "sync" "github.com/beego/x2j" @@ -72,6 +73,11 @@ func (c *XMLConfigContainer) String(key string) string { return "" } +// Strings returns the []string value for a given key. +func (c *XMLConfigContainer) Strings(key string) []string { + return strings.Split(c.String(key), ";") +} + // WriteValue writes a new value for key. func (c *XMLConfigContainer) Set(key, val string) error { c.Lock() diff --git a/config/yaml.go b/config/yaml.go index 394cb3b2c0792367fc33237a4bd2533c346d292c..bd10b84682cbbc3e488dd024036c3b939b237d44 100644 --- a/config/yaml.go +++ b/config/yaml.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "log" "os" + "strings" "sync" "github.com/beego/goyaml2" @@ -117,6 +118,11 @@ func (c *YAMLConfigContainer) String(key string) string { return "" } +// Strings returns the []string value for a given key. +func (c *YAMLConfigContainer) Strings(key string) []string { + return strings.Split(c.String(key), ";") +} + // WriteValue writes a new value for key. func (c *YAMLConfigContainer) Set(key, val string) error { c.Lock()