|
| 1 | +package deleteopt |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + |
| 6 | + "github.com/mongodb/mongo-go-driver/core/option" |
| 7 | + "github.com/mongodb/mongo-go-driver/core/writeconcern" |
| 8 | +) |
| 9 | + |
| 10 | +var deleteBundle = new(DeleteBundle) |
| 11 | + |
| 12 | +// Delete is options for the delete() function. |
| 13 | +type Delete interface { |
| 14 | + delete() |
| 15 | + ConvertOption() option.DeleteOptioner |
| 16 | +} |
| 17 | + |
| 18 | +// DeleteBundle is a bundle of Delete options |
| 19 | +type DeleteBundle struct { |
| 20 | + option Delete |
| 21 | + next *DeleteBundle |
| 22 | +} |
| 23 | + |
| 24 | +func (db *DeleteBundle) delete() {} |
| 25 | + |
| 26 | +// ConvertOption implements the Delete interface. |
| 27 | +func (db *DeleteBundle) ConvertOption() option.DeleteOptioner { |
| 28 | + return nil |
| 29 | +} |
| 30 | + |
| 31 | +// BundleDelete bundles Delete options. |
| 32 | +func BundleDelete(opts ...Delete) *DeleteBundle { |
| 33 | + head := deleteBundle |
| 34 | + |
| 35 | + for _, opt := range opts { |
| 36 | + newBundle := DeleteBundle{ |
| 37 | + option: opt, |
| 38 | + next: head, |
| 39 | + } |
| 40 | + |
| 41 | + head = &newBundle |
| 42 | + } |
| 43 | + |
| 44 | + return head |
| 45 | +} |
| 46 | + |
| 47 | +// Collation adds an option to specify a collation. |
| 48 | +func (db *DeleteBundle) Collation(c *option.Collation) *DeleteBundle { |
| 49 | + bundle := &DeleteBundle{ |
| 50 | + option: Collation(c), |
| 51 | + next: db, |
| 52 | + } |
| 53 | + |
| 54 | + return bundle |
| 55 | +} |
| 56 | + |
| 57 | +// WriteConcern adds an option to specify a write concern. |
| 58 | +func (db *DeleteBundle) WriteConcern(wc *writeconcern.WriteConcern) *DeleteBundle { |
| 59 | + bundle := &DeleteBundle{ |
| 60 | + option: WriteConcern(wc), |
| 61 | + next: db, |
| 62 | + } |
| 63 | + |
| 64 | + return bundle |
| 65 | +} |
| 66 | + |
| 67 | +// Unbundle transforms a bundle into a slice of options, optionally deduplicating |
| 68 | +func (db *DeleteBundle) Unbundle(deduplicate bool) ([]option.DeleteOptioner, error) { |
| 69 | + |
| 70 | + options, err := db.unbundle() |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + if !deduplicate { |
| 76 | + return options, nil |
| 77 | + } |
| 78 | + |
| 79 | + // iterate backwards and make dedup slice |
| 80 | + optionsSet := make(map[reflect.Type]struct{}) |
| 81 | + |
| 82 | + for i := len(options) - 1; i >= 0; i-- { |
| 83 | + currOption := options[i] |
| 84 | + optionType := reflect.TypeOf(currOption) |
| 85 | + |
| 86 | + if _, ok := optionsSet[optionType]; ok { |
| 87 | + // option already found |
| 88 | + options = append(options[:i], options[i+1:]...) |
| 89 | + continue |
| 90 | + } |
| 91 | + |
| 92 | + optionsSet[optionType] = struct{}{} |
| 93 | + } |
| 94 | + |
| 95 | + return options, nil |
| 96 | +} |
| 97 | + |
| 98 | +// Calculates the total length of a bundle, accounting for nested bundles. |
| 99 | +func (db *DeleteBundle) bundleLength() int { |
| 100 | + if db == nil { |
| 101 | + return 0 |
| 102 | + } |
| 103 | + |
| 104 | + bundleLen := 0 |
| 105 | + for ; db != nil && db.option != nil; db = db.next { |
| 106 | + if converted, ok := db.option.(*DeleteBundle); ok { |
| 107 | + // nested bundle |
| 108 | + bundleLen += converted.bundleLength() |
| 109 | + continue |
| 110 | + } |
| 111 | + |
| 112 | + bundleLen++ |
| 113 | + } |
| 114 | + |
| 115 | + return bundleLen |
| 116 | +} |
| 117 | + |
| 118 | +// Helper that recursively unwraps bundle into slice of options |
| 119 | +func (db *DeleteBundle) unbundle() ([]option.DeleteOptioner, error) { |
| 120 | + if db == nil { |
| 121 | + return nil, nil |
| 122 | + } |
| 123 | + |
| 124 | + listLen := db.bundleLength() |
| 125 | + |
| 126 | + options := make([]option.DeleteOptioner, listLen) |
| 127 | + index := listLen - 1 |
| 128 | + |
| 129 | + for listHead := db; listHead != nil && listHead.option != nil; listHead = listHead.next { |
| 130 | + // if the current option is a nested bundle, Unbundle it and add its options to the current array |
| 131 | + if converted, ok := listHead.option.(*DeleteBundle); ok { |
| 132 | + nestedOptions, err := converted.unbundle() |
| 133 | + if err != nil { |
| 134 | + return nil, err |
| 135 | + } |
| 136 | + |
| 137 | + // where to start inserting nested options |
| 138 | + startIndex := index - len(nestedOptions) + 1 |
| 139 | + |
| 140 | + // add nested options in order |
| 141 | + for _, nestedOp := range nestedOptions { |
| 142 | + options[startIndex] = nestedOp |
| 143 | + startIndex++ |
| 144 | + } |
| 145 | + index -= len(nestedOptions) |
| 146 | + continue |
| 147 | + } |
| 148 | + |
| 149 | + options[index] = listHead.option.ConvertOption() |
| 150 | + index-- |
| 151 | + } |
| 152 | + |
| 153 | + return options, nil |
| 154 | +} |
| 155 | + |
| 156 | +// String implements the Stringer interface |
| 157 | +func (db *DeleteBundle) String() string { |
| 158 | + if db == nil { |
| 159 | + return "" |
| 160 | + } |
| 161 | + |
| 162 | + str := "" |
| 163 | + for head := db; head != nil && head.option != nil; head = head.next { |
| 164 | + if converted, ok := head.option.(*DeleteBundle); ok { |
| 165 | + str += converted.String() |
| 166 | + continue |
| 167 | + } |
| 168 | + |
| 169 | + str += head.option.ConvertOption().String() + "\n" |
| 170 | + } |
| 171 | + |
| 172 | + return str |
| 173 | +} |
| 174 | + |
| 175 | +// Collation specifies a collation. |
| 176 | +func Collation(c *option.Collation) OptCollation { |
| 177 | + return OptCollation{Collation: c} |
| 178 | +} |
| 179 | + |
| 180 | +// WriteConcern specifies a write concern. |
| 181 | +func WriteConcern(wc *writeconcern.WriteConcern) OptWriteConcern { |
| 182 | + return OptWriteConcern{wc} |
| 183 | +} |
| 184 | + |
| 185 | +// OptCollation specifies a collation. |
| 186 | +type OptCollation option.OptCollation |
| 187 | + |
| 188 | +func (OptCollation) delete() {} |
| 189 | + |
| 190 | +// ConvertOption implements the Delete interface. |
| 191 | +func (opt OptCollation) ConvertOption() option.DeleteOptioner { |
| 192 | + return option.OptCollation(opt) |
| 193 | +} |
| 194 | + |
| 195 | +// OptWriteConcern specifies a write concern. |
| 196 | +type OptWriteConcern option.OptWriteConcern |
| 197 | + |
| 198 | +func (OptWriteConcern) delete() {} |
| 199 | + |
| 200 | +// ConvertOption implements the Delete interface. |
| 201 | +func (opt OptWriteConcern) ConvertOption() option.DeleteOptioner { |
| 202 | + return option.OptWriteConcern(opt) |
| 203 | +} |
0 commit comments