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.

distinctoptions.go 1.5 kB

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