[GODRIVER-315] Mongo shipped BSON marshaller to support nested structs using embedding Created: 27/Mar/18  Updated: 13/Apr/18  Resolved: 13/Apr/18

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: 0.0.2
Fix Version/s: 0.0.3

Type: Improvement Priority: Major - P3
Reporter: Volodymyr Assignee: Kristofer Brandow (Inactive)
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Linux
Fedora


Issue Links:
Depends
depends on GODRIVER-317 Handle nil properties of a struct in ... Closed
Duplicate
duplicates GODRIVER-317 Handle nil properties of a struct in ... Closed

 Description   

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



 Comments   
Comment by Jeffrey Yemin [ 13/Apr/18 ]

Hi runonautomation

Closing this as a duplicate of GODRIVER-317. Let us know if that doesn't fix the issue for you.

Comment by Kristofer Brandow (Inactive) [ 05/Apr/18 ]

Hi Volodymyr,

We've merged a commit that should fix this. Can you please confirm?

--Kris

Comment by Githook User [ 05/Apr/18 ]

Author:

{'email': 'kris@mongodb.com', 'name': 'Kris Brandow', 'username': 'skriptble'}

Message: Handle nil properly in BSON encoder

Added support for nil interfaces, maps, pointers, and slices.
Added support for round tripping empty maps and slices and nil maps and
slices. Since these are distinct in Go, we use BSON Null for a nil map
or slice and an empty document or array, respectively, for zero value
map and slice types.

GODRIVER-317
GODRIVER-315

Change-Id: I96036138acb745b1f42d3190e9664366bb85a9a6
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/bf482478e9fde8a815ce22eec54a36c0ab410502

Comment by Kristofer Brandow (Inactive) [ 27/Mar/18 ]

I created GODRIVER-317 to fix this issue.

Comment by Kristofer Brandow (Inactive) [ 27/Mar/18 ]

Hi Volodymyr,

It looks like we aren't properly handling the case where a struct property is an interface and is nil. This is something we should definitely be handling, I will add a ticket for this. Thanks for the bug report!

--Kris

Comment by Volodymyr [ 27/Mar/18 ]

Hi guys,
Please review if this is a valid case. If not - please feel free to close the issue.
Thank you in advance!

Generated at Thu Feb 08 08:33:57 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.