diff --git a/decode.go b/decode.go index 6c7d398b..8e76f02e 100644 --- a/decode.go +++ b/decode.go @@ -308,7 +308,7 @@ func (md *MetaData) unifySlice(data interface{}, rv reflect.Value) error { return badtype("slice", data) } sliceLen := datav.Len() - if rv.IsNil() { + if rv.IsNil() || rv.Len() != datav.Len() { rv.Set(reflect.MakeSlice(rv.Type(), sliceLen, sliceLen)) } return md.unifySliceArray(datav, rv) diff --git a/decode_test.go b/decode_test.go index 3805931f..019c23d5 100644 --- a/decode_test.go +++ b/decode_test.go @@ -468,6 +468,15 @@ type ingredient struct { Name string } +func TestDecodeToEmptySlice(t *testing.T) { + s := struct{ Test []string }{Test: []string{}} + + _, err := Decode(`Test = ["test"]`, &s) + if err != nil { + t.Fatal(err) + } +} + func ExampleMetaData_PrimitiveDecode() { var md MetaData var err error