Skip to content

Commit

Permalink
Parse empty string as an empty slice of numbers (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krivak authored and teepark committed May 24, 2019
1 parent 2c13623 commit a52ba04
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
15 changes: 9 additions & 6 deletions envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,15 @@ func processField(value string, field reflect.Value) error {
}
field.SetFloat(val)
case reflect.Slice:
vals := strings.Split(value, ",")
sl := reflect.MakeSlice(typ, len(vals), len(vals))
for i, val := range vals {
err := processField(val, sl.Index(i))
if err != nil {
return err
sl := reflect.MakeSlice(typ, 0, 0)
if len(strings.TrimSpace(value)) != 0 {
vals := strings.Split(value, ",")
sl = reflect.MakeSlice(typ, len(vals), len(vals))
for i, val := range vals {
err := processField(val, sl.Index(i))
if err != nil {
return err
}
}
}
field.Set(sl)
Expand Down
5 changes: 5 additions & 0 deletions envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Specification struct {
Timeout time.Duration
AdminUsers []string
MagicNumbers []int
EmptyNumbers []int
ColorCodes map[string]int
MultiWordVar string
MultiWordVarWithAutoSplit uint32 `split_words:"true"`
Expand Down Expand Up @@ -93,6 +94,7 @@ func TestProcess(t *testing.T) {
os.Setenv("ENV_CONFIG_TIMEOUT", "2m")
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_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 @@ -146,6 +148,9 @@ func TestProcess(t *testing.T) {
s.MagicNumbers[2] != 20 {
t.Errorf("expected %#v, got %#v", []int{5, 10, 20}, s.MagicNumbers)
}
if len(s.EmptyNumbers) != 0 {
t.Errorf("expected %#v, got %#v", []int{}, s.EmptyNumbers)
}
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 @@ -11,6 +11,7 @@ ENV_CONFIG_TTL=
ENV_CONFIG_TIMEOUT=
ENV_CONFIG_ADMINUSERS=
ENV_CONFIG_MAGICNUMBERS=
ENV_CONFIG_EMPTYNUMBERS=
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 @@ -66,6 +66,11 @@ ENV_CONFIG_MAGICNUMBERS
..[type]........Comma-separated.list.of.Integer
..[default].....
..[required]....
ENV_CONFIG_EMPTYNUMBERS
..[description].
..[type]........Comma-separated.list.of.Integer
..[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 @@ -15,6 +15,7 @@ ENV_CONFIG_TTL...................................Unsigned.Integer...............
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_COLORCODES............................Comma-separated.list.of.String:Integer.pairs......................................
ENV_CONFIG_MULTIWORDVAR..........................String............................................................................
ENV_CONFIG_MULTI_WORD_VAR_WITH_AUTO_SPLIT........Unsigned.Integer..................................................................
Expand Down

0 comments on commit a52ba04

Please sign in to comment.