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

BSON marshaling does not know how to handle common Go patterns such as struct composition

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: BSON

      In Go, struct composition is extremely common. Embedding a struct into another struct is the only way we can achieve some level of subclassing and inheritance in the language.

       

      It appears the BSON marshalers are not prepared to handle this at all. In most cases, the fields are just completely ignored. In other cases, it includes a hidden field name.

       

      I expect the bson marshaller to handle structs in the same way as the Golang JSON marshaller. I have created a gist to express the JSON behavior https://gist.github.com/qhenkart/2c4fd696d1450ac266a09d1c62d4f67a

       

      The exact same code with bson tags and the mongo library will be completely ignored by the bson marshaller. The final document that is inserted is simply empty. There are a lot of different variations of struct composition, and the BSON marshaller must know how to handle them.

       

      Additionally, I recently tried extending primitive.ObjectId, to create a workaround on another issue I posted recently. I extended it as follows

      `type ObjectID struct { primitive.ObjectID}`

       

      This allows me to overwrite or add methods to the class and inherit all of the behavior of the primitive.ObjectID. It works great except the BSON marshaller doesn't ignore it in this case. In this case it would insert something along the lines of 

      `id: {objectId: ObjectId("<validobjectid>")}

            Assignee:
            divjot.arora@mongodb.com Divjot Arora (Inactive)
            Reporter:
            qhenkart@gmail.com Quest Henkart
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: