Skip to content

Commit

Permalink
fix empty slice bug (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbsmith7741 authored Aug 20, 2021
1 parent 89eae26 commit 9fa9ae5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,9 @@ func SetField(value reflect.Value, s string, sField reflect.StructField) error {
case reflect.Slice:
// create a generate slice and recursively assign the elements
baseType := reflect.TypeOf(value.Interface()).Elem()
if s == "" { // ignore empty slices
return nil
}
data := strings.Split(s, separator)
slice := reflect.MakeSlice(value.Type(), 0, len(data))
for _, v := range data {
Expand Down
4 changes: 4 additions & 0 deletions unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ func TestUnmarshal(t *testing.T) {
IntsP: []*int{trial.IntP(1), trial.IntP(2), nil, trial.IntP(3)},
},
},
"empty slice": {
Input: "?Ints=&Strings=",
Expected: &testStruct{},
},
"alias type (dessert)": {
Input: "?Dessert=brownie",
Expected: &testStruct{Dessert: brownie},
Expand Down

0 comments on commit 9fa9ae5

Please sign in to comment.