Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-1003

bson handling of type declaration should be more clear

    XMLWordPrintableJSON

Details

    • Icon: Task Task
    • Resolution: Works as Designed
    • Icon: Major - P3 Major - P3
    • None
    • None
    • BSON

    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")
      }
      

      Attachments

        Activity

          People

            divjot.arora@mongodb.com Divjot Arora (Inactive)
            sam.kleinman Sam Kleinman (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: