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
7package options
8
9import "time"
10
11// DistinctOptions represents options that can be used to configure a Distinct operation.
12type DistinctOptions struct {
13	// Specifies a collation to use for string comparisons during the operation. This option is only valid for MongoDB
14	// versions >= 3.4. For previous server versions, the driver will return an error if this option is used. The
15	// default value is nil, which means the default collation of the collection will be used.
16	Collation *Collation
17
18	// The maximum amount of time that the query can run on the server. The default value is nil, meaning that there
19	// is no time limit for query execution.
20	MaxTime *time.Duration
21}
22
23// Distinct creates a new DistinctOptions instance.
24func Distinct() *DistinctOptions {
25	return &DistinctOptions{}
26}
27
28// SetCollation sets the value for the Collation field.
29func (do *DistinctOptions) SetCollation(c *Collation) *DistinctOptions {
30	do.Collation = c
31	return do
32}
33
34// SetMaxTime sets the value for the MaxTime field.
35func (do *DistinctOptions) SetMaxTime(d time.Duration) *DistinctOptions {
36	do.MaxTime = &d
37	return do
38}
39
40// MergeDistinctOptions combines the given DistinctOptions instances into a single DistinctOptions in a last-one-wins
41// fashion.
42func MergeDistinctOptions(opts ...*DistinctOptions) *DistinctOptions {
43	distinctOpts := Distinct()
44	for _, do := range opts {
45		if do == nil {
46			continue
47		}
48		if do.Collation != nil {
49			distinctOpts.Collation = do.Collation
50		}
51		if do.MaxTime != nil {
52			distinctOpts.MaxTime = do.MaxTime
53		}
54	}
55
56	return distinctOpts
57}
58