Skip to content

Commit

Permalink
Merge pull request #69 from jprgic/mongo_driver_add_support_for_int32…
Browse files Browse the repository at this point in the history
…_decoding

add read method that decodes bson.int32 to int
  • Loading branch information
ZoranCalic committed Mar 7, 2023
2 parents 1d622f6 + e9bc25f commit 0beed92
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/mdb2/mdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions pkg/mdb2/mdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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) {
Expand Down Expand Up @@ -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"])
}

0 comments on commit 0beed92

Please sign in to comment.