Skip to content

Commit 0a182da

Browse files
authored
Merge pull request #3 from Attumm/ide-friendly-interface
Initial change for ide friendly interface
2 parents 7197253 + 871ecc7 commit 0a182da

File tree

6 files changed

+274
-150
lines changed

6 files changed

+274
-150
lines changed

settingo/main_test.go

Lines changed: 0 additions & 88 deletions
This file was deleted.

settingo/settingo.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package settingo
2+
3+
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),
12+
}
13+
14+
func Get(x string) string {
15+
return SETTINGS.Get(x)
16+
}
17+
func Set(flagName, defaultVar, message string) {
18+
SETTINGS.Set(flagName, defaultVar, message)
19+
}
20+
21+
func SetString(flagName, defaultVar, message string) {
22+
SETTINGS.Set(flagName, defaultVar, message)
23+
}
24+
25+
func SetInt(flagName string, defaultVar int, message string) {
26+
SETTINGS.SetInt(flagName, defaultVar, message)
27+
}
28+
29+
func SetBool(flagName string, defaultVar bool, message string) {
30+
SETTINGS.SetBool(flagName, defaultVar, message)
31+
}
32+
33+
func SetMap(flagName string, defaultVar map[string][]string, message string) {
34+
SETTINGS.SetMap(flagName, defaultVar, message)
35+
}
36+
37+
func SetSlice(flagName string, defaultVar []string, message string, sep string) {
38+
SETTINGS.SetSlice(flagName, defaultVar, message, sep)
39+
}
40+
41+
func SetParsed(flagName, defaultVar, message string, parserFunc func(string) string) {
42+
SETTINGS.SetParsed(flagName, defaultVar, message, parserFunc)
43+
}
44+
45+
func SetParsedInt(flagName, defaultVar, message string, parserFunc func(int) int) {
46+
SETTINGS.SetParsedInt(flagName, defaultVar, message, parserFunc)
47+
}
48+
49+
func GetInt(flagName string) int {
50+
return SETTINGS.GetInt(flagName)
51+
}
52+
53+
func GetBool(flagName string) bool {
54+
return SETTINGS.GetBool(flagName)
55+
}
56+
57+
func GetMap(flagName string) map[string][]string {
58+
return SETTINGS.GetMap(flagName)
59+
}
60+
61+
func GetSlice(flagName string) []string {
62+
return SETTINGS.GetSlice(flagName)
63+
}
64+
65+
func Parse() {
66+
SETTINGS.Parse()
67+
}
68+
func ParseTo(to interface{}) {
69+
SETTINGS.ParseTo(to)
70+
}

settingo/settingo_test.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package settingo
2+
3+
import (
4+
"os"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
type TestConfig struct {
10+
Foobar string `settingo:"help text for foobar"`
11+
FoobarInt int `settingo:"help text for FoobarInt"`
12+
FoobarBool bool `settingo:"help text for FoobarBool"`
13+
FoobarMap map[string][]string `settingo:"help text FoobarMap"`
14+
}
15+
16+
func Test_struct_types_default(t *testing.T) {
17+
18+
expected := "default_value_for_foobar"
19+
expectedInt := 42
20+
expectedBool := true
21+
expectedMap := make(map[string][]string)
22+
23+
expectedMap["foo"] = []string{"bar"}
24+
expectedMap["foo1"] = []string{"bar1", "bar2"}
25+
26+
config := &TestConfig{
27+
Foobar: expected,
28+
FoobarInt: expectedInt,
29+
FoobarBool: expectedBool,
30+
FoobarMap: expectedMap,
31+
}
32+
33+
SETTINGS.LoadStruct(config)
34+
35+
if config.Foobar != expected {
36+
t.Error(config.Foobar, " != ", expected)
37+
}
38+
39+
if config.FoobarInt != expectedInt {
40+
t.Error(config.FoobarInt, " != ", expectedInt)
41+
}
42+
43+
if config.FoobarBool != expectedBool {
44+
t.Error(config.FoobarBool, " != ", expectedBool)
45+
}
46+
47+
if !reflect.DeepEqual(config.FoobarMap, expectedMap) {
48+
t.Error(config.FoobarMap, " != ", expectedMap)
49+
}
50+
51+
}
52+
53+
type ExampleConfig struct {
54+
Foobar string `settingo:"help text for foobar"`
55+
FoobarInt int `settingo:"help text for FoobarInt"`
56+
FoobarBool bool `settingo:"help text for FoobarBool"`
57+
FoobarMap map[string][]string `settingo:"help text FoobarMap"`
58+
}
59+
60+
func Test_struct_types_os_env(t *testing.T) {
61+
62+
expected := "other value"
63+
os.Setenv("FOOBAR", expected)
64+
defaultStr := "default value"
65+
66+
expectedInt := 44
67+
os.Setenv("FOOBARINT", "44")
68+
defaultInt := 42
69+
70+
os.Setenv("FOOBARBOOL", "y")
71+
expectedBool := true
72+
defaultBool := false
73+
74+
os.Setenv("FOOBARMAP", "foo:bar;foo1:bar1,bar2")
75+
expectedMap := make(map[string][]string)
76+
defaultMap := make(map[string][]string)
77+
78+
expectedMap["foo"] = []string{"bar"}
79+
expectedMap["foo1"] = []string{"bar1", "bar2"}
80+
81+
config := &ExampleConfig{
82+
Foobar: defaultStr,
83+
FoobarInt: defaultInt,
84+
FoobarBool: defaultBool,
85+
FoobarMap: defaultMap,
86+
}
87+
88+
SETTINGS.ParseTo(config)
89+
90+
if config.Foobar != expected {
91+
t.Error(config.Foobar, " != ", expected)
92+
}
93+
94+
if config.FoobarInt != expectedInt {
95+
t.Error(config.FoobarInt, " != ", expectedInt)
96+
}
97+
98+
if config.FoobarBool != expectedBool {
99+
t.Error(config.FoobarBool, " != ", expectedBool)
100+
}
101+
102+
if !reflect.DeepEqual(config.FoobarMap, expectedMap) {
103+
t.Error(config.FoobarMap, " != ", expectedMap)
104+
}
105+
106+
// Cleanup
107+
os.Unsetenv("FOOBAR")
108+
os.Unsetenv("FOOBAR_INT")
109+
os.Unsetenv("FOOBAR_BOOL")
110+
os.Unsetenv("FOOBAR_MAP")
111+
}

0 commit comments

Comments
 (0)