Skip to content

Commit 3660569

Browse files
committed
Add mongo/insertopt
GODRIVER-272 Change-Id: If3308cd368fd3e9d7cf412a47de3440a8a883236
1 parent cbef64e commit 3660569

File tree

4 files changed

+626
-24
lines changed

4 files changed

+626
-24
lines changed

mongo/collection.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/mongodb/mongo-go-driver/core/writeconcern"
2222
"github.com/mongodb/mongo-go-driver/mongo/aggregateopt"
2323
"github.com/mongodb/mongo-go-driver/mongo/findopt"
24+
"github.com/mongodb/mongo-go-driver/mongo/insertopt"
2425
"github.com/mongodb/mongo-go-driver/mongo/updateopt"
2526
)
2627

@@ -71,7 +72,7 @@ func (coll *Collection) namespace() command.Namespace {
7172
// TODO(skriptble): Determine if we should unwrap the value for the
7273
// InsertOneResult or just return the bson.Element or a bson.Value.
7374
func (coll *Collection) InsertOne(ctx context.Context, document interface{},
74-
opts ...option.InsertOneOptioner) (*InsertOneResult, error) {
75+
opts ...insertopt.One) (*InsertOneResult, error) {
7576

7677
if ctx == nil {
7778
ctx = context.Background()
@@ -87,16 +88,18 @@ func (coll *Collection) InsertOne(ctx context.Context, document interface{},
8788
return nil, err
8889
}
8990

90-
newOptions := make([]option.InsertOptioner, 0, len(opts))
91-
for _, opt := range opts {
92-
newOptions = append(newOptions, opt)
91+
// convert options into []option.InsertOptioner and dedup
92+
oneOpts, err := insertopt.BundleOne(opts...).Unbundle(true)
93+
94+
if err != nil {
95+
return nil, err
9396
}
9497

9598
oldns := coll.namespace()
9699
cmd := command.Insert{
97100
NS: command.Namespace{DB: oldns.DB, Collection: oldns.Collection},
98101
Docs: []*bson.Document{doc},
99-
Opts: newOptions,
102+
Opts: oneOpts,
100103
}
101104

102105
res, err := dispatch.Insert(ctx, cmd, coll.client.topology, coll.writeSelector, coll.writeConcern)
@@ -118,7 +121,7 @@ func (coll *Collection) InsertOne(ctx context.Context, document interface{},
118121
// *bson.Document. See TransformDocument for the list of valid types for
119122
// documents.
120123
func (coll *Collection) InsertMany(ctx context.Context, documents []interface{},
121-
opts ...option.InsertManyOptioner) (*InsertManyResult, error) {
124+
opts ...insertopt.Many) (*InsertManyResult, error) {
122125

123126
if ctx == nil {
124127
ctx = context.Background()
@@ -141,16 +144,18 @@ func (coll *Collection) InsertMany(ctx context.Context, documents []interface{},
141144
result[i] = insertedID
142145
}
143146

144-
newOptions := make([]option.InsertOptioner, 0, len(opts))
145-
for _, opt := range opts {
146-
newOptions = append(newOptions, opt)
147+
// convert options into []option.InsertOptioner and dedup
148+
manyOpts, err := insertopt.BundleMany(opts...).Unbundle(true)
149+
150+
if err != nil {
151+
return nil, err
147152
}
148153

149154
oldns := coll.namespace()
150155
cmd := command.Insert{
151156
NS: command.Namespace{DB: oldns.DB, Collection: oldns.Collection},
152157
Docs: docs,
153-
Opts: newOptions,
158+
Opts: manyOpts,
154159
}
155160

156161
res, err := dispatch.Insert(ctx, cmd, coll.client.topology, coll.writeSelector, coll.writeConcern)

mongo/collection_internal_test.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/mongodb/mongo-go-driver/internal/testutil"
2020
"github.com/mongodb/mongo-go-driver/mongo/aggregateopt"
2121
"github.com/mongodb/mongo-go-driver/mongo/findopt"
22+
"github.com/mongodb/mongo-go-driver/mongo/insertopt"
2223
"github.com/mongodb/mongo-go-driver/mongo/updateopt"
2324
"github.com/stretchr/testify/assert"
2425
"github.com/stretchr/testify/require"
@@ -153,9 +154,8 @@ func TestCollection_InsertOne_WriteConcernError(t *testing.T) {
153154
doc := bson.NewDocument(bson.EC.ObjectID("_id", objectid.New()))
154155
coll := createTestCollection(t, nil, nil)
155156

156-
optwc, err := Opt.WriteConcern(writeconcern.New(writeconcern.W(25)))
157-
require.NoError(t, err)
158-
_, err = coll.InsertOne(context.Background(), doc, optwc)
157+
optwc := insertopt.WriteConcern(writeconcern.New(writeconcern.W(25)))
158+
_, err := coll.InsertOne(context.Background(), doc, optwc)
159159
got, ok := err.(WriteConcernError)
160160
if !ok {
161161
t.Errorf("Did not receive correct type of error. got %T; want %T", err, WriteConcernError{})
@@ -254,7 +254,7 @@ func TestCollection_InsertMany_ErrorCases(t *testing.T) {
254254
require.NoError(t, err)
255255

256256
// without option ordered
257-
_, err = coll.InsertMany(context.Background(), docs, option.OptOrdered(false))
257+
_, err = coll.InsertMany(context.Background(), docs, insertopt.Ordered(false))
258258
got, ok := err.(BulkWriteError)
259259
if !ok {
260260
t.Errorf("Did not receive correct type of error. got %T; want %T", err, WriteErrors{})
@@ -301,13 +301,8 @@ func TestCollection_InsertMany_ErrorCases(t *testing.T) {
301301
bson.NewDocument(bson.EC.ObjectID("_id", objectid.New())),
302302
}
303303

304-
optwc, err := Opt.WriteConcern(writeconcern.New(writeconcern.W(42)))
305-
if err != nil {
306-
t.Errorf("could not create write concern: %+v", err)
307-
t.FailNow()
308-
}
309-
310-
_, err = coll.InsertMany(context.Background(), docs, optwc)
304+
optwc := insertopt.WriteConcern(writeconcern.New(writeconcern.W(42)))
305+
_, err := coll.InsertMany(context.Background(), docs, optwc)
311306
if err == nil {
312307
t.Errorf("write concern error not propagated from command: %+v", err)
313308
}
@@ -338,9 +333,8 @@ func TestCollection_InsertMany_WriteConcernError(t *testing.T) {
338333
}
339334
coll := createTestCollection(t, nil, nil)
340335

341-
optwc, err := Opt.WriteConcern(writeconcern.New(writeconcern.W(25)))
342-
require.NoError(t, err)
343-
_, err = coll.InsertMany(context.Background(), docs, optwc)
336+
optwc := insertopt.WriteConcern(writeconcern.New(writeconcern.W(25)))
337+
_, err := coll.InsertMany(context.Background(), docs, optwc)
344338
got, ok := err.(BulkWriteError)
345339
if !ok {
346340
t.Errorf("Did not receive correct type of error. got %T; want %T\nError message: %s", err, BulkWriteError{}, err)

0 commit comments

Comments
 (0)