Skip to content

Commit b0038e6

Browse files
GODRIVER-3434 Revert lister-builder pattern for client suboptions
1 parent ad9c732 commit b0038e6

16 files changed

+119
-313
lines changed

internal/integration/client_side_encryption_prose_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ func TestClientSideEncryptionProse(t *testing.T) {
527527
tlsConfig["kmip"] = kmipConfig
528528
}
529529

530-
getBaseAutoEncryptionOpts := func() *options.AutoEncryptionOptionsBuilder {
530+
getBaseAutoEncryptionOpts := func() *options.AutoEncryptionOptions {
531531
return options.AutoEncryption().
532532
SetKmsProviders(fullKmsProvidersMap).
533533
SetKeyVaultNamespace(kvNamespace).
@@ -537,7 +537,7 @@ func TestClientSideEncryptionProse(t *testing.T) {
537537

538538
testCases := []struct {
539539
name string
540-
aeo *options.AutoEncryptionOptionsBuilder
540+
aeo *options.AutoEncryptionOptions
541541
schema bson.Raw // the schema to create the collection. if nil, the collection won't be explicitly created
542542
}{
543543
{"remote schema", getBaseAutoEncryptionOpts(), corpusSchema},
@@ -3014,7 +3014,7 @@ type cseProseTest struct {
30143014
cseStarted []*event.CommandStartedEvent
30153015
}
30163016

3017-
func setup(mt *mtest.T, aeo *options.AutoEncryptionOptionsBuilder, kvClientOpts *options.ClientOptions,
3017+
func setup(mt *mtest.T, aeo *options.AutoEncryptionOptions, kvClientOpts *options.ClientOptions,
30183018
ceo options.Lister[options.ClientEncryptionOptions]) *cseProseTest {
30193019
mt.Helper()
30203020
var cpt cseProseTest

internal/integration/json_helpers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func createClientOptions(t testing.TB, opts bson.Raw) *options.ClientOptions {
125125
return clientOpts
126126
}
127127

128-
func createAutoEncryptionOptions(t testing.TB, opts bson.Raw) *options.AutoEncryptionOptionsBuilder {
128+
func createAutoEncryptionOptions(t testing.TB, opts bson.Raw) *options.AutoEncryptionOptions {
129129
t.Helper()
130130

131131
aeo := options.AutoEncryption()

internal/integration/unified/client_entity.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"go.mongodb.org/mongo-driver/v2/internal/integration/mtest"
2121
"go.mongodb.org/mongo-driver/v2/internal/integtest"
2222
"go.mongodb.org/mongo-driver/v2/internal/logger"
23-
"go.mongodb.org/mongo-driver/v2/internal/mongoutil"
2423
"go.mongodb.org/mongo-driver/v2/mongo"
2524
"go.mongodb.org/mongo-driver/v2/mongo/options"
2625
"go.mongodb.org/mongo-driver/v2/mongo/readconcern"
@@ -184,15 +183,10 @@ func newClientEntity(ctx context.Context, em *EntityMap, entityOptions *entityOp
184183
}
185184
}
186185
if entityOptions.ServerAPIOptions != nil {
187-
args, err := mongoutil.NewOptions[options.ServerAPIOptions](entityOptions.ServerAPIOptions)
188-
if err != nil {
189-
return nil, fmt.Errorf("failed to construct options from builder: %w", err)
190-
}
191-
192-
if err := args.ServerAPIVersion.Validate(); err != nil {
186+
if err := entityOptions.ServerAPIOptions.ServerAPIVersion.Validate(); err != nil {
193187
return nil, err
194188
}
195-
clientOpts.SetServerAPIOptions(entityOptions.ServerAPIOptions.ServerAPIOptionsBuilder)
189+
clientOpts.SetServerAPIOptions(entityOptions.ServerAPIOptions.ServerAPIOptions)
196190
} else {
197191
integtest.AddTestServerAPIVersion(clientOpts)
198192
}

internal/integration/unified/server_api_options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// serverAPIOptions is a wrapper for *options.ServerAPIOptions. This type implements the bson.Unmarshaler interface
1717
// to convert BSON documents to a serverAPIOptions instance.
1818
type serverAPIOptions struct {
19-
*options.ServerAPIOptionsBuilder
19+
*options.ServerAPIOptions
2020
}
2121

2222
type serverAPIVersion = options.ServerAPIVersion
@@ -37,7 +37,7 @@ func (s *serverAPIOptions) UnmarshalBSON(data []byte) error {
3737
return fmt.Errorf("unrecognized fields for serverAPIOptions: %v", mapKeys(temp.Extra))
3838
}
3939

40-
s.ServerAPIOptionsBuilder = options.ServerAPI(temp.ServerAPIVersion)
40+
s.ServerAPIOptions = options.ServerAPI(temp.ServerAPIVersion)
4141
if temp.DeprecationErrors != nil {
4242
s.SetDeprecationErrors(*temp.DeprecationErrors)
4343
}

internal/integration/unified_spec_test.go

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ import (
2727
"go.mongodb.org/mongo-driver/v2/internal/failpoint"
2828
"go.mongodb.org/mongo-driver/v2/internal/integration/mtest"
2929
"go.mongodb.org/mongo-driver/v2/internal/integtest"
30-
"go.mongodb.org/mongo-driver/v2/internal/mongoutil"
31-
"go.mongodb.org/mongo-driver/v2/internal/require"
3230
"go.mongodb.org/mongo-driver/v2/mongo"
3331
"go.mongodb.org/mongo-driver/v2/mongo/address"
3432
"go.mongodb.org/mongo-driver/v2/mongo/options"
@@ -291,31 +289,20 @@ func runSpecTestCase(mt *mtest.T, test *testCase, testFile testFile) {
291289
// bypassAutoEncryption nor bypassQueryAnalysis are true), then add extra options to load
292290
// the crypt_shared library.
293291
if testClientOpts.AutoEncryptionOptions != nil {
294-
aeArgs, err := mongoutil.NewOptions[options.AutoEncryptionOptions](testClientOpts.AutoEncryptionOptions)
295-
require.NoError(mt, err, "failed to construct options from builder")
292+
aeOpts := testClientOpts.AutoEncryptionOptions
296293

297-
bypassAutoEncryption := aeArgs.BypassAutoEncryption != nil && *aeArgs.BypassAutoEncryption
298-
bypassQueryAnalysis := aeArgs.BypassQueryAnalysis != nil && *aeArgs.BypassQueryAnalysis
294+
bypassAutoEncryption := aeOpts.BypassAutoEncryption != nil && *aeOpts.BypassAutoEncryption
295+
bypassQueryAnalysis := aeOpts.BypassQueryAnalysis != nil && *aeOpts.BypassQueryAnalysis
299296

300297
if !bypassAutoEncryption && !bypassQueryAnalysis {
301-
if aeArgs.ExtraOptions == nil {
302-
aeArgs.ExtraOptions = make(map[string]interface{})
298+
if aeOpts.ExtraOptions == nil {
299+
aeOpts.ExtraOptions = make(map[string]interface{})
303300
}
304301

305302
for k, v := range getCryptSharedLibExtraOptions() {
306-
aeArgs.ExtraOptions[k] = v
303+
aeOpts.ExtraOptions[k] = v
307304
}
308305
}
309-
310-
testClientOpts.AutoEncryptionOptions = &options.AutoEncryptionOptionsBuilder{
311-
Opts: []func(*options.AutoEncryptionOptions) error{
312-
func(args *options.AutoEncryptionOptions) error {
313-
*args = *aeArgs
314-
315-
return nil
316-
},
317-
},
318-
}
319306
}
320307

321308
test.monitor = newUnifiedRunnerEventMonitor()

internal/mongoutil/mongoutil_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,4 @@ func BenchmarkNewOptions(b *testing.B) {
3131
_, _ = NewOptions[options.FindOptions](opts...)
3232
}
3333
})
34-
35-
b.Run("reflect.ValuOf is never called", func(b *testing.B) {
36-
opts := make([]options.Lister[options.LoggerOptions], b.N)
37-
38-
for i := 0; i < b.N; i++ {
39-
var lo *options.LoggerOptionsBuilder
40-
opts[i] = lo
41-
}
42-
43-
b.ReportAllocs()
44-
for i := 0; i < b.N; i++ {
45-
_, _ = NewOptions[options.LoggerOptions](opts...)
46-
}
47-
})
4834
}

mongo/client.go

Lines changed: 35 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Copyright (C) MongoDB, Inc. 2017-present.
21
//
2+
// Copyright (C) MongoDB, Inc. 2017-present.
33
// Licensed under the Apache License, Version 2.0 (the "License"); you may
44
// not use this file except in compliance with the License. You may obtain
55
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
@@ -471,12 +471,7 @@ func (c *Client) endSessions(ctx context.Context) {
471471
}
472472

473473
func (c *Client) configureAutoEncryption(args *options.ClientOptions) error {
474-
aeArgs, err := mongoutil.NewOptions[options.AutoEncryptionOptions](args.AutoEncryptionOptions)
475-
if err != nil {
476-
return fmt.Errorf("failed to construct options from builder: %w", err)
477-
}
478-
479-
c.encryptedFieldsMap = aeArgs.EncryptedFieldsMap
474+
c.encryptedFieldsMap = args.AutoEncryptionOptions.EncryptedFieldsMap
480475
if err := c.configureKeyVaultClientFLE(args); err != nil {
481476
return err
482477
}
@@ -519,60 +514,51 @@ func (c *Client) getOrCreateInternalClient(args *options.ClientOptions) (*Client
519514
return c.internalClientFLE, err
520515
}
521516

522-
func (c *Client) configureKeyVaultClientFLE(clientArgs *options.ClientOptions) error {
523-
// parse key vault options and create new key vault client
524-
aeArgs, err := mongoutil.NewOptions[options.AutoEncryptionOptions](clientArgs.AutoEncryptionOptions)
525-
if err != nil {
526-
return fmt.Errorf("failed to construct options from builder: %w", err)
527-
}
517+
func (c *Client) configureKeyVaultClientFLE(clientOpts *options.ClientOptions) error {
518+
aeOpts := clientOpts.AutoEncryptionOptions
519+
520+
var err error
528521

529522
switch {
530-
case aeArgs.KeyVaultClientOptions != nil:
531-
c.keyVaultClientFLE, err = newClient(aeArgs.KeyVaultClientOptions)
532-
case clientArgs.MaxPoolSize != nil && *clientArgs.MaxPoolSize == 0:
523+
case aeOpts.KeyVaultClientOptions != nil:
524+
c.keyVaultClientFLE, err = newClient(aeOpts.KeyVaultClientOptions)
525+
case clientOpts.MaxPoolSize != nil && *clientOpts.MaxPoolSize == 0:
533526
c.keyVaultClientFLE = c
534527
default:
535-
c.keyVaultClientFLE, err = c.getOrCreateInternalClient(clientArgs)
528+
c.keyVaultClientFLE, err = c.getOrCreateInternalClient(clientOpts)
536529
}
537530

538531
if err != nil {
539532
return err
540533
}
541534

542-
dbName, collName := splitNamespace(aeArgs.KeyVaultNamespace)
535+
dbName, collName := splitNamespace(aeOpts.KeyVaultNamespace)
543536
c.keyVaultCollFLE = c.keyVaultClientFLE.Database(dbName).Collection(collName, keyVaultCollOpts)
544537
return nil
545538
}
546539

547-
func (c *Client) configureMetadataClientFLE(clientArgs *options.ClientOptions) error {
548-
// parse key vault options and create new key vault client
549-
aeArgs, err := mongoutil.NewOptions[options.AutoEncryptionOptions](clientArgs.AutoEncryptionOptions)
550-
if err != nil {
551-
return fmt.Errorf("failed to construct options from builder: %w", err)
552-
}
540+
func (c *Client) configureMetadataClientFLE(clientOpts *options.ClientOptions) error {
541+
aeOpts := clientOpts.AutoEncryptionOptions
553542

554-
if aeArgs.BypassAutoEncryption != nil && *aeArgs.BypassAutoEncryption {
543+
if aeOpts.BypassAutoEncryption != nil && *aeOpts.BypassAutoEncryption {
555544
// no need for a metadata client.
556545
return nil
557546
}
558-
if clientArgs.MaxPoolSize != nil && *clientArgs.MaxPoolSize == 0 {
547+
if clientOpts.MaxPoolSize != nil && *clientOpts.MaxPoolSize == 0 {
559548
c.metadataClientFLE = c
560549
return nil
561550
}
562551

563-
c.metadataClientFLE, err = c.getOrCreateInternalClient(clientArgs)
552+
var err error
553+
c.metadataClientFLE, err = c.getOrCreateInternalClient(clientOpts)
554+
564555
return err
565556
}
566557

567-
func (c *Client) newMongoCrypt(opts options.Lister[options.AutoEncryptionOptions]) (*mongocrypt.MongoCrypt, error) {
568-
args, err := mongoutil.NewOptions[options.AutoEncryptionOptions](opts)
569-
if err != nil {
570-
return nil, fmt.Errorf("failed to construct options from builder: %w", err)
571-
}
572-
558+
func (c *Client) newMongoCrypt(opts *options.AutoEncryptionOptions) (*mongocrypt.MongoCrypt, error) {
573559
// convert schemas in SchemaMap to bsoncore documents
574560
cryptSchemaMap := make(map[string]bsoncore.Document)
575-
for k, v := range args.SchemaMap {
561+
for k, v := range opts.SchemaMap {
576562
schema, err := marshal(v, c.bsonOpts, c.registry)
577563
if err != nil {
578564
return nil, err
@@ -582,23 +568,23 @@ func (c *Client) newMongoCrypt(opts options.Lister[options.AutoEncryptionOptions
582568

583569
// convert schemas in EncryptedFieldsMap to bsoncore documents
584570
cryptEncryptedFieldsMap := make(map[string]bsoncore.Document)
585-
for k, v := range args.EncryptedFieldsMap {
571+
for k, v := range opts.EncryptedFieldsMap {
586572
encryptedFields, err := marshal(v, c.bsonOpts, c.registry)
587573
if err != nil {
588574
return nil, err
589575
}
590576
cryptEncryptedFieldsMap[k] = encryptedFields
591577
}
592578

593-
kmsProviders, err := marshal(args.KmsProviders, c.bsonOpts, c.registry)
579+
kmsProviders, err := marshal(opts.KmsProviders, c.bsonOpts, c.registry)
594580
if err != nil {
595581
return nil, fmt.Errorf("error creating KMS providers document: %w", err)
596582
}
597583

598584
// Set the crypt_shared library override path from the "cryptSharedLibPath" extra option if one
599585
// was set.
600586
cryptSharedLibPath := ""
601-
if val, ok := args.ExtraOptions["cryptSharedLibPath"]; ok {
587+
if val, ok := opts.ExtraOptions["cryptSharedLibPath"]; ok {
602588
str, ok := val.(string)
603589
if !ok {
604590
return nil, fmt.Errorf(
@@ -611,12 +597,12 @@ func (c *Client) newMongoCrypt(opts options.Lister[options.AutoEncryptionOptions
611597
// intended for use from tests; there is no supported public API for explicitly disabling
612598
// loading the crypt_shared library.
613599
cryptSharedLibDisabled := false
614-
if v, ok := args.ExtraOptions["__cryptSharedLibDisabledForTestOnly"]; ok {
600+
if v, ok := opts.ExtraOptions["__cryptSharedLibDisabledForTestOnly"]; ok {
615601
cryptSharedLibDisabled = v.(bool)
616602
}
617603

618-
bypassAutoEncryption := args.BypassAutoEncryption != nil && *args.BypassAutoEncryption
619-
bypassQueryAnalysis := args.BypassQueryAnalysis != nil && *args.BypassQueryAnalysis
604+
bypassAutoEncryption := opts.BypassAutoEncryption != nil && *opts.BypassAutoEncryption
605+
bypassQueryAnalysis := opts.BypassQueryAnalysis != nil && *opts.BypassQueryAnalysis
620606

621607
mc, err := mongocrypt.NewMongoCrypt(mcopts.MongoCrypt().
622608
SetKmsProviders(kmsProviders).
@@ -625,13 +611,13 @@ func (c *Client) newMongoCrypt(opts options.Lister[options.AutoEncryptionOptions
625611
SetEncryptedFieldsMap(cryptEncryptedFieldsMap).
626612
SetCryptSharedLibDisabled(cryptSharedLibDisabled || bypassAutoEncryption).
627613
SetCryptSharedLibOverridePath(cryptSharedLibPath).
628-
SetHTTPClient(args.HTTPClient))
614+
SetHTTPClient(opts.HTTPClient))
629615
if err != nil {
630616
return nil, err
631617
}
632618

633619
var cryptSharedLibRequired bool
634-
if val, ok := args.ExtraOptions["cryptSharedLibRequired"]; ok {
620+
if val, ok := opts.ExtraOptions["cryptSharedLibRequired"]; ok {
635621
b, ok := val.(bool)
636622
if !ok {
637623
return nil, fmt.Errorf(
@@ -652,10 +638,8 @@ func (c *Client) newMongoCrypt(opts options.Lister[options.AutoEncryptionOptions
652638
}
653639

654640
//nolint:unused // the unused linter thinks that this function is unreachable because "c.newMongoCrypt" always panics without the "cse" build tag set.
655-
func (c *Client) configureCryptFLE(mc *mongocrypt.MongoCrypt, opts options.Lister[options.AutoEncryptionOptions]) {
656-
args, _ := mongoutil.NewOptions[options.AutoEncryptionOptions](opts)
657-
658-
bypass := args.BypassAutoEncryption != nil && *args.BypassAutoEncryption
641+
func (c *Client) configureCryptFLE(mc *mongocrypt.MongoCrypt, opts *options.AutoEncryptionOptions) {
642+
bypass := opts.BypassAutoEncryption != nil && *opts.BypassAutoEncryption
659643
kr := keyRetriever{coll: c.keyVaultCollFLE}
660644
var cir collInfoRetriever
661645
// If bypass is true, c.metadataClientFLE is nil and the collInfoRetriever
@@ -670,7 +654,7 @@ func (c *Client) configureCryptFLE(mc *mongocrypt.MongoCrypt, opts options.Liste
670654
CollInfoFn: cir.cryptCollInfo,
671655
KeyFn: kr.cryptKeys,
672656
MarkFn: c.mongocryptdFLE.markCommand,
673-
TLSConfig: args.TLSConfig,
657+
TLSConfig: opts.TLSConfig,
674658
BypassAutoEncryption: bypass,
675659
})
676660
}
@@ -892,28 +876,23 @@ func (c *Client) createBaseCursorOptions() driver.CursorOptions {
892876

893877
// newLogger will use the LoggerOptions to create an internal logger and publish
894878
// messages using a LogSink.
895-
func newLogger(opts options.Lister[options.LoggerOptions]) (*logger.Logger, error) {
879+
func newLogger(opts *options.LoggerOptions) (*logger.Logger, error) {
896880
// If there are no logger options, then create a default logger.
897881
if opts == nil {
898882
opts = options.Logger()
899883
}
900884

901-
args, err := mongoutil.NewOptions[options.LoggerOptions](opts)
902-
if err != nil {
903-
return nil, fmt.Errorf("failed to construct options from builder: %w", err)
904-
}
905-
906885
// If there are no component-level options and the environment does not
907886
// contain component variables, then do nothing.
908-
if len(args.ComponentLevels) == 0 && !logger.EnvHasComponentVariables() {
887+
if len(opts.ComponentLevels) == 0 && !logger.EnvHasComponentVariables() {
909888
return nil, nil
910889
}
911890

912891
// Otherwise, collect the component-level options and create a logger.
913892
componentLevels := make(map[logger.Component]logger.Level)
914-
for component, level := range args.ComponentLevels {
893+
for component, level := range opts.ComponentLevels {
915894
componentLevels[logger.Component(component)] = logger.Level(level)
916895
}
917896

918-
return logger.New(args.Sink, args.MaxDocumentLength, componentLevels)
897+
return logger.New(opts.Sink, opts.MaxDocumentLength, componentLevels)
919898
}

mongo/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func TestClient(t *testing.T) {
413413
}
414414
})
415415
t.Run("serverAPI version", func(t *testing.T) {
416-
getServerAPIOptions := func() *options.ServerAPIOptionsBuilder {
416+
getServerAPIOptions := func() *options.ServerAPIOptions {
417417
return options.ServerAPI(options.ServerAPIVersion1).
418418
SetStrict(false).SetDeprecationErrors(false)
419419
}

0 commit comments

Comments
 (0)