Skip to content

Commit 0b417c4

Browse files
committed
Process values bound for []byte fields like strings.
fixes #141
1 parent d3cf2cd commit 0b417c4

File tree

7 files changed

+20
-1
lines changed

7 files changed

+20
-1
lines changed

envconfig.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ func processField(value string, field reflect.Value) error {
303303
field.SetFloat(val)
304304
case reflect.Slice:
305305
sl := reflect.MakeSlice(typ, 0, 0)
306-
if len(strings.TrimSpace(value)) != 0 {
306+
if typ.Elem().Kind() == reflect.Uint8 {
307+
sl = reflect.ValueOf([]byte(value))
308+
} else if len(strings.TrimSpace(value)) != 0 {
307309
vals := strings.Split(value, ",")
308310
sl = reflect.MakeSlice(typ, len(vals), len(vals))
309311
for i, val := range vals {

envconfig_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Specification struct {
4545
AdminUsers []string
4646
MagicNumbers []int
4747
EmptyNumbers []int
48+
ByteSlice []byte
4849
ColorCodes map[string]int
4950
MultiWordVar string
5051
MultiWordVarWithAutoSplit uint32 `split_words:"true"`
@@ -96,6 +97,7 @@ func TestProcess(t *testing.T) {
9697
os.Setenv("ENV_CONFIG_ADMINUSERS", "John,Adam,Will")
9798
os.Setenv("ENV_CONFIG_MAGICNUMBERS", "5,10,20")
9899
os.Setenv("ENV_CONFIG_EMPTYNUMBERS", "")
100+
os.Setenv("ENV_CONFIG_BYTESLICE", "this is a test value")
99101
os.Setenv("ENV_CONFIG_COLORCODES", "red:1,green:2,blue:3")
100102
os.Setenv("SERVICE_HOST", "127.0.0.1")
101103
os.Setenv("ENV_CONFIG_TTL", "30")
@@ -152,6 +154,10 @@ func TestProcess(t *testing.T) {
152154
if len(s.EmptyNumbers) != 0 {
153155
t.Errorf("expected %#v, got %#v", []int{}, s.EmptyNumbers)
154156
}
157+
expected := "this is a test value"
158+
if string(s.ByteSlice) != expected {
159+
t.Errorf("expected %v, got %v", expected, string(s.ByteSlice))
160+
}
155161
if s.Ignored != "" {
156162
t.Errorf("expected empty string, got %#v", s.Ignored)
157163
}

testdata/custom.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ ENV_CONFIG_TIMEOUT=
1212
ENV_CONFIG_ADMINUSERS=
1313
ENV_CONFIG_MAGICNUMBERS=
1414
ENV_CONFIG_EMPTYNUMBERS=
15+
ENV_CONFIG_BYTESLICE=
1516
ENV_CONFIG_COLORCODES=
1617
ENV_CONFIG_MULTIWORDVAR=
1718
ENV_CONFIG_MULTI_WORD_VAR_WITH_AUTO_SPLIT=

testdata/default_list.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ ENV_CONFIG_EMPTYNUMBERS
7171
..[type]........Comma-separated.list.of.Integer
7272
..[default].....
7373
..[required]....
74+
ENV_CONFIG_BYTESLICE
75+
..[description].
76+
..[type]........String
77+
..[default].....
78+
..[required]....
7479
ENV_CONFIG_COLORCODES
7580
..[description].
7681
..[type]........Comma-separated.list.of.String:Integer.pairs

testdata/default_table.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ ENV_CONFIG_TIMEOUT...............................Duration.......................
1616
ENV_CONFIG_ADMINUSERS............................Comma-separated.list.of.String....................................................
1717
ENV_CONFIG_MAGICNUMBERS..........................Comma-separated.list.of.Integer...................................................
1818
ENV_CONFIG_EMPTYNUMBERS..........................Comma-separated.list.of.Integer...................................................
19+
ENV_CONFIG_BYTESLICE.............................String............................................................................
1920
ENV_CONFIG_COLORCODES............................Comma-separated.list.of.String:Integer.pairs......................................
2021
ENV_CONFIG_MULTIWORDVAR..........................String............................................................................
2122
ENV_CONFIG_MULTI_WORD_VAR_WITH_AUTO_SPLIT........Unsigned.Integer..................................................................

testdata/fault.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,4 @@
3333
{.Key}
3434
{.Key}
3535
{.Key}
36+
{.Key}

usage.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ func implementsInterface(t reflect.Type) bool {
5858
func toTypeDescription(t reflect.Type) string {
5959
switch t.Kind() {
6060
case reflect.Array, reflect.Slice:
61+
if t.Elem().Kind() == reflect.Uint8 {
62+
return "String"
63+
}
6164
return fmt.Sprintf("Comma-separated list of %s", toTypeDescription(t.Elem()))
6265
case reflect.Map:
6366
return fmt.Sprintf(

0 commit comments

Comments
 (0)