Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // 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 | |
| 7 | package options |
| 8 | |
| 9 | // DefaultOrdered is the default order for a BulkWriteOptions struct created from BulkWrite. |
| 10 | var DefaultOrdered = true |
| 11 | |
| 12 | // BulkWriteOptions represent all possible options for a bulkWrite operation. |
| 13 | type BulkWriteOptions struct { |
| 14 | BypassDocumentValidation *bool // If true, allows the write to opt out of document-level validation. |
| 15 | Ordered *bool // If true, when a write fails, return without performing remaining writes. Defaults to true. |
| 16 | } |
| 17 | |
| 18 | // BulkWrite creates a new *BulkWriteOptions |
| 19 | func BulkWrite() *BulkWriteOptions { |
| 20 | return &BulkWriteOptions{ |
| 21 | Ordered: &DefaultOrdered, |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // SetOrdered configures the ordered option. If true, when a write fails, the function will return without attempting |
| 26 | // remaining writes. Defaults to true. |
| 27 | func (b *BulkWriteOptions) SetOrdered(ordered bool) *BulkWriteOptions { |
| 28 | b.Ordered = &ordered |
| 29 | return b |
| 30 | } |
| 31 | |
| 32 | // SetBypassDocumentValidation specifies if the write should opt out of document-level validation. |
| 33 | // Valid for server versions >= 3.2. For servers < 3.2, this option is ignored. |
| 34 | func (b *BulkWriteOptions) SetBypassDocumentValidation(bypass bool) *BulkWriteOptions { |
| 35 | b.BypassDocumentValidation = &bypass |
| 36 | return b |
| 37 | } |
| 38 | |
| 39 | // MergeBulkWriteOptions combines the given *BulkWriteOptions into a single *BulkWriteOptions in a last one wins fashion. |
| 40 | func MergeBulkWriteOptions(opts ...*BulkWriteOptions) *BulkWriteOptions { |
| 41 | b := BulkWrite() |
| 42 | for _, opt := range opts { |
| 43 | if opt == nil { |
| 44 | continue |
| 45 | } |
| 46 | if opt.Ordered != nil { |
| 47 | b.Ordered = opt.Ordered |
| 48 | } |
| 49 | if opt.BypassDocumentValidation != nil { |
| 50 | b.BypassDocumentValidation = opt.BypassDocumentValidation |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return b |
| 55 | } |