Something went wrong. Try again.
initialize struct from env vars (golang)
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129package envstruct
import ( "fmt" "net" "reflect" "testing")
func TestConvertCase(t *testing.T) { const prefix = "MY_APP_" var tests = []struct { input string expected string }{ {"myFunEnvVar", "MY_FUN_ENV_VAR"}, {"MyFunEnvVar", "MY_FUN_ENV_VAR"}, {"writeToHTML", "WRITE_TO_HTML"}, {"IP", "IP"}, }
for _, tt := range tests { t.Run(fmt.Sprintf("%s without prefix", tt.input), func(t *testing.T) { output := convertCase("", tt.input) if output != tt.expected { t.Errorf("got %s, expected %s", output, tt.expected) } }) t.Run(fmt.Sprintf("%s with prefix", tt.input), func(t *testing.T) { output := convertCase(prefix, tt.input) expected := prefix + tt.expected if output != expected { t.Errorf("got %s, expected %s", output, expected) } }) }}
func TestParse(t *testing.T) { type Embedded struct { Field1 string `envDefault:"default"` } type logOpts struct { File string `envDefault:"./log"` UseJSON bool } type testStruct struct { Port uint BufSize uint Log logOpts Custom string `envVar:"MY_VAR" envNoPrefix:"true" envDefault:""` IP net.IP Embedded } type inputs struct { port string bufsize string logfile string useJSON string custom string ip string embed string }
var tests = []struct { name string values inputs expected testStruct errExpected bool }{ { "simple", inputs{"443", "64", "~/.cache/my_app/log", "0", "fun", "127.0.0.1", "value"}, testStruct{443, 64, logOpts{"~/.cache/my_app/log", false}, "fun", net.IPv4(127, 0, 0, 1), Embedded{"value"}}, false, }, { "use default value", inputs{"443", "64", "", "false", "fun", "127.0.0.1", ""}, testStruct{443, 64, logOpts{"./log", false}, "fun", net.IPv4(127, 0, 0, 1), Embedded{"default"}}, false, }, { "incorrect int", inputs{"44.3", "64", "~/.cache/my_app/log", "false", "fun", "127.0.0.1", ""}, testStruct{443, 64, logOpts{"~/.cache/my_app/log", false}, "fun", net.IPv4(127, 0, 0, 1), Embedded{""}}, true, }, { "missing default empty string value", inputs{"443", "64", "~/.cache/my_app/log", "false", "", "127.0.0.1", "value"}, testStruct{443, 64, logOpts{"~/.cache/my_app/log", false}, "", net.IPv4(127, 0, 0, 1), Embedded{"value"}}, false, }, { "test UnmarshalText failure", inputs{"443", "64", "~/.cache/my_app/log", "false", "fun", "not an ip", ""}, testStruct{443, 64, logOpts{"~/.cache/my_app/log", false}, "fun", net.IPv4(127, 0, 0, 1), Embedded{""}}, true, }, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var s testStruct
t.Setenv("PREFIX_PORT", tt.values.port) t.Setenv("PREFIX_BUF_SIZE", tt.values.bufsize) if tt.values.logfile != "" { t.Setenv("PREFIX_LOG_FILE", tt.values.logfile) } t.Setenv("PREFIX_LOG_USE_JSON", tt.values.useJSON) t.Setenv("MY_VAR", tt.values.custom) t.Setenv("PREFIX_IP", tt.values.ip) t.Setenv("PREFIX_FIELD1", tt.values.embed)
err := Parse("prefix", &s) t.Log(s) if err != nil && !tt.errExpected { t.Errorf("got parse error: %s", err) } else if err == nil && tt.errExpected { t.Error("expected parse error") } else if !tt.errExpected && !reflect.DeepEqual(s, tt.expected) { t.Errorf("got %v, expected %v", s, tt.expected) } }) }}