diff --git a/envconfig.go b/envconfig.go index 7700394..3f16108 100644 --- a/envconfig.go +++ b/envconfig.go @@ -303,7 +303,9 @@ func processField(value string, field reflect.Value) error { field.SetFloat(val) case reflect.Slice: sl := reflect.MakeSlice(typ, 0, 0) - if len(strings.TrimSpace(value)) != 0 { + if typ.Elem().Kind() == reflect.Uint8 { + sl = reflect.ValueOf([]byte(value)) + } else if len(strings.TrimSpace(value)) != 0 { vals := strings.Split(value, ",") sl = reflect.MakeSlice(typ, len(vals), len(vals)) for i, val := range vals { diff --git a/envconfig_test.go b/envconfig_test.go index 92cb434..2ca765d 100644 --- a/envconfig_test.go +++ b/envconfig_test.go @@ -45,6 +45,7 @@ type Specification struct { AdminUsers []string MagicNumbers []int EmptyNumbers []int + ByteSlice []byte ColorCodes map[string]int MultiWordVar string MultiWordVarWithAutoSplit uint32 `split_words:"true"` @@ -96,6 +97,7 @@ func TestProcess(t *testing.T) { os.Setenv("ENV_CONFIG_ADMINUSERS", "John,Adam,Will") os.Setenv("ENV_CONFIG_MAGICNUMBERS", "5,10,20") os.Setenv("ENV_CONFIG_EMPTYNUMBERS", "") + os.Setenv("ENV_CONFIG_BYTESLICE", "this is a test value") os.Setenv("ENV_CONFIG_COLORCODES", "red:1,green:2,blue:3") os.Setenv("SERVICE_HOST", "127.0.0.1") os.Setenv("ENV_CONFIG_TTL", "30") @@ -152,6 +154,10 @@ func TestProcess(t *testing.T) { if len(s.EmptyNumbers) != 0 { t.Errorf("expected %#v, got %#v", []int{}, s.EmptyNumbers) } + expected := "this is a test value" + if string(s.ByteSlice) != expected { + t.Errorf("expected %v, got %v", expected, string(s.ByteSlice)) + } if s.Ignored != "" { t.Errorf("expected empty string, got %#v", s.Ignored) } diff --git a/testdata/custom.txt b/testdata/custom.txt index 2174004..04d2f5d 100644 --- a/testdata/custom.txt +++ b/testdata/custom.txt @@ -12,6 +12,7 @@ ENV_CONFIG_TIMEOUT= ENV_CONFIG_ADMINUSERS= ENV_CONFIG_MAGICNUMBERS= ENV_CONFIG_EMPTYNUMBERS= +ENV_CONFIG_BYTESLICE= ENV_CONFIG_COLORCODES= ENV_CONFIG_MULTIWORDVAR= ENV_CONFIG_MULTI_WORD_VAR_WITH_AUTO_SPLIT= diff --git a/testdata/default_list.txt b/testdata/default_list.txt index 49dbea1..fb0eced 100644 --- a/testdata/default_list.txt +++ b/testdata/default_list.txt @@ -71,6 +71,11 @@ ENV_CONFIG_EMPTYNUMBERS ..[type]........Comma-separated.list.of.Integer ..[default]..... ..[required].... +ENV_CONFIG_BYTESLICE +..[description]. +..[type]........String +..[default]..... +..[required].... ENV_CONFIG_COLORCODES ..[description]. ..[type]........Comma-separated.list.of.String:Integer.pairs diff --git a/testdata/default_table.txt b/testdata/default_table.txt index 9d23d3f..65c9b44 100644 --- a/testdata/default_table.txt +++ b/testdata/default_table.txt @@ -16,6 +16,7 @@ ENV_CONFIG_TIMEOUT...............................Duration....................... ENV_CONFIG_ADMINUSERS............................Comma-separated.list.of.String.................................................... ENV_CONFIG_MAGICNUMBERS..........................Comma-separated.list.of.Integer................................................... ENV_CONFIG_EMPTYNUMBERS..........................Comma-separated.list.of.Integer................................................... +ENV_CONFIG_BYTESLICE.............................String............................................................................ ENV_CONFIG_COLORCODES............................Comma-separated.list.of.String:Integer.pairs...................................... ENV_CONFIG_MULTIWORDVAR..........................String............................................................................ ENV_CONFIG_MULTI_WORD_VAR_WITH_AUTO_SPLIT........Unsigned.Integer.................................................................. diff --git a/testdata/fault.txt b/testdata/fault.txt index b33f137..b525ff1 100644 --- a/testdata/fault.txt +++ b/testdata/fault.txt @@ -33,3 +33,4 @@ {.Key} {.Key} {.Key} +{.Key} diff --git a/usage.go b/usage.go index 92d713b..1e6d0a8 100644 --- a/usage.go +++ b/usage.go @@ -58,6 +58,9 @@ func implementsInterface(t reflect.Type) bool { func toTypeDescription(t reflect.Type) string { switch t.Kind() { case reflect.Array, reflect.Slice: + if t.Elem().Kind() == reflect.Uint8 { + return "String" + } return fmt.Sprintf("Comma-separated list of %s", toTypeDescription(t.Elem())) case reflect.Map: return fmt.Sprintf(