Skip to content

Commit e277ba3

Browse files
committed
improve interface
1 parent dbd7837 commit e277ba3

File tree

5 files changed

+87
-42
lines changed

5 files changed

+87
-42
lines changed

internal/options/options.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (C) MongoDB, Inc. 2025-present.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"); you may
4+
// not use this file except in compliance with the License. You may obtain
5+
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
7+
package options
8+
9+
// Options stores internal options.
10+
type Options struct {
11+
values map[string]any
12+
}
13+
14+
// WithValue sets an option value with the associated key.
15+
func WithValue(opts Options, key string, option any) Options {
16+
if opts.values == nil {
17+
opts.values = make(map[string]any)
18+
}
19+
opts.values[key] = option
20+
return opts
21+
}
22+
23+
// Value returns the value associated with the options for key.
24+
func Value(opts Options, key string) any {
25+
if opts.values == nil {
26+
return nil
27+
}
28+
if val, ok := opts.values[key]; ok {
29+
return val
30+
}
31+
return nil
32+
}
33+
34+
// Equal compares two Options instances for equality.
35+
func Equal(opts1, opts2 Options) bool {
36+
if len(opts1.values) != len(opts2.values) {
37+
return false
38+
}
39+
for key, val1 := range opts1.values {
40+
if val2, ok := opts2.values[key]; !ok || val1 != val2 {
41+
return false
42+
}
43+
}
44+
return true
45+
}

mongo/options/clientoptions.go

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"go.mongodb.org/mongo-driver/v2/bson"
2727
"go.mongodb.org/mongo-driver/v2/event"
2828
"go.mongodb.org/mongo-driver/v2/internal/httputil"
29+
"go.mongodb.org/mongo-driver/v2/internal/options"
2930
"go.mongodb.org/mongo-driver/v2/mongo/readconcern"
3031
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
3132
"go.mongodb.org/mongo-driver/v2/mongo/writeconcern"
@@ -295,6 +296,12 @@ type ClientOptions struct {
295296
// release.
296297
Deployment driver.Deployment
297298

299+
// Custom specifies internal options for the new Client.
300+
//
301+
// Deprecated: This option is for internal use only and should not be set. It may be changed or removed in any
302+
// release.
303+
Custom options.Options
304+
298305
connString *connstring.ConnString
299306
err error
300307
}

mongo/options/clientoptions_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"go.mongodb.org/mongo-driver/v2/event"
2828
"go.mongodb.org/mongo-driver/v2/internal/assert"
2929
"go.mongodb.org/mongo-driver/v2/internal/httputil"
30+
"go.mongodb.org/mongo-driver/v2/internal/options"
3031
"go.mongodb.org/mongo-driver/v2/internal/ptrutil"
3132
"go.mongodb.org/mongo-driver/v2/mongo/readconcern"
3233
"go.mongodb.org/mongo-driver/v2/mongo/readpref"
@@ -156,6 +157,7 @@ func TestClientOptions(t *testing.T) {
156157
cmp.Comparer(func(r1, r2 *bson.Registry) bool { return r1 == r2 }),
157158
cmp.Comparer(func(cfg1, cfg2 *tls.Config) bool { return cfg1 == cfg2 }),
158159
cmp.Comparer(func(fp1, fp2 *event.PoolMonitor) bool { return fp1 == fp2 }),
160+
cmp.Comparer(options.Equal),
159161
cmp.AllowUnexported(ClientOptions{}),
160162
cmpopts.IgnoreFields(http.Client{}, "Transport"),
161163
); diff != "" {
@@ -1253,6 +1255,7 @@ func TestApplyURI(t *testing.T) {
12531255
cmp.Comparer(func(r1, r2 *bson.Registry) bool { return r1 == r2 }),
12541256
cmp.Comparer(compareTLSConfig),
12551257
cmp.Comparer(compareErrors),
1258+
cmp.Comparer(options.Equal),
12561259
cmpopts.SortSlices(stringLess),
12571260
cmpopts.IgnoreFields(connstring.ConnString{}, "SSLClientCertificateKeyPassword"),
12581261
cmpopts.IgnoreFields(http.Client{}, "Transport"),

mongo/options/internaloptions.go renamed to x/mongo/driver/options/options.go

+17-18
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,30 @@ package options
99
import (
1010
"fmt"
1111

12+
"go.mongodb.org/mongo-driver/v2/mongo/options"
1213
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
1314
)
1415

1516
// SetInternalClientOptions sets internal options for ClientOptions.
1617
//
1718
// Deprecated: This function is for internal use only. It may be changed or removed in any release.
18-
func SetInternalClientOptions(opts *ClientOptions, custom map[string]any) (*ClientOptions, error) {
19+
func SetInternalClientOptions(opts *options.ClientOptions, key string, option any) error {
1920
const typeErr = "unexpected type for %s"
20-
for k, v := range custom {
21-
switch k {
22-
case "crypt":
23-
c, ok := v.(driver.Crypt)
24-
if !ok {
25-
return nil, fmt.Errorf(typeErr, k)
26-
}
27-
opts.Crypt = c
28-
case "deployment":
29-
d, ok := v.(driver.Deployment)
30-
if !ok {
31-
return nil, fmt.Errorf(typeErr, k)
32-
}
33-
opts.Deployment = d
34-
default:
35-
return nil, fmt.Errorf("unsupported option: %s", k)
21+
switch key {
22+
case "crypt":
23+
c, ok := option.(driver.Crypt)
24+
if !ok {
25+
return fmt.Errorf(typeErr, key)
3626
}
27+
opts.Crypt = c
28+
case "deployment":
29+
d, ok := option.(driver.Deployment)
30+
if !ok {
31+
return fmt.Errorf(typeErr, key)
32+
}
33+
opts.Deployment = d
34+
default:
35+
return fmt.Errorf("unsupported option: %s", key)
3736
}
38-
return opts, nil
37+
return nil
3938
}

mongo/options/internaloptions_test.go renamed to x/mongo/driver/options/options_test.go

+15-24
Original file line numberDiff line numberDiff line change
@@ -10,64 +10,55 @@ import (
1010
"testing"
1111

1212
"go.mongodb.org/mongo-driver/v2/internal/require"
13+
"go.mongodb.org/mongo-driver/v2/mongo/options"
1314
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
1415
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/drivertest"
1516
)
1617

17-
func TestSetClientOptions(t *testing.T) {
18+
func TestSetInternalClientOptions(t *testing.T) {
1819
t.Parallel()
1920

20-
t.Run("set Crypt with driver.Crypt", func(t *testing.T) {
21+
t.Run("crypt with driver.Crypt", func(t *testing.T) {
2122
t.Parallel()
2223

23-
opts := &ClientOptions{}
2424
c := driver.NewCrypt(&driver.CryptOptions{})
25-
opts, err := SetInternalClientOptions(opts, map[string]any{
26-
"crypt": c,
27-
})
28-
require.NoError(t, err)
29-
require.Equal(t, c, opts.Crypt)
25+
opts := options.Client()
26+
err := SetInternalClientOptions(opts, "crypt", c)
27+
require.NoError(t, err, "error setting crypt: %v", err)
28+
require.Equal(t, c, opts.Crypt, "expected %v, got %v", c, opts.Crypt)
3029
})
3130

3231
t.Run("set Crypt with driver.Deployment", func(t *testing.T) {
3332
t.Parallel()
3433

35-
opts := &ClientOptions{}
36-
_, err := SetInternalClientOptions(opts, map[string]any{
37-
"crypt": &drivertest.MockDeployment{},
38-
})
34+
opts := options.Client()
35+
err := SetInternalClientOptions(opts, "crypt", &drivertest.MockDeployment{})
3936
require.EqualError(t, err, "unexpected type for crypt")
4037
})
4138

4239
t.Run("set Deployment with driver.Deployment", func(t *testing.T) {
4340
t.Parallel()
4441

45-
opts := &ClientOptions{}
4642
d := &drivertest.MockDeployment{}
47-
opts, err := SetInternalClientOptions(opts, map[string]any{
48-
"deployment": d,
49-
})
43+
opts := options.Client()
44+
err := SetInternalClientOptions(opts, "deployment", d)
5045
require.NoError(t, err)
5146
require.Equal(t, d, opts.Deployment)
5247
})
5348

5449
t.Run("set Deployment with driver.Crypt", func(t *testing.T) {
5550
t.Parallel()
5651

57-
opts := &ClientOptions{}
58-
_, err := SetInternalClientOptions(opts, map[string]any{
59-
"deployment": driver.NewCrypt(&driver.CryptOptions{}),
60-
})
52+
opts := options.Client()
53+
err := SetInternalClientOptions(opts, "deployment", driver.NewCrypt(&driver.CryptOptions{}))
6154
require.EqualError(t, err, "unexpected type for deployment")
6255
})
6356

6457
t.Run("set unsupported option", func(t *testing.T) {
6558
t.Parallel()
6659

67-
opts := &ClientOptions{}
68-
_, err := SetInternalClientOptions(opts, map[string]any{
69-
"unsupported": "unsupported",
70-
})
60+
opts := options.Client()
61+
err := SetInternalClientOptions(opts, "unsupported", "unsupported")
7162
require.EqualError(t, err, "unsupported option: unsupported")
7263
})
7364
}

0 commit comments

Comments
 (0)