add Strings interface can return []string sep by ;
Example: peers = one;Two;Three
Showing
7 changed files
with
35 additions
and
0 deletions
| ... | @@ -8,6 +8,7 @@ import ( | ... | @@ -8,6 +8,7 @@ import ( |
| 8 | type ConfigContainer interface { | 8 | type ConfigContainer interface { |
| 9 | Set(key, val string) error // support section::key type in given key when using ini type. | 9 | Set(key, val string) error // support section::key type in given key when using ini type. |
| 10 | String(key string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same. | 10 | String(key string) string // support section::key type in key string when using ini and json type; Int,Int64,Bool,Float,DIY are same. |
| 11 | Strings(key string) []string //get string slice | ||
| 11 | Int(key string) (int, error) | 12 | Int(key string) (int, error) |
| 12 | Int64(key string) (int64, error) | 13 | Int64(key string) (int64, error) |
| 13 | Bool(key string) (bool, error) | 14 | Bool(key string) (bool, error) | ... | ... |
| ... | @@ -25,6 +25,10 @@ func (c *fakeConfigContainer) String(key string) string { | ... | @@ -25,6 +25,10 @@ func (c *fakeConfigContainer) String(key string) string { |
| 25 | return c.getData(key) | 25 | return c.getData(key) |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | func (c *fakeConfigContainer) Strings(key string) []string { | ||
| 29 | return strings.Split(c.getData(key), ";") | ||
| 30 | } | ||
| 31 | |||
| 28 | func (c *fakeConfigContainer) Int(key string) (int, error) { | 32 | func (c *fakeConfigContainer) Int(key string) (int, error) { |
| 29 | return strconv.Atoi(c.getData(key)) | 33 | return strconv.Atoi(c.getData(key)) |
| 30 | } | 34 | } | ... | ... |
| ... | @@ -146,6 +146,11 @@ func (c *IniConfigContainer) String(key string) string { | ... | @@ -146,6 +146,11 @@ func (c *IniConfigContainer) String(key string) string { |
| 146 | return c.getdata(key) | 146 | return c.getdata(key) |
| 147 | } | 147 | } |
| 148 | 148 | ||
| 149 | // Strings returns the []string value for a given key. | ||
| 150 | func (c *IniConfigContainer) Strings(key string) []string { | ||
| 151 | return strings.Split(c.String(key), ";") | ||
| 152 | } | ||
| 153 | |||
| 149 | // WriteValue writes a new value for key. | 154 | // WriteValue writes a new value for key. |
| 150 | // if write to one section, the key need be "section::key". | 155 | // if write to one section, the key need be "section::key". |
| 151 | // if the section is not existed, it panics. | 156 | // if the section is not existed, it panics. | ... | ... |
| ... | @@ -19,6 +19,7 @@ copyrequestbody = true | ... | @@ -19,6 +19,7 @@ copyrequestbody = true |
| 19 | key1="asta" | 19 | key1="asta" |
| 20 | key2 = "xie" | 20 | key2 = "xie" |
| 21 | CaseInsensitive = true | 21 | CaseInsensitive = true |
| 22 | peers = one;two;three | ||
| 22 | ` | 23 | ` |
| 23 | 24 | ||
| 24 | func TestIni(t *testing.T) { | 25 | func TestIni(t *testing.T) { |
| ... | @@ -78,4 +79,11 @@ func TestIni(t *testing.T) { | ... | @@ -78,4 +79,11 @@ func TestIni(t *testing.T) { |
| 78 | if v, err := iniconf.Bool("demo::caseinsensitive"); err != nil || v != true { | 79 | if v, err := iniconf.Bool("demo::caseinsensitive"); err != nil || v != true { |
| 79 | t.Fatal("get demo.caseinsensitive error") | 80 | t.Fatal("get demo.caseinsensitive error") |
| 80 | } | 81 | } |
| 82 | |||
| 83 | if data := iniconf.Strings("demo::peers"); len(data) != 3 { | ||
| 84 | t.Fatal("get strings error", data) | ||
| 85 | } else if data[0] != "one" { | ||
| 86 | t.Fatal("get first params error not equat to one") | ||
| 87 | } | ||
| 88 | |||
| 81 | } | 89 | } | ... | ... |
| ... | @@ -116,6 +116,11 @@ func (c *JsonConfigContainer) String(key string) string { | ... | @@ -116,6 +116,11 @@ func (c *JsonConfigContainer) String(key string) string { |
| 116 | return "" | 116 | return "" |
| 117 | } | 117 | } |
| 118 | 118 | ||
| 119 | // Strings returns the []string value for a given key. | ||
| 120 | func (c *JsonConfigContainer) Strings(key string) []string { | ||
| 121 | return strings.Split(c.String(key), ";") | ||
| 122 | } | ||
| 123 | |||
| 119 | // WriteValue writes a new value for key. | 124 | // WriteValue writes a new value for key. |
| 120 | func (c *JsonConfigContainer) Set(key, val string) error { | 125 | func (c *JsonConfigContainer) Set(key, val string) error { |
| 121 | c.Lock() | 126 | c.Lock() | ... | ... |
| ... | @@ -5,6 +5,7 @@ import ( | ... | @@ -5,6 +5,7 @@ import ( |
| 5 | "io/ioutil" | 5 | "io/ioutil" |
| 6 | "os" | 6 | "os" |
| 7 | "strconv" | 7 | "strconv" |
| 8 | "strings" | ||
| 8 | "sync" | 9 | "sync" |
| 9 | 10 | ||
| 10 | "github.com/beego/x2j" | 11 | "github.com/beego/x2j" |
| ... | @@ -72,6 +73,11 @@ func (c *XMLConfigContainer) String(key string) string { | ... | @@ -72,6 +73,11 @@ func (c *XMLConfigContainer) String(key string) string { |
| 72 | return "" | 73 | return "" |
| 73 | } | 74 | } |
| 74 | 75 | ||
| 76 | // Strings returns the []string value for a given key. | ||
| 77 | func (c *XMLConfigContainer) Strings(key string) []string { | ||
| 78 | return strings.Split(c.String(key), ";") | ||
| 79 | } | ||
| 80 | |||
| 75 | // WriteValue writes a new value for key. | 81 | // WriteValue writes a new value for key. |
| 76 | func (c *XMLConfigContainer) Set(key, val string) error { | 82 | func (c *XMLConfigContainer) Set(key, val string) error { |
| 77 | c.Lock() | 83 | c.Lock() | ... | ... |
| ... | @@ -7,6 +7,7 @@ import ( | ... | @@ -7,6 +7,7 @@ import ( |
| 7 | "io/ioutil" | 7 | "io/ioutil" |
| 8 | "log" | 8 | "log" |
| 9 | "os" | 9 | "os" |
| 10 | "strings" | ||
| 10 | "sync" | 11 | "sync" |
| 11 | 12 | ||
| 12 | "github.com/beego/goyaml2" | 13 | "github.com/beego/goyaml2" |
| ... | @@ -117,6 +118,11 @@ func (c *YAMLConfigContainer) String(key string) string { | ... | @@ -117,6 +118,11 @@ func (c *YAMLConfigContainer) String(key string) string { |
| 117 | return "" | 118 | return "" |
| 118 | } | 119 | } |
| 119 | 120 | ||
| 121 | // Strings returns the []string value for a given key. | ||
| 122 | func (c *YAMLConfigContainer) Strings(key string) []string { | ||
| 123 | return strings.Split(c.String(key), ";") | ||
| 124 | } | ||
| 125 | |||
| 120 | // WriteValue writes a new value for key. | 126 | // WriteValue writes a new value for key. |
| 121 | func (c *YAMLConfigContainer) Set(key, val string) error { | 127 | func (c *YAMLConfigContainer) Set(key, val string) error { |
| 122 | c.Lock() | 128 | c.Lock() | ... | ... |
-
Please register or sign in to post a comment