-
Notifications
You must be signed in to change notification settings - Fork 883
/
listcollectionopt.go
227 lines (180 loc) · 5.75 KB
/
listcollectionopt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package listcollectionopt
import (
"reflect"
"github.com/mongodb/mongo-go-driver/core/option"
"github.com/mongodb/mongo-go-driver/core/session"
)
var listCollectionsBundle = new(ListCollectionsBundle)
// ListCollections represents all possible params for the listCollections() function.
type ListCollections interface {
listCollections()
}
// ListCollectionsOption represents the options for the listCollections() function.
type ListCollectionsOption interface {
ListCollections
ConvertListCollectionsOption() option.ListCollectionsOptioner
}
// ListCollectionsSession is the session for the ListCollections() function.
type ListCollectionsSession interface {
ListCollections
ConvertListCollectionsSession() *session.Client
}
// ListCollectionsBundle is a bundle of ListCollections options
type ListCollectionsBundle struct {
option ListCollections
next *ListCollectionsBundle
}
// Implement the ListCollections interface
func (lcb *ListCollectionsBundle) listCollections() {}
// ConvertListCollectionsOption implements the ListCollections interface
func (lcb *ListCollectionsBundle) ConvertListCollectionsOption() option.ListCollectionsOptioner {
return nil
}
// BundleListCollections bundles ListCollections options
func BundleListCollections(opts ...ListCollections) *ListCollectionsBundle {
head := listCollectionsBundle
for _, opt := range opts {
newBundle := ListCollectionsBundle{
option: opt,
next: head,
}
head = &newBundle
}
return head
}
// NameOnly adds an option to specify whether to return only the collection names.
func (lcb *ListCollectionsBundle) NameOnly(b bool) *ListCollectionsBundle {
bundle := &ListCollectionsBundle{
option: NameOnly(b),
next: lcb,
}
return bundle
}
// Unbundle transforms a bundle into a slice of options, optionally deduplicating.
func (lcb *ListCollectionsBundle) Unbundle(deduplicate bool) ([]option.ListCollectionsOptioner, *session.Client, error) {
options, sess, err := lcb.unbundle()
if err != nil {
return nil, nil, err
}
if !deduplicate {
return options, sess, nil
}
// iterate backwards and make dedup slice
optionsSet := make(map[reflect.Type]struct{})
for i := len(options) - 1; i >= 0; i-- {
currOption := options[i]
optionType := reflect.TypeOf(currOption)
if _, ok := optionsSet[optionType]; ok {
// option already found
options = append(options[:i], options[i+1:]...)
continue
}
optionsSet[optionType] = struct{}{}
}
return options, sess, nil
}
// Calculates the total length of a bundle, accounting for nested bundles.
func (lcb *ListCollectionsBundle) bundleLength() int {
if lcb == nil {
return 0
}
bundleLen := 0
for ; lcb != nil; lcb = lcb.next {
if lcb.option == nil {
continue
}
if converted, ok := lcb.option.(*ListCollectionsBundle); ok {
// nested bundle
bundleLen += converted.bundleLength()
continue
}
if _, ok := lcb.option.(ListCollectionsSessionOpt); !ok {
bundleLen++
}
}
return bundleLen
}
// Helper that recursively unwraps bundle into slice of options
func (lcb *ListCollectionsBundle) unbundle() ([]option.ListCollectionsOptioner, *session.Client, error) {
if lcb == nil {
return nil, nil, nil
}
var sess *session.Client
listLen := lcb.bundleLength()
options := make([]option.ListCollectionsOptioner, listLen)
index := listLen - 1
for listHead := lcb; listHead != nil; listHead = listHead.next {
if listHead.option == nil {
continue
}
// if the current option is a nested bundle, Unbundle it and add its options to the current array
if converted, ok := listHead.option.(*ListCollectionsBundle); ok {
nestedOptions, s, err := converted.unbundle()
if err != nil {
return nil, nil, err
}
if s != nil && sess == nil {
sess = s
}
// where to start inserting nested options
startIndex := index - len(nestedOptions) + 1
// add nested options in order
for _, nestedOp := range nestedOptions {
options[startIndex] = nestedOp
startIndex++
}
index -= len(nestedOptions)
continue
}
switch t := listHead.option.(type) {
case ListCollectionsOption:
options[index] = t.ConvertListCollectionsOption()
index--
case ListCollectionsSession:
if sess == nil {
sess = t.ConvertListCollectionsSession()
}
}
}
return options, sess, nil
}
// String implements the Stringer interface
func (lcb *ListCollectionsBundle) String() string {
if lcb == nil {
return ""
}
str := ""
for head := lcb; head != nil && head.option != nil; head = head.next {
if converted, ok := head.option.(*ListCollectionsBundle); ok {
str += converted.String()
continue
}
if conv, ok := head.option.(ListCollectionsOption); !ok {
str += conv.ConvertListCollectionsOption().String() + "\n"
}
}
return str
}
// NameOnly specifies whether to return only the collection names.
func NameOnly(b bool) OptNameOnly {
return OptNameOnly(b)
}
// OptNameOnly specifies whether to return only the collection names.
type OptNameOnly option.OptNameOnly
func (OptNameOnly) listCollections() {}
// ConvertListCollectionsOption implements the ListCollections interface.
func (opt OptNameOnly) ConvertListCollectionsOption() option.ListCollectionsOptioner {
return option.OptNameOnly(opt)
}
// ListCollectionsSessionOpt is a listCollections session option.
type ListCollectionsSessionOpt struct{}
func (ListCollectionsSessionOpt) listCollections() {}
// ConvertListCollectionsSession implements the ListCollectionsSession interface.
func (ListCollectionsSessionOpt) ConvertListCollectionsSession() *session.Client {
return nil
}