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

Mongo shipped BSON marshaller to support nested structs using embedding

    • Type: Icon: Improvement Improvement
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • 0.0.3
    • Affects Version/s: 0.0.2
    • Component/s: BSON
    • None
    • Environment:
      Linux
      Fedora

      package main_test
      
      import (
      	"encoding/json"
      	"log"
      	"testing"
      
      	"github.com/mongodb/mongo-go-driver/bson"
      	"github.com/stretchr/testify/assert"
      	bsonmgo "gopkg.in/mgo.v2/bson"
      )
      
      // SomeInterface -
      type SomeInterface interface {
      }
      
      // SomeStruct -
      type SomeStruct struct {
      	SomeInterface
      }
      
      // SomeParentStruct -
      type SomeParentStruct struct {
      	Name  string        `bson:"name"`
      	Child SomeInterface `bson:"interface"`
      }
      
      // Validate that we can JSON Marshal the structure
      func TestJSON(t *testing.T) {
      	st := SomeStruct{}
      	structExample := SomeParentStruct{
      		Child: st,
      	}
      	doc, err := json.Marshal(structExample)
      	assert.Equal(t, err, nil, "JSON")
      	log.Printf("%s", doc)
      }
      
      // Validate that we can BSON Marshal the structure with MGO lib
      func TestMgoBSON(t *testing.T) {
      	st := SomeStruct{}
      	structExample := SomeParentStruct{
      		Child: st,
      	}
      	doc, err := bsonmgo.Marshal(structExample)
      	assert.Equal(t, err, nil, "JSON")
      	log.Printf("%s", doc)
      }
      
      // Validate that we cannot BSON Marshal the structure with official mongo lib
      func TestMongoBSON(t *testing.T) {
      	st := SomeStruct{}
      	structExample := SomeParentStruct{
      		Child: st,
      	}
      	doc, err := bson.Marshal(structExample)
      	assert.Equal(t, err, nil, "JSON")
      	log.Printf("%s", doc)
      }
      
      

            Assignee:
            kris.brandow@mongodb.com Kristofer Brandow (Inactive)
            Reporter:
            runonautomation Volodymyr
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: