Skip to content

Commit da16339

Browse files
committed
GODRIVER-3037 Support internal-only options.
1 parent 7791095 commit da16339

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

mongo/options/internaloptions.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
import (
10+
"fmt"
11+
12+
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
13+
)
14+
15+
// Deprecated: SetInternalClientOptions sets internal only options for ClientOptions. It may be changed
16+
// or removed in any release.
17+
func SetInternalClientOptions(opts *ClientOptions, custom map[string]any) (*ClientOptions, error) {
18+
const typeErr = "unexpected type for %s"
19+
for k, v := range custom {
20+
switch k {
21+
case "crypt":
22+
c, ok := v.(driver.Crypt)
23+
if !ok {
24+
return nil, fmt.Errorf(typeErr, k)
25+
}
26+
opts.Crypt = c
27+
case "deployment":
28+
d, ok := v.(driver.Deployment)
29+
if !ok {
30+
return nil, fmt.Errorf(typeErr, k)
31+
}
32+
opts.Deployment = d
33+
default:
34+
return nil, fmt.Errorf("unsupported option: %s", k)
35+
}
36+
}
37+
return opts, nil
38+
}

mongo/options/internaloptions_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
import (
10+
"testing"
11+
12+
"go.mongodb.org/mongo-driver/v2/internal/require"
13+
"go.mongodb.org/mongo-driver/v2/x/mongo/driver"
14+
"go.mongodb.org/mongo-driver/v2/x/mongo/driver/drivertest"
15+
)
16+
17+
func TestSetClientOptions(t *testing.T) {
18+
t.Parallel()
19+
20+
t.Run("set Crypt with driver.Crypt", func(t *testing.T) {
21+
t.Parallel()
22+
23+
opts := &ClientOptions{}
24+
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)
30+
})
31+
32+
t.Run("set Crypt with driver.Deployment", func(t *testing.T) {
33+
t.Parallel()
34+
35+
opts := &ClientOptions{}
36+
_, err := SetInternalClientOptions(opts, map[string]any{
37+
"crypt": &drivertest.MockDeployment{},
38+
})
39+
require.EqualError(t, err, "unexpected type for crypt")
40+
})
41+
42+
t.Run("set Deployment with driver.Deployment", func(t *testing.T) {
43+
t.Parallel()
44+
45+
opts := &ClientOptions{}
46+
d := &drivertest.MockDeployment{}
47+
opts, err := SetInternalClientOptions(opts, map[string]any{
48+
"deployment": d,
49+
})
50+
require.NoError(t, err)
51+
require.Equal(t, d, opts.Deployment)
52+
})
53+
54+
t.Run("set Deployment with driver.Crypt", func(t *testing.T) {
55+
t.Parallel()
56+
57+
opts := &ClientOptions{}
58+
_, err := SetInternalClientOptions(opts, map[string]any{
59+
"deployment": driver.NewCrypt(&driver.CryptOptions{}),
60+
})
61+
require.EqualError(t, err, "unexpected type for deployment")
62+
})
63+
64+
t.Run("set unsupported option", func(t *testing.T) {
65+
t.Parallel()
66+
67+
opts := &ClientOptions{}
68+
_, err := SetInternalClientOptions(opts, map[string]any{
69+
"unsupported": "unsupported",
70+
})
71+
require.EqualError(t, err, "unsupported option: unsupported")
72+
})
73+
}

0 commit comments

Comments
 (0)