Skip to content

Commit

Permalink
GODRIVER-2582 Accept any value for "documents" in InsertMany (#1437)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramitmittal committed Nov 1, 2023
1 parent f675eba commit 9852b9c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
15 changes: 12 additions & 3 deletions mongo/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion mongo/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions mongo/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9852b9c

Please sign in to comment.