From 9852b9c47cb93aa55d17310a0f4dd84b0dae6c7b Mon Sep 17 00:00:00 2001 From: Ramit Mittal Date: Wed, 1 Nov 2023 22:59:16 +0530 Subject: [PATCH] GODRIVER-2582 Accept any value for "documents" in InsertMany (#1437) --- mongo/collection.go | 15 ++++++++++++--- mongo/collection_test.go | 5 ++++- mongo/errors.go | 3 +++ 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/mongo/collection.go b/mongo/collection.go index 129df8ff57..d460214398 100644 --- a/mongo/collection.go +++ b/mongo/collection.go @@ -442,14 +442,23 @@ func (coll *Collection) InsertOne(ctx context.Context, document interface{}, // The opts parameter can be used to specify options for the operation (see the options.InsertManyOptions documentation.) // // For more information about the command, see https://www.mongodb.com/docs/manual/reference/command/insert/. -func (coll *Collection) InsertMany(ctx context.Context, documents []interface{}, +func (coll *Collection) InsertMany(ctx context.Context, documents interface{}, opts ...*options.InsertManyOptions) (*InsertManyResult, error) { - if len(documents) == 0 { + dv := reflect.ValueOf(documents) + if dv.Kind() != reflect.Slice { + return nil, ErrNotSlice + } + if dv.Len() == 0 { return nil, ErrEmptySlice } - result, err := coll.insert(ctx, documents, opts...) + docSlice := make([]interface{}, 0, dv.Len()) + for i := 0; i < dv.Len(); i++ { + docSlice = append(docSlice, dv.Index(i).Interface()) + } + + result, err := coll.insert(ctx, docSlice, opts...) rr, err := processWriteError(err) if rr&rrMany == 0 { return nil, err diff --git a/mongo/collection_test.go b/mongo/collection_test.go index 6e2d9e9f1c..86dc85848a 100644 --- a/mongo/collection_test.go +++ b/mongo/collection_test.go @@ -141,11 +141,14 @@ func TestCollection(t *testing.T) { assert.Equal(t, ErrNilDocument, err, "expected error %v, got %v", ErrNilDocument, err) _, err = coll.InsertMany(bgCtx, nil) - assert.Equal(t, ErrEmptySlice, err, "expected error %v, got %v", ErrEmptySlice, err) + assert.Equal(t, ErrNotSlice, err, "expected error %v, got %v", ErrNotSlice, err) _, err = coll.InsertMany(bgCtx, []interface{}{}) assert.Equal(t, ErrEmptySlice, err, "expected error %v, got %v", ErrEmptySlice, err) + _, err = coll.InsertMany(bgCtx, "x") + assert.Equal(t, ErrNotSlice, err, "expected error %v, got %v", ErrNotSlice, err) + _, err = coll.DeleteOne(bgCtx, nil) assert.Equal(t, ErrNilDocument, err, "expected error %v, got %v", ErrNilDocument, err) diff --git a/mongo/errors.go b/mongo/errors.go index 925a8ab0bd..87bac984c6 100644 --- a/mongo/errors.go +++ b/mongo/errors.go @@ -36,6 +36,9 @@ var ErrNilValue = errors.New("value is nil") // ErrEmptySlice is returned when an empty slice is passed to a CRUD method that requires a non-empty slice. var ErrEmptySlice = errors.New("must provide at least one element in input slice") +// ErrNotSlice is returned when a type other than slice is passed to InsertMany. +var ErrNotSlice = errors.New("must provide a non-empty slice") + // ErrMapForOrderedArgument is returned when a map with multiple keys is passed to a CRUD method for an ordered parameter type ErrMapForOrderedArgument struct { ParamName string