[GODRIVER-1003] bson handling of type declaration should be more clear Created: 26/Apr/19  Updated: 27/Oct/23  Resolved: 23/Jul/20

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Sam Kleinman (Inactive) Assignee: Divjot Arora (Inactive)
Resolution: Works as Designed Votes: 0
Labels: evg, mgocompat
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I wrote the following test which reflects a problem that we ran into during our migration from mgobson. It was easy to amend our code to avoid this situation, but the error message that you get from Unmarshal in this situation is "cannot decode binary into a slice", which isn't super intuitive, particularly without context, and because the "First" field is what a developer would think of as a slice.

func TestBinarySlice(t *testing.T) {
	type raw []byte
	type fob struct {
		First raw `bson:"first"`
	}
 
	inter := M{
		"first": []byte("foo"),
	}
 
	data, err := Marshal(inter)
	require.NoError(t, err)
 
	out := fob{}
 
	err = Unmarshal(data, &out)
	require.NoError(t, err)
 
	require.Equal(t, string(out.First), "foo")
}



 Comments   
Comment by Divjot Arora (Inactive) [ 23/Jul/20 ]

I can't repro this against release/1.4 or release/1.3, so I'm closing out this ticket.

Generated at Thu Feb 08 08:35:27 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.