Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 0 additions & 88 deletions settingo/main_test.go

This file was deleted.

70 changes: 70 additions & 0 deletions settingo/settingo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package settingo

var SETTINGS = Settings{
msg: make(map[string]string),
VarString: make(map[string]string),
VarInt: make(map[string]int),
VarMap: make(map[string]map[string][]string),
VarSlice: make(map[string][]string),
Parsers: make(map[string]func(string) string),
ParsersInt: make(map[string]func(int) int),
VarBool: make(map[string]bool),
}

func Get(x string) string {
return SETTINGS.Get(x)
}
func Set(flagName, defaultVar, message string) {
SETTINGS.Set(flagName, defaultVar, message)
}

func SetString(flagName, defaultVar, message string) {
SETTINGS.Set(flagName, defaultVar, message)
}

func SetInt(flagName string, defaultVar int, message string) {
SETTINGS.SetInt(flagName, defaultVar, message)
}

func SetBool(flagName string, defaultVar bool, message string) {
SETTINGS.SetBool(flagName, defaultVar, message)
}

func SetMap(flagName string, defaultVar map[string][]string, message string) {
SETTINGS.SetMap(flagName, defaultVar, message)
}

func SetSlice(flagName string, defaultVar []string, message string, sep string) {
SETTINGS.SetSlice(flagName, defaultVar, message, sep)
}

func SetParsed(flagName, defaultVar, message string, parserFunc func(string) string) {
SETTINGS.SetParsed(flagName, defaultVar, message, parserFunc)
}

func SetParsedInt(flagName, defaultVar, message string, parserFunc func(int) int) {
SETTINGS.SetParsedInt(flagName, defaultVar, message, parserFunc)
}

func GetInt(flagName string) int {
return SETTINGS.GetInt(flagName)
}

func GetBool(flagName string) bool {
return SETTINGS.GetBool(flagName)
}

func GetMap(flagName string) map[string][]string {
return SETTINGS.GetMap(flagName)
}

func GetSlice(flagName string) []string {
return SETTINGS.GetSlice(flagName)
}

func Parse() {
SETTINGS.Parse()
}
func ParseTo(to interface{}) {
SETTINGS.ParseTo(to)
}
111 changes: 111 additions & 0 deletions settingo/settingo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package settingo

import (
"os"
"reflect"
"testing"
)

type TestConfig struct {
Foobar string `settingo:"help text for foobar"`
FoobarInt int `settingo:"help text for FoobarInt"`
FoobarBool bool `settingo:"help text for FoobarBool"`
FoobarMap map[string][]string `settingo:"help text FoobarMap"`
}

func Test_struct_types_default(t *testing.T) {

expected := "default_value_for_foobar"
expectedInt := 42
expectedBool := true
expectedMap := make(map[string][]string)

expectedMap["foo"] = []string{"bar"}
expectedMap["foo1"] = []string{"bar1", "bar2"}

config := &TestConfig{
Foobar: expected,
FoobarInt: expectedInt,
FoobarBool: expectedBool,
FoobarMap: expectedMap,
}

SETTINGS.LoadStruct(config)

if config.Foobar != expected {
t.Error(config.Foobar, " != ", expected)
}

if config.FoobarInt != expectedInt {
t.Error(config.FoobarInt, " != ", expectedInt)
}

if config.FoobarBool != expectedBool {
t.Error(config.FoobarBool, " != ", expectedBool)
}

if !reflect.DeepEqual(config.FoobarMap, expectedMap) {
t.Error(config.FoobarMap, " != ", expectedMap)
}

}

type ExampleConfig struct {
Foobar string `settingo:"help text for foobar"`
FoobarInt int `settingo:"help text for FoobarInt"`
FoobarBool bool `settingo:"help text for FoobarBool"`
FoobarMap map[string][]string `settingo:"help text FoobarMap"`
}

func Test_struct_types_os_env(t *testing.T) {

expected := "other value"
os.Setenv("FOOBAR", expected)
defaultStr := "default value"

expectedInt := 44
os.Setenv("FOOBARINT", "44")
defaultInt := 42

os.Setenv("FOOBARBOOL", "y")
expectedBool := true
defaultBool := false

os.Setenv("FOOBARMAP", "foo:bar;foo1:bar1,bar2")
expectedMap := make(map[string][]string)
defaultMap := make(map[string][]string)

expectedMap["foo"] = []string{"bar"}
expectedMap["foo1"] = []string{"bar1", "bar2"}

config := &ExampleConfig{
Foobar: defaultStr,
FoobarInt: defaultInt,
FoobarBool: defaultBool,
FoobarMap: defaultMap,
}

SETTINGS.ParseTo(config)

if config.Foobar != expected {
t.Error(config.Foobar, " != ", expected)
}

if config.FoobarInt != expectedInt {
t.Error(config.FoobarInt, " != ", expectedInt)
}

if config.FoobarBool != expectedBool {
t.Error(config.FoobarBool, " != ", expectedBool)
}

if !reflect.DeepEqual(config.FoobarMap, expectedMap) {
t.Error(config.FoobarMap, " != ", expectedMap)
}

// Cleanup
os.Unsetenv("FOOBAR")
os.Unsetenv("FOOBAR_INT")
os.Unsetenv("FOOBAR_BOOL")
os.Unsetenv("FOOBAR_MAP")
}
Loading