Skip to content

Commit 7367a26

Browse files
authored
Merge pull request #6 from Attumm/added-support-for-slice-sep
Added support for slice sep
2 parents 055a587 + 73cc354 commit 7367a26

File tree

4 files changed

+83
-48
lines changed

4 files changed

+83
-48
lines changed

settingo/settingo.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package settingo
22

33
var SETTINGS = Settings{
4-
msg: make(map[string]string),
5-
VarString: make(map[string]string),
6-
VarInt: make(map[string]int),
7-
VarMap: make(map[string]map[string][]string),
8-
VarSlice: make(map[string][]string),
9-
Parsers: make(map[string]func(string) string),
10-
ParsersInt: make(map[string]func(int) int),
11-
VarBool: make(map[string]bool),
4+
msg: make(map[string]string),
5+
VarString: make(map[string]string),
6+
VarInt: make(map[string]int),
7+
VarMap: make(map[string]map[string][]string),
8+
VarSlice: make(map[string][]string),
9+
VarSliceSep: make(map[string]string),
10+
Parsers: make(map[string]func(string) string),
11+
ParsersInt: make(map[string]func(int) int),
12+
VarBool: make(map[string]bool),
1213
}
1314

1415
func Get(x string) string {

settingo/settingo_test.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88
)
99

1010
type ExampleConfig struct {
11-
Foobar string `settingo:"help text for foobar"`
12-
FoobarInt int `settingo:"help text for FoobarInt"`
13-
FoobarBool bool `settingo:"help text for FoobarBool"`
14-
FoobarMap map[string][]string `settingo:"help text FoobarMap"`
15-
FoobarSlice []string `settingo:"help text for FoobarSlice"`
16-
FooParse string `settingo:"help text for FooParse"`
17-
FooParseInt int `settingo:"help text for FooParseInt"`
11+
Foobar string `settingo:"help text for foobar"`
12+
FoobarInt int `settingo:"help text for FoobarInt"`
13+
FoobarBool bool `settingo:"help text for FoobarBool"`
14+
FoobarMap map[string][]string `settingo:"help text FoobarMap"`
15+
FoobarSlice []string `settingo:"help text for FoobarSlice"`
16+
FoobarSliceSep []string `settingo:"sep=; help text for FoobarSliceSep"`
17+
FooParse string `settingo:"help text for FooParse"`
18+
FooParseInt int `settingo:"help text for FooParseInt"`
1819
}
1920

2021
func Test_struct_types_os_env(t *testing.T) {
@@ -41,6 +42,10 @@ func Test_struct_types_os_env(t *testing.T) {
4142
expectedSlice := []string{"item1", "item2", "item3"}
4243
defaultSlice := []string{}
4344

45+
os.Setenv("FOOBARSLICESEP", "itemA;itemB;itemC")
46+
expectedSliceSep := []string{"itemA", "itemB", "itemC"}
47+
defaultSliceSep := []string{"default"}
48+
4449
os.Setenv("FOOPARSE", "postgres://user:[email protected]:5432/mydb")
4550
expectedFooParse := "database.example.com"
4651
defaultFooParse := "foobar"
@@ -59,14 +64,16 @@ func Test_struct_types_os_env(t *testing.T) {
5964
SetBool("FOOBARBOOL", defaultBool, "help text for FoobarBool")
6065
SetMap("FOOBARMAP", defaultMap, "help text FoobarMap")
6166
SetSlice("FOOBARSLICE", defaultSlice, "help text for FoobarSlice", ",")
67+
SetSlice("FOOBARSLICESEP", defaultSliceSep, "help text for FoobarSliceSep", ";")
6268

6369
config := &ExampleConfig{
64-
Foobar: defaultStr,
65-
FoobarInt: defaultInt,
66-
FoobarBool: defaultBool,
67-
FoobarMap: defaultMap,
68-
FoobarSlice: defaultSlice,
69-
FooParse: defaultFooParse,
70+
Foobar: defaultStr,
71+
FoobarInt: defaultInt,
72+
FoobarBool: defaultBool,
73+
FoobarMap: defaultMap,
74+
FoobarSlice: defaultSlice,
75+
FoobarSliceSep: defaultSliceSep,
76+
FooParse: defaultFooParse,
7077
}
7178

7279
ParseTo(config)
@@ -82,6 +89,9 @@ func Test_struct_types_os_env(t *testing.T) {
8289
if GetBool("FOOBARBOOL") != expectedBool {
8390
t.Error(GetBool("FOOBARBOOL"), " != ", expectedBool)
8491
}
92+
if !reflect.DeepEqual(GetSlice("FOOBARSLICESEP"), expectedSliceSep) {
93+
t.Error(GetSlice("FOOBARSLICESEP"), " != ", expectedSliceSep)
94+
}
8595

8696
if !reflect.DeepEqual(GetMap("FOOBARMAP"), expectedMap) {
8797
t.Error(GetMap("FOOBARMAP"), " != ", expectedMap)
@@ -111,6 +121,10 @@ func Test_struct_types_os_env(t *testing.T) {
111121
t.Error(config.FoobarSlice, " != ", expectedSlice)
112122
}
113123

124+
if !reflect.DeepEqual(config.FoobarSliceSep, expectedSliceSep) {
125+
t.Error(config.FoobarSliceSep, " != ", expectedSliceSep)
126+
}
127+
114128
if config.FooParse != expectedFooParse {
115129
t.Error(config.FooParse, " != ", expectedFooParse)
116130
}

settingo/settings.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ func truthiness(s string) bool {
1818
}
1919

2020
type Settings struct {
21-
msg map[string]string
22-
VarString map[string]string
23-
VarInt map[string]int
24-
VarBool map[string]bool
25-
VarMap map[string]map[string][]string
26-
VarSlice map[string][]string
27-
Parsers map[string]func(string) string
28-
ParsersInt map[string]func(int) int
21+
msg map[string]string
22+
VarString map[string]string
23+
VarInt map[string]int
24+
VarBool map[string]bool
25+
VarMap map[string]map[string][]string
26+
VarSlice map[string][]string
27+
VarSliceSep map[string]string
28+
Parsers map[string]func(string) string
29+
ParsersInt map[string]func(int) int
2930
}
3031

3132
func (s *Settings) Set(flagName, defaultVar, message string) {
@@ -58,6 +59,7 @@ func (s *Settings) SetSlice(flagName string, defaultVar []string, message string
5859
}
5960
s.msg[flagName] = message
6061
s.VarSlice[flagName] = defaultVar
62+
s.VarSliceSep[flagName] = sep
6163
}
6264

6365
func (s *Settings) SetParsed(flagName, defaultVar, message string, parserFunc func(string) string) {
@@ -113,10 +115,10 @@ func (s *Settings) HandleCMDLineInput() {
113115
var newV = flag.String(key, ParseMapToLine(val), s.msg[key])
114116
parsedBool[key] = newV
115117
}
116-
parsedList := make(map[string]*string)
118+
parsedSlice := make(map[string]*string)
117119
for key, val := range s.VarSlice {
118-
var newV = flag.String(key, strings.Join(val, ","), s.msg[key])
119-
parsedList[key] = newV
120+
var newV = flag.String(key, strings.Join(val, s.VarSliceSep[key]), s.msg[key])
121+
parsedSlice[key] = newV
120122
}
121123
flag.Parse()
122124

@@ -141,8 +143,8 @@ func (s *Settings) HandleCMDLineInput() {
141143
s.VarMap[key] = ParseLineToMap(*val)
142144
}
143145

144-
for key, val := range parsedList {
145-
s.VarSlice[key] = strings.Split(*val, ",")
146+
for key, val := range parsedSlice {
147+
s.VarSlice[key] = strings.Split(*val, s.VarSliceSep[key])
146148
}
147149
}
148150

@@ -177,7 +179,7 @@ func (s *Settings) HandleOSInput() {
177179
for key := range s.VarSlice {
178180
varEnv, found := os.LookupEnv(key)
179181
if found {
180-
s.VarSlice[key] = strings.Split(varEnv, ",")
182+
s.VarSlice[key] = strings.Split(varEnv, s.VarSliceSep[key])
181183
}
182184
}
183185
}
@@ -220,7 +222,7 @@ func (s *Settings) LoadStruct(cfg interface{}) {
220222
for i := 0; i < value.Len(); i++ {
221223
slice[i] = value.Index(i).String()
222224
}
223-
s.SetSlice(name, slice, help, "")
225+
s.SetSlice(name, slice, help, s.VarSliceSep[name])
224226
}
225227
case reflect.Map:
226228
if value.Type().Key().Kind() == reflect.String &&

settingo/settings_test.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import (
88
)
99

1010
type TestConfig struct {
11-
Foobar string `settingo:"help text for foobar"`
12-
FoobarInt int `settingo:"help text for FoobarInt"`
13-
FoobarBool bool `settingo:"help text for FoobarBool"`
14-
FoobarMap map[string][]string `settingo:"help text FoobarMap"`
15-
FoobarSlice []string `settingo:"help text for FoobarSlice"`
16-
FooParse string `settingo:"help text for FooParse"`
11+
Foobar string `settingo:"help text for foobar"`
12+
FoobarInt int `settingo:"help text for FoobarInt"`
13+
FoobarBool bool `settingo:"help text for FoobarBool"`
14+
FoobarMap map[string][]string `settingo:"help text FoobarMap"`
15+
FoobarSlice []string `settingo:"help text for FoobarSlice"`
16+
FoobarSliceSep []string `settingo:"sep=; help text for FoobarSliceSep"`
17+
FoobarSliceEmptySep []string `settingo:"help text for FoobarSliceEmptySep"`
18+
FooParse string `settingo:"help text for FooParse"`
1719
}
1820

1921
func Test_struct_types_default(t *testing.T) {
@@ -29,6 +31,12 @@ func Test_struct_types_default(t *testing.T) {
2931
os.Setenv("FOOBARSLICE", "item1,item2,item3")
3032
expectedSlice := []string{"item1", "item2", "item3"}
3133

34+
os.Setenv("FOOBARSLICESEP", "itemA;itemB;itemC")
35+
expectedSliceSep := []string{"itemA", "itemB", "itemC"}
36+
37+
os.Setenv("FOOBARSLICEEMPTYSEP", "valA,valB,valC")
38+
expectedSliceEmptySep := []string{"valA", "valB", "valC"}
39+
3240
os.Setenv("FOOPARSE", "postgres://user:[email protected]:5432/mydb")
3341
expectedFooParse := "database.example.com"
3442

@@ -42,12 +50,14 @@ func Test_struct_types_default(t *testing.T) {
4250
})
4351

4452
config := &TestConfig{
45-
Foobar: expected,
46-
FoobarInt: expectedInt,
47-
FoobarBool: expectedBool,
48-
FoobarMap: expectedMap,
49-
FoobarSlice: expectedSlice,
50-
FooParse: expectedFooParse,
53+
Foobar: expected,
54+
FoobarInt: expectedInt,
55+
FoobarBool: expectedBool,
56+
FoobarMap: expectedMap,
57+
FoobarSlice: expectedSlice,
58+
FoobarSliceSep: expectedSliceSep,
59+
FoobarSliceEmptySep: expectedSliceEmptySep,
60+
FooParse: expectedFooParse,
5161
}
5262

5363
SETTINGS.LoadStruct(config)
@@ -72,6 +82,14 @@ func Test_struct_types_default(t *testing.T) {
7282
t.Error(config.FoobarSlice, " != ", expectedSlice)
7383
}
7484

85+
if !reflect.DeepEqual(config.FoobarSliceSep, expectedSliceSep) {
86+
t.Error(config.FoobarSliceSep, " != ", expectedSliceSep)
87+
}
88+
89+
if !reflect.DeepEqual(config.FoobarSliceEmptySep, expectedSliceEmptySep) {
90+
t.Error(config.FoobarSliceEmptySep, " != ", expectedSliceEmptySep)
91+
}
92+
7593
if config.FooParse != expectedFooParse {
7694
t.Error(config.FooParse, " != ", expectedFooParse)
7795
}

0 commit comments

Comments
 (0)