Skip to content

Commit 79e6c40

Browse files
author
Divjot Arora
committed
Pass custom registry to topology options.
GODRIVER-681 Change-Id: I50ac9930acd72719274a03357a20eb841a346c32
1 parent 620f598 commit 79e6c40

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

mongo/client_internal_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ import (
2222

2323
"time"
2424

25+
"github.com/mongodb/mongo-go-driver/bson/bsoncodec"
26+
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
2527
"github.com/mongodb/mongo-go-driver/mongo/options"
2628
"github.com/mongodb/mongo-go-driver/mongo/readpref"
2729
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
2830
"github.com/mongodb/mongo-go-driver/x/mongo/driver/session"
2931
"github.com/mongodb/mongo-go-driver/x/mongo/driver/uuid"
3032
"github.com/mongodb/mongo-go-driver/x/network/connstring"
33+
"reflect"
3134
)
3235

3336
func createTestClient(t *testing.T) *Client {
@@ -94,6 +97,62 @@ func TestClientOptions(t *testing.T) {
9497
require.Equal(t, "test", c.connString.ReplicaSet)
9598
}
9699

100+
type NewCodec struct {
101+
ID int64 `bson:"_id"`
102+
}
103+
104+
func (e *NewCodec) EncodeValue(ectx bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
105+
return vw.WriteInt64(val.Int())
106+
}
107+
108+
// DecodeValue negates the value of ID when reading
109+
func (e *NewCodec) DecodeValue(ectx bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
110+
i, err := vr.ReadInt64()
111+
if err != nil {
112+
return err
113+
}
114+
115+
val.SetInt(i * -1)
116+
return nil
117+
}
118+
119+
func TestClientRegistryPassedToCursors(t *testing.T) {
120+
// register a new codec for the int64 type that does the default encoding for an int64 and negates the value when
121+
// decoding
122+
123+
rb := bson.NewRegistryBuilder()
124+
cod := &NewCodec{}
125+
rb.RegisterCodec(reflect.TypeOf(int64(0)), cod)
126+
127+
cs := testutil.ConnString(t)
128+
client, err := NewClientWithOptions(cs.String(), options.Client().SetRegistry(rb.Build()))
129+
require.NoError(t, err)
130+
err = client.Connect(ctx)
131+
require.NoError(t, err)
132+
133+
db := client.Database("TestRegistryDB")
134+
defer func() {
135+
_ = db.Drop(ctx)
136+
_ = client.Disconnect(ctx)
137+
}()
138+
139+
coll := db.Collection("TestRegistryColl")
140+
141+
_, err = coll.InsertOne(ctx, NewCodec{ID: 10})
142+
require.NoError(t, err)
143+
144+
c, err := coll.Find(ctx, nil)
145+
require.NoError(t, err)
146+
147+
require.True(t, c.Next(ctx))
148+
149+
var foundDoc NewCodec
150+
err = c.Decode(&foundDoc)
151+
require.NoError(t, err)
152+
153+
require.Equal(t, foundDoc.ID, int64(-10))
154+
}
155+
97156
func TestClient_TLSConnection(t *testing.T) {
98157
skipIfBelow30(t) // 3.0 doesn't return a security field in the serverStatus response
99158
t.Parallel()

mongo/options/clientoptions.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,19 @@ func (c *ClientOptions) SetReadPreference(rp *readpref.ReadPref) *ClientOptions
220220
func (c *ClientOptions) SetRegistry(registry *bsoncodec.Registry) *ClientOptions {
221221
c.Registry = registry
222222

223+
// add registry to the server options so that it will be used for the cursors built by this client
224+
c.TopologyOptions = append(
225+
c.TopologyOptions,
226+
topology.WithServerOptions(func(opts ...topology.ServerOption) []topology.ServerOption {
227+
return append(
228+
opts,
229+
topology.WithRegistry(func(*bsoncodec.Registry) *bsoncodec.Registry {
230+
return registry
231+
}),
232+
)
233+
}),
234+
)
235+
223236
return c
224237
}
225238

0 commit comments

Comments
 (0)