blob: 064ede3865a62bd551b21948ad36897608107fd5 [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// Copyright (C) MongoDB, Inc. 2017-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package options
8
9// InsertOneOptions represents all possible options to the insertOne()
10type InsertOneOptions struct {
11 BypassDocumentValidation *bool // If true, allows the write to opt-out of document level validation
12}
13
14// InsertOne returns a pointer to a new InsertOneOptions
15func InsertOne() *InsertOneOptions {
16 return &InsertOneOptions{}
17}
18
19// SetBypassDocumentValidation allows the write to opt-out of document level validation.
20// Valid for server versions >= 3.2. For servers < 3.2, this option is ignored.
21func (ioo *InsertOneOptions) SetBypassDocumentValidation(b bool) *InsertOneOptions {
22 ioo.BypassDocumentValidation = &b
23 return ioo
24}
25
26// MergeInsertOneOptions combines the argued InsertOneOptions into a single InsertOneOptions in a last-one-wins fashion
27func MergeInsertOneOptions(opts ...*InsertOneOptions) *InsertOneOptions {
28 ioOpts := InsertOne()
29 for _, ioo := range opts {
30 if ioo == nil {
31 continue
32 }
33 if ioo.BypassDocumentValidation != nil {
34 ioOpts.BypassDocumentValidation = ioo.BypassDocumentValidation
35 }
36 }
37
38 return ioOpts
39}
40
41// InsertManyOptions represents all possible options to the insertMany()
42type InsertManyOptions struct {
43 BypassDocumentValidation *bool // If true, allows the write to opt-out of document level validation
44 Ordered *bool // If true, when an insert fails, return without performing the remaining inserts. Defaults to true.
45}
46
47// InsertMany returns a pointer to a new InsertManyOptions
48func InsertMany() *InsertManyOptions {
49 return &InsertManyOptions{
50 Ordered: &DefaultOrdered,
51 }
52}
53
54// SetBypassDocumentValidation allows the write to opt-out of document level validation.
55// Valid for server versions >= 3.2. For servers < 3.2, this option is ignored.
56func (imo *InsertManyOptions) SetBypassDocumentValidation(b bool) *InsertManyOptions {
57 imo.BypassDocumentValidation = &b
58 return imo
59}
60
61// SetOrdered configures the ordered option. If true, when a write fails, the function will return without attempting
62// remaining writes. Defaults to true.
63func (imo *InsertManyOptions) SetOrdered(b bool) *InsertManyOptions {
64 imo.Ordered = &b
65 return imo
66}
67
68// MergeInsertManyOptions combines the argued InsertManyOptions into a single InsertManyOptions in a last-one-wins fashion
69func MergeInsertManyOptions(opts ...*InsertManyOptions) *InsertManyOptions {
70 imOpts := InsertMany()
71 for _, imo := range opts {
72 if imo == nil {
73 continue
74 }
75 if imo.BypassDocumentValidation != nil {
76 imOpts.BypassDocumentValidation = imo.BypassDocumentValidation
77 }
78 if imo.Ordered != nil {
79 imOpts.Ordered = imo.Ordered
80 }
81 }
82
83 return imOpts
84}