Skip to content

Commit

Permalink
Make new destination slice when length doesn't match.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdg committed Jun 5, 2015
1 parent 056c9bc commit 110f954
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 110f954

Please sign in to comment.