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 | // ReplaceOptions represents all possible options to the replaceOne() function |
| 10 | type ReplaceOptions struct { |
| 11 | BypassDocumentValidation *bool // If true, allows the write to opt-out of document level validation |
| 12 | Collation *Collation // Specifies a collation |
| 13 | Upsert *bool // When true, creates a new document if no document matches the query |
| 14 | } |
| 15 | |
| 16 | // Replace returns a pointer to a new ReplaceOptions |
| 17 | func Replace() *ReplaceOptions { |
| 18 | return &ReplaceOptions{} |
| 19 | } |
| 20 | |
| 21 | // SetBypassDocumentValidation allows the write to opt-out of document level validation. |
| 22 | // Valid for server versions >= 3.2. For servers < 3.2, this option is ignored. |
| 23 | func (ro *ReplaceOptions) SetBypassDocumentValidation(b bool) *ReplaceOptions { |
| 24 | ro.BypassDocumentValidation = &b |
| 25 | return ro |
| 26 | } |
| 27 | |
| 28 | // SetCollation specifies a collation. |
| 29 | // Valid for servers >= 3.4 |
| 30 | func (ro *ReplaceOptions) SetCollation(c *Collation) *ReplaceOptions { |
| 31 | ro.Collation = c |
| 32 | return ro |
| 33 | } |
| 34 | |
| 35 | // SetUpsert allows the creation of a new document if not document matches the query |
| 36 | func (ro *ReplaceOptions) SetUpsert(b bool) *ReplaceOptions { |
| 37 | ro.Upsert = &b |
| 38 | return ro |
| 39 | } |
| 40 | |
| 41 | // MergeReplaceOptions combines the argued ReplaceOptions into a single ReplaceOptions in a last-one-wins fashion |
| 42 | func MergeReplaceOptions(opts ...*ReplaceOptions) *ReplaceOptions { |
| 43 | rOpts := Replace() |
| 44 | for _, ro := range opts { |
| 45 | if ro == nil { |
| 46 | continue |
| 47 | } |
| 48 | if ro.BypassDocumentValidation != nil { |
| 49 | rOpts.BypassDocumentValidation = ro.BypassDocumentValidation |
| 50 | } |
| 51 | if ro.Collation != nil { |
| 52 | rOpts.Collation = ro.Collation |
| 53 | } |
| 54 | if ro.Upsert != nil { |
| 55 | rOpts.Upsert = ro.Upsert |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return rOpts |
| 60 | } |