Skip to content

Commit

Permalink
Update InsertOneOptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Nov 27, 2023
1 parent d33301f commit e3feaac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
24 changes: 10 additions & 14 deletions mongo/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,25 +402,21 @@ func (coll *Collection) insert(ctx context.Context, documents []interface{},
func (coll *Collection) InsertOne(ctx context.Context, document interface{},
opts ...*options.InsertOneOptions) (*InsertOneResult, error) {

ioOpts := options.InsertOne()
for _, ioo := range opts {
if ioo == nil {
continue
}
if ioo.BypassDocumentValidation != nil {
ioOpts.BypassDocumentValidation = ioo.BypassDocumentValidation
}
if ioo.Comment != nil {
ioOpts.Comment = ioo.Comment
args := &options.InsertOneArgs{}
for _, opt := range opts {
for _, optFn := range opt.Opts {
if err := optFn(args); err != nil {
return nil, err
}
}
}
imOpts := options.InsertMany()

if ioOpts.BypassDocumentValidation != nil && *ioOpts.BypassDocumentValidation {
imOpts.SetBypassDocumentValidation(*ioOpts.BypassDocumentValidation)
if args.BypassDocumentValidation != nil && *args.BypassDocumentValidation {
imOpts.SetBypassDocumentValidation(*args.BypassDocumentValidation)
}
if ioOpts.Comment != nil {
imOpts.SetComment(ioOpts.Comment)
if args.Comment != nil {
imOpts.SetComment(args.Comment)
}
res, err := coll.insert(ctx, []interface{}{document}, imOpts)

Expand Down
19 changes: 15 additions & 4 deletions mongo/options/insertoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

package options

// InsertOneOptions represents options that can be used to configure an InsertOne operation.
type InsertOneOptions struct {
// InsertOneArgs represents arguments that can be used to configure an InsertOne operation.
type InsertOneArgs struct {
// If true, writes executed as part of the operation will opt out of document-level validation on the server. This
// option is valid for MongoDB versions >= 3.2 and is ignored for previous server versions. The default value is
// false. See https://www.mongodb.com/docs/manual/core/schema-validation/ for more information about document
Expand All @@ -19,20 +19,31 @@ type InsertOneOptions struct {
Comment interface{}
}

// InsertOneOptions represents functional options that configure an InsertOneArgs.
type InsertOneOptions struct {
Opts []func(*InsertOneArgs) error
}

// InsertOne creates a new InsertOneOptions instance.
func InsertOne() *InsertOneOptions {
return &InsertOneOptions{}
}

// SetBypassDocumentValidation sets the value for the BypassDocumentValidation field.
func (ioo *InsertOneOptions) SetBypassDocumentValidation(b bool) *InsertOneOptions {
ioo.BypassDocumentValidation = &b
ioo.Opts = append(ioo.Opts, func(args *InsertOneArgs) error {
args.BypassDocumentValidation = &b
return nil
})
return ioo
}

// SetComment sets the value for the Comment field.
func (ioo *InsertOneOptions) SetComment(comment interface{}) *InsertOneOptions {
ioo.Comment = comment
ioo.Opts = append(ioo.Opts, func(args *InsertOneArgs) error {
args.Comment = &comment
return nil
})
return ioo
}

Expand Down

0 comments on commit e3feaac

Please sign in to comment.