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

Support encoding for nil pointers

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: BSON
    • Labels:
      None

      Encoding a map[string]interface{} contains nil pointer as value or any struct which has nil pointer field causes runtime error.

      type Foo struct {
      	Bar *Bar
      }
      type Bar struct{}
      
      f := Foo{
      	Bar: nil,
      }
      //  causes "*bsoncodec.StructCodec can only process structs, but got a reflect.Value"
      _, err := bson.Marshal(f)
      	
      m := map[string]interface{}{
      	"foo": nil,
      }
      // causes "cannot perform an encoder or decoder lookup on <nil>"
      _, err = bson.Marshal(m)
      

      To address this problem(maybe a bug?), we should treat nil pointer as BSON null when encoding to BSON, in the same way as json.Marshal() does.

            Assignee:
            Unassigned Unassigned
            Reporter:
            jiftechnify takumi fujiwara
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: