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