Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Dec 7, 2023
1 parent c6914e1 commit 99c10fd
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 34 deletions.
3 changes: 2 additions & 1 deletion bson/bson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ func TestMapCodec(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
mapCodec := bsoncodec.NewMapCodec(tc.opts)
mapRegistry := NewRegistryBuilder().RegisterDefaultEncoder(reflect.Map, mapCodec).Build()
mapRegistry := NewRegistry()
mapRegistry.RegisterKindEncoder(reflect.Map, mapCodec)
buf.Reset()
vw, err := bsonrw.NewBSONValueWriter(buf)
assert.Nil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion bson/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func TestDecoderv2(t *testing.T) {
t.Run("SetRegistry", func(t *testing.T) {
t.Parallel()

r1, r2 := DefaultRegistry, NewRegistryBuilder().Build()
r1, r2 := DefaultRegistry, NewRegistry()
dc1 := bsoncodec.DecodeContext{Registry: r1}
dc2 := bsoncodec.DecodeContext{Registry: r2}
dec, err := NewDecoder(bsonrw.NewBSONDocumentReader([]byte{}))
Expand Down
5 changes: 2 additions & 3 deletions bson/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,8 @@ func TestCachingEncodersNotSharedAcrossRegistries(t *testing.T) {

return vw.WriteInt32(int32(val.Int()) * -1)
}
customReg := NewRegistryBuilder().
RegisterTypeEncoder(tInt32, encodeInt32).
Build()
customReg := NewRegistry()
customReg.RegisterTypeEncoder(tInt32, encodeInt32)

// Helper function to run the test and make assertions. The provided original value should result in the document
// {"x": {$numberInt: 1}} when marshalled with the default registry.
Expand Down
14 changes: 7 additions & 7 deletions bson/raw_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestRawValue(t *testing.T) {
t.Run("Uses registry attached to value", func(t *testing.T) {
t.Parallel()

reg := bsoncodec.NewRegistryBuilder().Build()
reg := bsoncodec.NewRegistry()
val := RawValue{Type: bsontype.String, Value: bsoncore.AppendString(nil, "foobar"), r: reg}
var s string
want := bsoncodec.ErrNoDecoder{Type: reflect.TypeOf(s)}
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestRawValue(t *testing.T) {
t.Run("Returns lookup error", func(t *testing.T) {
t.Parallel()

reg := bsoncodec.NewRegistryBuilder().Build()
reg := bsoncodec.NewRegistry()
var val RawValue
var s string
want := bsoncodec.ErrNoDecoder{Type: reflect.TypeOf(s)}
Expand All @@ -76,7 +76,7 @@ func TestRawValue(t *testing.T) {
t.Run("Returns DecodeValue error", func(t *testing.T) {
t.Parallel()

reg := NewRegistryBuilder().Build()
reg := NewRegistry()
val := RawValue{Type: bsontype.Double, Value: bsoncore.AppendDouble(nil, 3.14159)}
var s string
want := fmt.Errorf("cannot decode %v into a string type", bsontype.Double)
Expand All @@ -88,7 +88,7 @@ func TestRawValue(t *testing.T) {
t.Run("Success", func(t *testing.T) {
t.Parallel()

reg := NewRegistryBuilder().Build()
reg := NewRegistry()
want := float64(3.14159)
val := RawValue{Type: bsontype.Double, Value: bsoncore.AppendDouble(nil, want)}
var got float64
Expand All @@ -115,7 +115,7 @@ func TestRawValue(t *testing.T) {
t.Run("Returns lookup error", func(t *testing.T) {
t.Parallel()

dc := bsoncodec.DecodeContext{Registry: bsoncodec.NewRegistryBuilder().Build()}
dc := bsoncodec.DecodeContext{Registry: bsoncodec.NewRegistry()}
var val RawValue
var s string
want := bsoncodec.ErrNoDecoder{Type: reflect.TypeOf(s)}
Expand All @@ -127,7 +127,7 @@ func TestRawValue(t *testing.T) {
t.Run("Returns DecodeValue error", func(t *testing.T) {
t.Parallel()

dc := bsoncodec.DecodeContext{Registry: NewRegistryBuilder().Build()}
dc := bsoncodec.DecodeContext{Registry: NewRegistry()}
val := RawValue{Type: bsontype.Double, Value: bsoncore.AppendDouble(nil, 3.14159)}
var s string
want := fmt.Errorf("cannot decode %v into a string type", bsontype.Double)
Expand All @@ -139,7 +139,7 @@ func TestRawValue(t *testing.T) {
t.Run("Success", func(t *testing.T) {
t.Parallel()

dc := bsoncodec.DecodeContext{Registry: NewRegistryBuilder().Build()}
dc := bsoncodec.DecodeContext{Registry: NewRegistry()}
want := float64(3.14159)
val := RawValue{Type: bsontype.Double, Value: bsoncore.AppendDouble(nil, want)}
var got float64
Expand Down
5 changes: 2 additions & 3 deletions bson/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,8 @@ func TestCachingDecodersNotSharedAcrossRegistries(t *testing.T) {
val.SetInt(int64(-1 * i32))
return nil
}
customReg := NewRegistryBuilder().
RegisterTypeDecoder(tInt32, decodeInt32).
Build()
customReg := NewRegistry()
customReg.RegisterTypeDecoder(tInt32, decodeInt32)

docBytes := bsoncore.BuildDocumentFromElements(
nil,
Expand Down
10 changes: 6 additions & 4 deletions bson/unmarshal_value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,16 @@ func TestUnmarshalValue(t *testing.T) {
bytes: bsoncore.AppendString(nil, "hello world"),
},
}
rb := NewRegistryBuilder().RegisterTypeDecoder(reflect.TypeOf([]byte{}), bsoncodec.NewSliceCodec()).Build()
reg := NewRegistry()
reg.RegisterTypeDecoder(reflect.TypeOf([]byte{}), bsoncodec.NewSliceCodec())
for _, tc := range testCases {
tc := tc

t.Run(tc.name, func(t *testing.T) {
t.Parallel()

gotValue := reflect.New(reflect.TypeOf(tc.val))
err := UnmarshalValueWithRegistry(rb, tc.bsontype, tc.bytes, gotValue.Interface())
err := UnmarshalValueWithRegistry(reg, tc.bsontype, tc.bytes, gotValue.Interface())
assert.Nil(t, err, "UnmarshalValueWithRegistry error: %v", err)
assert.Equal(t, tc.val, gotValue.Elem().Interface(), "value mismatch; expected %s, got %s", tc.val, gotValue.Elem())
})
Expand All @@ -111,12 +112,13 @@ func BenchmarkSliceCodecUnmarshal(b *testing.B) {
bytes: bsoncore.AppendString(nil, strings.Repeat("t", 4096)),
},
}
rb := NewRegistryBuilder().RegisterTypeDecoder(reflect.TypeOf([]byte{}), bsoncodec.NewSliceCodec()).Build()
reg := NewRegistry()
reg.RegisterTypeDecoder(reflect.TypeOf([]byte{}), bsoncodec.NewSliceCodec())
for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := UnmarshalValueWithRegistry(rb, bm.bsontype, bm.bytes, &[]byte{})
err := UnmarshalValueWithRegistry(reg, bm.bsontype, bm.bytes, &[]byte{})
if err != nil {
b.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions mongo/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestDatabase(t *testing.T) {
wc2 := &writeconcern.WriteConcern{W: 10}
rcLocal := readconcern.Local()
rcMajority := readconcern.Majority()
reg := bsoncodec.NewRegistryBuilder().Build()
reg := bsoncodec.NewRegistry()

opts := options.Database().SetReadPreference(rpPrimary).SetReadConcern(rcLocal).SetWriteConcern(wc1).
SetReadPreference(rpSecondary).SetReadConcern(rcMajority).SetWriteConcern(wc2).SetRegistry(reg)
Expand All @@ -71,7 +71,7 @@ func TestDatabase(t *testing.T) {
rpPrimary := readpref.Primary()
rcLocal := readconcern.Local()
wc1 := &writeconcern.WriteConcern{W: 10}
reg := bsoncodec.NewRegistryBuilder().Build()
reg := bsoncodec.NewRegistry()

client := setupClient(options.Client().SetReadPreference(rpPrimary).SetReadConcern(rcLocal).SetRegistry(reg))
got := client.Database("foo", options.Database().SetWriteConcern(wc1))
Expand Down
5 changes: 4 additions & 1 deletion mongo/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,11 @@ func (sc *slowConn) Read(b []byte) (n int, err error) {
func TestClient(t *testing.T) {
mt := mtest.New(t, noClientOpts)

reg := bson.NewRegistry()
reg.RegisterTypeEncoder(reflect.TypeOf(int64(0)), &negateCodec{})
reg.RegisterTypeDecoder(reflect.TypeOf(int64(0)), &negateCodec{})
registryOpts := options.Client().
SetRegistry(bson.NewRegistryBuilder().RegisterCodec(reflect.TypeOf(int64(0)), &negateCodec{}).Build())
SetRegistry(reg)
mt.RunOpts("registry passed to cursors", mtest.NewOptions().ClientOptions(registryOpts), func(mt *mtest.T) {
_, err := mt.Coll.InsertOne(context.Background(), negateCodec{ID: 10})
assert.Nil(mt, err, "InsertOne error: %v", err)
Expand Down
8 changes: 6 additions & 2 deletions mongo/integration/crud_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"testing"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/internal/assert"
"go.mongodb.org/mongo-driver/internal/bsonutil"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -55,8 +56,11 @@ type crudOutcome struct {
Collection *outcomeCollection `bson:"collection"`
}

var crudRegistry = bson.NewRegistryBuilder().
RegisterTypeMapEntry(bson.TypeEmbeddedDocument, reflect.TypeOf(bson.Raw{})).Build()
var crudRegistry = func() *bsoncodec.Registry {
reg := bson.NewRegistry()
reg.RegisterTypeMapEntry(bson.TypeEmbeddedDocument, reflect.TypeOf(bson.Raw{}))
return reg
}()

func TestCrudSpec(t *testing.T) {
for _, dir := range []string{crudReadDir, crudWriteDir} {
Expand Down
9 changes: 6 additions & 3 deletions mongo/integration/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"testing"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/internal/assert"
Expand All @@ -31,9 +32,11 @@ const (
)

var (
interfaceAsMapRegistry = bson.NewRegistryBuilder().
RegisterTypeMapEntry(bsontype.EmbeddedDocument, reflect.TypeOf(bson.M{})).
Build()
interfaceAsMapRegistry = func() *bsoncodec.Registry {
reg := bson.NewRegistry()
reg.RegisterTypeMapEntry(bsontype.EmbeddedDocument, reflect.TypeOf(bson.M{}))
return reg
}()
)

func TestDatabase(t *testing.T) {
Expand Down
9 changes: 6 additions & 3 deletions mongo/integration/unified_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,12 @@ var directories = []string{
}

var checkOutcomeOpts = options.Collection().SetReadPreference(readpref.Primary()).SetReadConcern(readconcern.Local())
var specTestRegistry = bson.NewRegistryBuilder().
RegisterTypeMapEntry(bson.TypeEmbeddedDocument, reflect.TypeOf(bson.Raw{})).
RegisterTypeDecoder(reflect.TypeOf(testData{}), bsoncodec.ValueDecoderFunc(decodeTestData)).Build()
var specTestRegistry = func() *bsoncodec.Registry {
reg := bson.NewRegistry()
reg.RegisterTypeMapEntry(bson.TypeEmbeddedDocument, reflect.TypeOf(bson.Raw{}))
reg.RegisterTypeDecoder(reflect.TypeOf(testData{}), bsoncodec.ValueDecoderFunc(decodeTestData))
return reg
}()

func TestUnifiedSpecs(t *testing.T) {
for _, specDir := range directories {
Expand Down
2 changes: 1 addition & 1 deletion mongo/options/clientoptions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestClientOptions(t *testing.T) {
{"Monitor", (*ClientOptions).SetMonitor, &event.CommandMonitor{}, "Monitor", false},
{"ReadConcern", (*ClientOptions).SetReadConcern, readconcern.Majority(), "ReadConcern", false},
{"ReadPreference", (*ClientOptions).SetReadPreference, readpref.SecondaryPreferred(), "ReadPreference", false},
{"Registry", (*ClientOptions).SetRegistry, bson.NewRegistryBuilder().Build(), "Registry", false},
{"Registry", (*ClientOptions).SetRegistry, bson.NewRegistry(), "Registry", false},
{"ReplicaSet", (*ClientOptions).SetReplicaSet, "example-replicaset", "ReplicaSet", true},
{"RetryWrites", (*ClientOptions).SetRetryWrites, true, "RetryWrites", true},
{"ServerSelectionTimeout", (*ClientOptions).SetServerSelectionTimeout, 5 * time.Second, "ServerSelectionTimeout", true},
Expand Down
8 changes: 6 additions & 2 deletions mongo/read_write_concern_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/internal/assert"
"go.mongodb.org/mongo-driver/mongo/readconcern"
Expand All @@ -31,8 +32,11 @@ const (

var (
serverDefaultConcern = []byte{5, 0, 0, 0, 0} // server default read concern and write concern is empty document
specTestRegistry = bson.NewRegistryBuilder().
RegisterTypeMapEntry(bson.TypeEmbeddedDocument, reflect.TypeOf(bson.Raw{})).Build()
specTestRegistry = func() *bsoncodec.Registry {
reg := bson.NewRegistry()
reg.RegisterTypeMapEntry(bson.TypeEmbeddedDocument, reflect.TypeOf(bson.Raw{}))
return reg
}()
)

type connectionStringTestFile struct {
Expand Down
2 changes: 1 addition & 1 deletion x/mongo/driver/topology/server_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"go.mongodb.org/mongo-driver/x/mongo/driver/session"
)

var defaultRegistry = bson.NewRegistryBuilder().Build()
var defaultRegistry = bson.NewRegistry()

type serverConfig struct {
clock *session.ClusterClock
Expand Down

0 comments on commit 99c10fd

Please sign in to comment.