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 | import "time" |
| 10 | |
| 11 | // DistinctOptions represents all possible options to the distinct() function |
| 12 | type DistinctOptions struct { |
| 13 | Collation *Collation // Specifies a collation |
| 14 | MaxTime *time.Duration // The maximum amount of time to allow the operation to run |
| 15 | } |
| 16 | |
| 17 | // Distinct returns a pointer to a new DistinctOptions |
| 18 | func Distinct() *DistinctOptions { |
| 19 | return &DistinctOptions{} |
| 20 | } |
| 21 | |
| 22 | // SetCollation specifies a collation |
| 23 | // Valid for server versions >= 3.4 |
| 24 | func (do *DistinctOptions) SetCollation(c *Collation) *DistinctOptions { |
| 25 | do.Collation = c |
| 26 | return do |
| 27 | } |
| 28 | |
| 29 | // SetMaxTime specifies the maximum amount of time to allow the operation to run |
| 30 | func (do *DistinctOptions) SetMaxTime(d time.Duration) *DistinctOptions { |
| 31 | do.MaxTime = &d |
| 32 | return do |
| 33 | } |
| 34 | |
| 35 | // MergeDistinctOptions combines the argued DistinctOptions into a single DistinctOptions in a last-one-wins fashion |
| 36 | func MergeDistinctOptions(opts ...*DistinctOptions) *DistinctOptions { |
| 37 | distinctOpts := Distinct() |
| 38 | for _, do := range opts { |
| 39 | if do == nil { |
| 40 | continue |
| 41 | } |
| 42 | if do.Collation != nil { |
| 43 | distinctOpts.Collation = do.Collation |
| 44 | } |
| 45 | if do.MaxTime != nil { |
| 46 | distinctOpts.MaxTime = do.MaxTime |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | return distinctOpts |
| 51 | } |