Skip to content

Commit

Permalink
Process values bound for []byte fields like strings.
Browse files Browse the repository at this point in the history
fixes #141
  • Loading branch information
teepark committed May 24, 2019
1 parent d3cf2cd commit 0b417c4
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions testdata/custom.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
5 changes: 5 additions & 0 deletions testdata/default_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions testdata/default_table.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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..................................................................
Expand Down
1 change: 1 addition & 0 deletions testdata/fault.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@
{.Key}
{.Key}
{.Key}
{.Key}
3 changes: 3 additions & 0 deletions usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

2 comments on commit 0b417c4

@nashikb
Copy link

@nashikb nashikb commented on 0b417c4 Mar 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@teepark in da house! I just realized I've been relying on this specific functionality. \o/

@teepark
Copy link
Collaborator Author

@teepark teepark commented on 0b417c4 Mar 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nashikb hey KB! Glad to hear it. I used to maintain this library :)

Please sign in to comment.