You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

insertoptions.go 2.9 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. package options
  7. // InsertOneOptions represents all possible options to the InsertOne() function.
  8. type InsertOneOptions struct {
  9. BypassDocumentValidation *bool // If true, allows the write to opt-out of document level validation
  10. }
  11. // InsertOne returns a pointer to a new InsertOneOptions
  12. func InsertOne() *InsertOneOptions {
  13. return &InsertOneOptions{}
  14. }
  15. // SetBypassDocumentValidation allows the write to opt-out of document level validation.
  16. // Valid for server versions >= 3.2. For servers < 3.2, this option is ignored.
  17. func (ioo *InsertOneOptions) SetBypassDocumentValidation(b bool) *InsertOneOptions {
  18. ioo.BypassDocumentValidation = &b
  19. return ioo
  20. }
  21. // MergeInsertOneOptions combines the argued InsertOneOptions into a single InsertOneOptions in a last-one-wins fashion
  22. func MergeInsertOneOptions(opts ...*InsertOneOptions) *InsertOneOptions {
  23. ioOpts := InsertOne()
  24. for _, ioo := range opts {
  25. if ioo == nil {
  26. continue
  27. }
  28. if ioo.BypassDocumentValidation != nil {
  29. ioOpts.BypassDocumentValidation = ioo.BypassDocumentValidation
  30. }
  31. }
  32. return ioOpts
  33. }
  34. // InsertManyOptions represents all possible options to the InsertMany() function.
  35. type InsertManyOptions struct {
  36. BypassDocumentValidation *bool // If true, allows the write to opt-out of document level validation
  37. Ordered *bool // If true, when an insert fails, return without performing the remaining inserts. Defaults to true.
  38. }
  39. // InsertMany returns a pointer to a new InsertManyOptions
  40. func InsertMany() *InsertManyOptions {
  41. return &InsertManyOptions{
  42. Ordered: &DefaultOrdered,
  43. }
  44. }
  45. // SetBypassDocumentValidation allows the write to opt-out of document level validation.
  46. // Valid for server versions >= 3.2. For servers < 3.2, this option is ignored.
  47. func (imo *InsertManyOptions) SetBypassDocumentValidation(b bool) *InsertManyOptions {
  48. imo.BypassDocumentValidation = &b
  49. return imo
  50. }
  51. // SetOrdered configures the ordered option. If true, when a write fails, the function will return without attempting
  52. // remaining writes. Defaults to true.
  53. func (imo *InsertManyOptions) SetOrdered(b bool) *InsertManyOptions {
  54. imo.Ordered = &b
  55. return imo
  56. }
  57. // MergeInsertManyOptions combines the argued InsertManyOptions into a single InsertManyOptions in a last-one-wins fashion
  58. func MergeInsertManyOptions(opts ...*InsertManyOptions) *InsertManyOptions {
  59. imOpts := InsertMany()
  60. for _, imo := range opts {
  61. if imo == nil {
  62. continue
  63. }
  64. if imo.BypassDocumentValidation != nil {
  65. imOpts.BypassDocumentValidation = imo.BypassDocumentValidation
  66. }
  67. if imo.Ordered != nil {
  68. imOpts.Ordered = imo.Ordered
  69. }
  70. }
  71. return imOpts
  72. }