From e9bc25f5f71fbf8b4bd4e149f1f7eda12f37680a Mon Sep 17 00:00:00 2001 From: jprgic Date: Tue, 7 Mar 2023 12:22:06 +0100 Subject: [PATCH] add support for backward compatible deserialization on mongo driver Changed config to deserialize int32 to int when output is interface{}. This was done to resolve issues listed in: https://3.basecamp.com/3077084/buckets/10103289/messages/5732789325%5C\#__recording_5864997976 . Changed config to deserialize bson.array to []interface{} instead of bson.D. This was done to resolve issues in https://3.basecamp.com/3077084/buckets/10103289/messages/5732789325/\#__recording_5890369631 . Changes remain consistent with old driver. --- pkg/mdb2/mdb.go | 7 +++++- pkg/mdb2/mdb_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/pkg/mdb2/mdb.go b/pkg/mdb2/mdb.go index 63e225a..0605ab6 100644 --- a/pkg/mdb2/mdb.go +++ b/pkg/mdb2/mdb.go @@ -194,7 +194,12 @@ func (mdb *Mdb) Init(connStr string, opts ...func(db *Mdb)) error { // this code matches mgo behavior and allows to decode interface{} as JSON // https://jira.mongodb.org/browse/GODRIVER-988 tM := reflect.TypeOf(bson.M{}) - reg := bson.NewRegistryBuilder().RegisterTypeMapEntry(bsontype.EmbeddedDocument, tM).Build() + reg := bson.NewRegistryBuilder().RegisterTypeMapEntry(bsontype.EmbeddedDocument, tM). + // Read int32 as int (when deserializing into interface{}) for backward compatibility + RegisterTypeMapEntry(bsontype.Int32, reflect.TypeOf(int(0))). + // Read bson.Array as []interface{} for backward compatibility + RegisterTypeMapEntry(bsontype.Array, reflect.TypeOf([]interface{}{})). + Build() mdb.clientOptions.SetRegistry(reg) mdb.checkPointIn = time.Minute diff --git a/pkg/mdb2/mdb_test.go b/pkg/mdb2/mdb_test.go index 9ac1108..3bf8d57 100644 --- a/pkg/mdb2/mdb_test.go +++ b/pkg/mdb2/mdb_test.go @@ -6,10 +6,13 @@ import ( "encoding/json" "fmt" "io/ioutil" + "os" "reflect" "strings" "testing" + "github.com/minus5/svckit/log" + "github.com/minus5/svckit/pkg/testu2/mongo" "github.com/stretchr/testify/assert" ) @@ -21,6 +24,35 @@ type obj2 struct { Id interface{} `bson:"_id"` } +type db2 struct { + Mdb +} + +const ( + testDbName = "test_mdb" + testCollectionName = "col" + testTopic = "test.topic" +) + +var mongoSrv *mongo.Mongo +var db *db2 + +func TestMain(m *testing.M) { + // starts test mongo server + mongoSrv = mongo.New() + log.SetOutput(ioutil.Discard) + + os.Exit(m.Run()) +} + +func setup(t *testing.T) { + db = &db2{} + if err := db.Init(DefaultConnStr(), MajoritySafe(), Name(testDbName)); err != nil { + log.Fatalf("failed to open connection: %s", err) + return + } +} + var testCacheDir = "./tmp/cacheDir" func TestCacheAdd(t *testing.T) { @@ -111,3 +143,26 @@ func _id(o interface{}) interface{} { } return nil } + +func TestMongoSerde(t *testing.T) { + setup(t) + m := map[string]interface{}{ + "a": 12345, + "b": map[string]interface{}{ + "i": 12345678901235, + "i64": int64(12345678901235), + "i32": int32(1234567), + }, + "c": 12345678901234, + } + db.SaveId(testCollectionName, 2513, m) + + var res map[string]interface{} + db.ReadId(testCollectionName, 2513, &res) + + assert.Equal(t, m["a"], res["a"]) + assert.Equal(t, int64(m["b"].(map[string]interface{})["i"].(int)), res["b"].(map[string]interface{})["i"]) + assert.Equal(t, m["b"].(map[string]interface{})["i64"].(int64), res["b"].(map[string]interface{})["i64"]) + assert.Equal(t, int(m["b"].(map[string]interface{})["i32"].(int32)), res["b"].(map[string]interface{})["i32"]) + assert.Equal(t, int64(m["c"].(int)), res["c"]) +}