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

Cannot handle nil pointer to struct with Marshaler or ValueMarshaler implemented

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 1.1.0
    • BSON
    • None

    Description

      Considering the following example:

      type A struct {
      	X *B
      }
       
      type B struct {
      	Y string
      }
       
      func (B) MarshalBSON() ([]byte, error) {
      	return bson.Marshal(bson.M{"foo": "bar"})
      }
      

      In that case it can marshal A{X: &B} but cannot marshal A{X: nil}

      If *B (instead of B) has MarshalBSON implemented it can handle nil pointer. but has no way to represent null in BSON, because MarshalBSON is defaulted to EmbeddedDocument.
      I don't know what should return if we implement MarshalBSONValue in *B. "return bsontype.Null, nil, nil" doesn't seem to be correct.

      Attachments

        Activity

          People

            manny.eppinger@mongodb.com Emmanuel Eppinger (Inactive)
            mwei Yao Wei
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: