From fee3c2b8f9a4ca782346044ab4331728997f5421 Mon Sep 17 00:00:00 2001 From: astaxie Date: Wed, 15 Jan 2014 17:19:03 +0800 Subject: [PATCH] add Strings interface can return []string sep by ; Example: peers = one;Two;Three --- config/config.go | 5 +++-- config/fake.go | 4 ++++ config/ini.go | 5 +++++ config/ini_test.go | 8 ++++++++ config/json.go | 5 +++++ config/xml.go | 6 ++++++ config/yaml.go | 6 ++++++ 7 files changed, 37 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index 5fb0dd81..5e4c2e9c 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 05279932..26a9f430 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 22c23f40..75e6486c 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 cf87e77c..08a69e50 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 883e0674..24874e8a 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 35f19336..7943d8fe 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 394cb3b2..bd10b846 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()