Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make new destination slice when length doesn't match. #84

Merged
merged 1 commit into from
Feb 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead you might want to only do this if rv.Cap() < datav.Len() and otherwise rv.SetLen(datav.Len()). But it's an optimization that probably doesn't matter for most TOML use cases.

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