From 9fa9ae56a75d218332140e7c0771a02155657aae Mon Sep 17 00:00:00 2001 From: Joshua Smith Date: Fri, 20 Aug 2021 08:45:38 -0600 Subject: [PATCH] fix empty slice bug (#23) --- unmarshal.go | 3 +++ unmarshal_test.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/unmarshal.go b/unmarshal.go index dd80dfd..5ad9b2c 100644 --- a/unmarshal.go +++ b/unmarshal.go @@ -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 { diff --git a/unmarshal_test.go b/unmarshal_test.go index 5d358c6..c3315af 100644 --- a/unmarshal_test.go +++ b/unmarshal_test.go @@ -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},