Skip to content

Commit a68198b

Browse files
author
Divjot Arora
committed
Add mongo/mongoopt
GODRIVER-272 Change-Id: Ic859a6f885a40991616fccebec0f925fa667037a
1 parent 813c368 commit a68198b

33 files changed

+980
-215
lines changed

mongo/aggregateopt/aggregateopt.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"reflect"
77

88
"github.com/mongodb/mongo-go-driver/core/option"
9+
"github.com/mongodb/mongo-go-driver/mongo/mongoopt"
910
)
1011

1112
var aggregateBundle = new(AggregateBundle)
@@ -47,7 +48,7 @@ func BundleAggregate(opts ...Aggregate) *AggregateBundle {
4748
// AllowDiskUse adds an option to allow aggregation stages to write to temporary files.
4849
func (ab *AggregateBundle) AllowDiskUse(b bool) *AggregateBundle {
4950
bundle := &AggregateBundle{
50-
option: OptAllowDiskUse(b),
51+
option: AllowDiskUse(b),
5152
next: ab,
5253
}
5354

@@ -57,7 +58,7 @@ func (ab *AggregateBundle) AllowDiskUse(b bool) *AggregateBundle {
5758
// BatchSize adds an option to specify the number of documents to return in every batch.
5859
func (ab *AggregateBundle) BatchSize(i int32) *AggregateBundle {
5960
bundle := &AggregateBundle{
60-
option: OptBatchSize(i),
61+
option: BatchSize(i),
6162
next: ab,
6263
}
6364

@@ -67,17 +68,17 @@ func (ab *AggregateBundle) BatchSize(i int32) *AggregateBundle {
6768
// BypassDocumentValidation adds an option to allow the write to opt-out of document-level validation.
6869
func (ab *AggregateBundle) BypassDocumentValidation(b bool) *AggregateBundle {
6970
bundle := &AggregateBundle{
70-
option: OptBypassDocumentValidation(b),
71+
option: BypassDocumentValidation(b),
7172
next: ab,
7273
}
7374

7475
return bundle
7576
}
7677

7778
// Collation adds an option to specify a Collation.
78-
func (ab *AggregateBundle) Collation(c option.Collation) *AggregateBundle {
79+
func (ab *AggregateBundle) Collation(c *mongoopt.Collation) *AggregateBundle {
7980
bundle := &AggregateBundle{
80-
option: OptCollation{Collation: &c},
81+
option: Collation(c),
8182
next: ab,
8283
}
8384

@@ -87,7 +88,7 @@ func (ab *AggregateBundle) Collation(c option.Collation) *AggregateBundle {
8788
// MaxTime adds an option to specify the maximum amount of time to allow the query to run.
8889
func (ab *AggregateBundle) MaxTime(d time.Duration) *AggregateBundle {
8990
bundle := &AggregateBundle{
90-
option: OptMaxTime(d),
91+
option: MaxTime(d),
9192
next: ab,
9293
}
9394

@@ -97,7 +98,7 @@ func (ab *AggregateBundle) MaxTime(d time.Duration) *AggregateBundle {
9798
// Comment adds an option to specify a string to help trace the operation through the database profiler, currentOp, and logs.
9899
func (ab *AggregateBundle) Comment(s string) *AggregateBundle {
99100
bundle := &AggregateBundle{
100-
option: OptComment(s),
101+
option: Comment(s),
101102
next: ab,
102103
}
103104

@@ -107,7 +108,7 @@ func (ab *AggregateBundle) Comment(s string) *AggregateBundle {
107108
// Hint adds an option to specify the index to use for the aggregation.
108109
func (ab *AggregateBundle) Hint(hint interface{}) *AggregateBundle {
109110
bundle := &AggregateBundle{
110-
option: OptHint{hint},
111+
option: Hint(hint),
111112
next: ab,
112113
}
113114

@@ -237,8 +238,10 @@ func BypassDocumentValidation(b bool) OptBypassDocumentValidation {
237238
}
238239

239240
// Collation specifies a collation.
240-
func Collation(c option.Collation) OptCollation {
241-
return OptCollation{Collation: &c}
241+
func Collation(c *mongoopt.Collation) OptCollation {
242+
return OptCollation{
243+
Collation: c.Convert(),
244+
}
242245
}
243246

244247
// MaxTime specifies the maximum amount of time to allow the query to run.

mongo/aggregateopt/aggregateopt_test.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package aggregateopt
33
import (
44
"testing"
55

6+
"reflect"
7+
68
"github.com/mongodb/mongo-go-driver/core/option"
79
"github.com/mongodb/mongo-go-driver/internal/testutil/helpers"
10+
"github.com/mongodb/mongo-go-driver/mongo/mongoopt"
811
)
912

1013
func createNestedBundle1(t *testing.T) *AggregateBundle {
@@ -148,6 +151,36 @@ func TestAggregateOpt(t *testing.T) {
148151
OptBatchSize(1000).ConvertOption(),
149152
}
150153

154+
t.Run("TestAll", func(t *testing.T) {
155+
c := &mongoopt.Collation{
156+
Locale: "string locale",
157+
}
158+
159+
opts := []Aggregate{
160+
AllowDiskUse(true),
161+
BatchSize(5),
162+
BypassDocumentValidation(false),
163+
Collation(c),
164+
Comment("hello world testing find"),
165+
Hint("hint for find"),
166+
MaxTime(5000),
167+
}
168+
bundle := BundleAggregate(opts...)
169+
170+
deleteOpts, err := bundle.Unbundle(true)
171+
testhelpers.RequireNil(t, err, "got non-nill error from unbundle: %s", err)
172+
173+
if len(deleteOpts) != len(opts) {
174+
t.Errorf("expected unbundled opts len %d. got %d", len(opts), len(deleteOpts))
175+
}
176+
177+
for i, opt := range opts {
178+
if !reflect.DeepEqual(opt.ConvertOption(), deleteOpts[i]) {
179+
t.Errorf("opt mismatch. expected %#v, got %#v", opt, deleteOpts[i])
180+
}
181+
}
182+
})
183+
151184
t.Run("MakeOptions", func(t *testing.T) {
152185
head := bundle1
153186

@@ -194,7 +227,7 @@ func TestAggregateOpt(t *testing.T) {
194227
len(tc.expectedOpts))
195228
} else {
196229
for i, opt := range options {
197-
if opt != tc.expectedOpts[i] {
230+
if !reflect.DeepEqual(opt, tc.expectedOpts[i]) {
198231
t.Errorf("expected: %s\nreceived: %s", opt, tc.expectedOpts[i])
199232
}
200233
}

mongo/collection_internal_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414

1515
"github.com/mongodb/mongo-go-driver/bson"
1616
"github.com/mongodb/mongo-go-driver/bson/objectid"
17-
"github.com/mongodb/mongo-go-driver/core/option"
1817
"github.com/mongodb/mongo-go-driver/core/readconcern"
1918
"github.com/mongodb/mongo-go-driver/core/readpref"
2019
"github.com/mongodb/mongo-go-driver/core/writeconcern"
@@ -26,6 +25,7 @@ import (
2625
"github.com/mongodb/mongo-go-driver/mongo/distinctopt"
2726
"github.com/mongodb/mongo-go-driver/mongo/findopt"
2827
"github.com/mongodb/mongo-go-driver/mongo/insertopt"
28+
"github.com/mongodb/mongo-go-driver/mongo/mongoopt"
2929
"github.com/mongodb/mongo-go-driver/mongo/replaceopt"
3030
"github.com/mongodb/mongo-go-driver/mongo/updateopt"
3131
"github.com/stretchr/testify/assert"
@@ -466,7 +466,7 @@ func TestCollection_DeleteOne_notFound_withOption(t *testing.T) {
466466

467467
filter := bson.NewDocument(bson.EC.Int32("x", 0))
468468

469-
collationOpt := &option.Collation{
469+
collationOpt := &mongoopt.Collation{
470470
Locale: "en_US",
471471
}
472472
result, err := coll.DeleteOne(context.Background(), filter, deleteopt.Collation(collationOpt))
@@ -585,7 +585,7 @@ func TestCollection_DeleteMany_notFound_withOption(t *testing.T) {
585585
filter := bson.NewDocument(
586586
bson.EC.SubDocumentFromElements("x", bson.EC.Int32("$lt", 1)))
587587

588-
result, err := coll.DeleteMany(context.Background(), filter, deleteopt.Collation(&option.Collation{Locale: "en_US"}))
588+
result, err := coll.DeleteMany(context.Background(), filter, deleteopt.Collation(&mongoopt.Collation{Locale: "en_US"}))
589589
require.Nil(t, err)
590590
require.Equal(t, result.DeletedCount, int64(0))
591591
}
@@ -1285,7 +1285,7 @@ func TestCollection_Distinct_withOption(t *testing.T) {
12851285
coll := createTestCollection(t, nil, nil)
12861286
initCollection(t, coll)
12871287

1288-
results, err := coll.Distinct(context.Background(), "x", nil, distinctopt.Collation(&option.Collation{Locale: "en_US"}))
1288+
results, err := coll.Distinct(context.Background(), "x", nil, distinctopt.Collation(&mongoopt.Collation{Locale: "en_US"}))
12891289
require.Nil(t, err)
12901290
require.Equal(t, results, []interface{}{int32(1), int32(2), int32(3), int32(4), int32(5)})
12911291
}

mongo/collectionopt/collectionopt_test.go

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ var wc2 = writeconcern.New(writeconcern.W(20))
1818
var rpPrimary = readpref.Primary()
1919
var rpSeconadary = readpref.Secondary()
2020

21+
func requireCollectionEqual(t *testing.T, expected *Collection, actual *Collection) {
22+
switch {
23+
case expected.ReadConcern != actual.ReadConcern:
24+
t.Errorf("read concerns don't match")
25+
case expected.WriteConcern != actual.WriteConcern:
26+
t.Errorf("write concerns don't match")
27+
case expected.ReadPreference != actual.ReadPreference:
28+
t.Errorf("read preferences don't match")
29+
}
30+
}
31+
2132
func createNestedBundle1(t *testing.T) *CollectionBundle {
2233
nested := BundleCollection(ReadConcern(rcMajority))
2334
testhelpers.RequireNotNil(t, nested, "nested bundle was nil")
@@ -60,7 +71,7 @@ func createNestedBundle3(t *testing.T) *CollectionBundle {
6071
return outer
6172
}
6273

63-
func TestDbOpt(t *testing.T) {
74+
func TestCollectionOpt(t *testing.T) {
6475
nilBundle := BundleCollection()
6576
var nilDb = &Collection{}
6677

@@ -95,11 +106,27 @@ func TestDbOpt(t *testing.T) {
95106
WriteConcern: wc1,
96107
}
97108

109+
t.Run("TestAll", func(t *testing.T) {
110+
opts := []Option{
111+
ReadConcern(rcLocal),
112+
WriteConcern(wc1),
113+
ReadPreference(rpPrimary),
114+
}
115+
116+
db, err := BundleCollection(opts...).Unbundle()
117+
testhelpers.RequireNil(t, err, "got non-nil error from unbundle: %s", err)
118+
requireCollectionEqual(t, db, &Collection{
119+
ReadConcern: rcLocal,
120+
WriteConcern: wc1,
121+
ReadPreference: rpPrimary,
122+
})
123+
})
124+
98125
t.Run("Unbundle", func(t *testing.T) {
99126
var cases = []struct {
100-
name string
101-
bundle *CollectionBundle
102-
db *Collection
127+
name string
128+
bundle *CollectionBundle
129+
collection *Collection
103130
}{
104131
{"NilBundle", nilBundle, nilDb},
105132
{"Bundle1", bundle1, bundle1Db},
@@ -111,15 +138,15 @@ func TestDbOpt(t *testing.T) {
111138

112139
for _, tc := range cases {
113140
t.Run(tc.name, func(t *testing.T) {
114-
db, err := tc.bundle.Unbundle()
141+
collection, err := tc.bundle.Unbundle()
115142
testhelpers.RequireNil(t, err, "err unbundling db: %s", err)
116143

117144
switch {
118-
case db.ReadConcern != tc.db.ReadConcern:
145+
case collection.ReadConcern != tc.collection.ReadConcern:
119146
t.Errorf("read concerns don't match")
120-
case db.WriteConcern != tc.db.WriteConcern:
147+
case collection.WriteConcern != tc.collection.WriteConcern:
121148
t.Errorf("write concerns don't match")
122-
case db.ReadPreference != tc.db.ReadPreference:
149+
case collection.ReadPreference != tc.collection.ReadPreference:
123150
t.Errorf("read preferences don't match")
124151
}
125152
})

mongo/countopt/countopt.go

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"reflect"
55

66
"github.com/mongodb/mongo-go-driver/core/option"
7-
"github.com/mongodb/mongo-go-driver/core/readconcern"
7+
"github.com/mongodb/mongo-go-driver/mongo/mongoopt"
88
)
99

1010
var countBundle = new(CountBundle)
@@ -46,31 +46,29 @@ func BundleCount(opts ...Count) *CountBundle {
4646
}
4747

4848
// Collation specifies a collation.
49-
func (cb *CountBundle) Collation(c *option.Collation) *CountBundle {
49+
func (cb *CountBundle) Collation(c *mongoopt.Collation) *CountBundle {
5050
bundle := &CountBundle{
51-
option: OptCollation{
52-
Collation: c,
53-
},
54-
next: cb,
51+
option: Collation(c),
52+
next: cb,
5553
}
5654

5755
return bundle
5856
}
5957

6058
// Limit adds an option to limit the maximum number of documents to count.
61-
func (cb *CountBundle) Limit(i int32) *CountBundle {
59+
func (cb *CountBundle) Limit(i int64) *CountBundle {
6260
bundle := &CountBundle{
63-
option: OptLimit(i),
61+
option: Limit(i),
6462
next: cb,
6563
}
6664

6765
return bundle
6866
}
6967

7068
// Skip adds an option to specify the number of documents to skip before counting.
71-
func (cb *CountBundle) Skip(i int32) *CountBundle {
69+
func (cb *CountBundle) Skip(i int64) *CountBundle {
7270
bundle := &CountBundle{
73-
option: OptSkip(i),
71+
option: Skip(i),
7472
next: cb,
7573
}
7674

@@ -80,7 +78,7 @@ func (cb *CountBundle) Skip(i int32) *CountBundle {
8078
// Hint adds an option to specify the index to use.
8179
func (cb *CountBundle) Hint(hint interface{}) *CountBundle {
8280
bundle := &CountBundle{
83-
option: OptHint{hint},
81+
option: Hint(hint),
8482
next: cb,
8583
}
8684

@@ -90,17 +88,7 @@ func (cb *CountBundle) Hint(hint interface{}) *CountBundle {
9088
// MaxTimeMs adds an option to specify the maximum amount of time to allow the operation to run.
9189
func (cb *CountBundle) MaxTimeMs(i int32) *CountBundle {
9290
bundle := &CountBundle{
93-
option: OptMaxTimeMs(i),
94-
next: cb,
95-
}
96-
97-
return bundle
98-
}
99-
100-
// ReadConcern adds an option to specify a read concern.
101-
func (cb *CountBundle) ReadConcern(rc *readconcern.ReadConcern) *CountBundle {
102-
bundle := &CountBundle{
103-
option: OptReadConcern{rc},
91+
option: MaxTimeMs(i),
10492
next: cb,
10593
}
10694

@@ -215,8 +203,10 @@ func (cb *CountBundle) String() string {
215203
}
216204

217205
// Collation specifies a Collation.
218-
func Collation(collation *option.Collation) OptCollation {
219-
return OptCollation{collation}
206+
func Collation(collation *mongoopt.Collation) OptCollation {
207+
return OptCollation{
208+
Collation: collation.Convert(),
209+
}
220210
}
221211

222212
// Limit limits the maximum number of documents to count.
@@ -239,13 +229,6 @@ func MaxTimeMs(i int32) OptMaxTimeMs {
239229
return OptMaxTimeMs(i)
240230
}
241231

242-
// ReadConcern specifies a read concern.
243-
func ReadConcern(rc *readconcern.ReadConcern) OptReadConcern {
244-
return OptReadConcern{
245-
ReadConcern: rc,
246-
}
247-
}
248-
249232
// OptCollation specifies a collation.
250233
type OptCollation option.OptCollation
251234

@@ -295,13 +278,3 @@ func (opt OptMaxTimeMs) ConvertOption() option.CountOptioner {
295278
}
296279

297280
func (OptMaxTimeMs) count() {}
298-
299-
// OptReadConcern specifies a read concern.
300-
type OptReadConcern option.OptReadConcern
301-
302-
// ConvertOption implements the Count interface.
303-
func (opt OptReadConcern) ConvertOption() option.CountOptioner {
304-
return option.OptReadConcern(opt)
305-
}
306-
307-
func (OptReadConcern) count() {}

0 commit comments

Comments
 (0)