-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoption.go
291 lines (245 loc) · 9.03 KB
/
option.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dbw
import (
"github.com/hashicorp/go-hclog"
)
// GetOpts - iterate the inbound Options and return a struct.
func GetOpts(opt ...Option) Options {
opts := getDefaultOptions()
for _, o := range opt {
if o != nil {
o(&opts)
}
}
return opts
}
// Option - how Options are passed as arguments.
type Option func(*Options)
// Options - how Options are represented which have been set via an Option
// function. Use GetOpts(...) to populated this struct with the options that
// have been specified for an operation. All option fields are exported so
// they're available for use by other packages.
type Options struct {
// WithBeforeWrite provides and option to provide a func to be called before a
// write operation. The i interface{} passed at runtime will be the resource(s)
// being written.
WithBeforeWrite func(i interface{}) error
// WithAfterWrite provides and option to provide a func to be called after a
// write operation. The i interface{} passed at runtime will be the resource(s)
// being written.
WithAfterWrite func(i interface{}, rowsAffected int) error
// WithLookup enables a lookup after a write operation.
WithLookup bool
// WithLimit provides an option to provide a limit. Intentionally allowing
// negative integers. If WithLimit < 0, then unlimited results are returned.
// If WithLimit == 0, then default limits are used for results (see DefaultLimit
// const).
WithLimit int
// WithFieldMaskPaths provides an option to provide field mask paths for update
// operations.
WithFieldMaskPaths []string
// WithNullPaths provides an option to provide null paths for update
// operations.
WithNullPaths []string
// WithVersion provides an option version number for update operations. Using
// this option requires that your resource has a version column that's
// incremented for every successful update operation. Version provides an
// optimistic locking mechanism for write operations.
WithVersion *uint32
WithSkipVetForWrite bool
// WithWhereClause provides an option to provide a where clause for an
// operation.
WithWhereClause string
// WithWhereClauseArgs provides an option to provide a where clause arguments for an
// operation.
WithWhereClauseArgs []interface{}
// WithOrder provides an option to provide an order when searching and looking
// up.
WithOrder string
// WithPrngValues provides an option to provide values to seed an PRNG when generating IDs
WithPrngValues []string
// WithLogger specifies an optional hclog to use for db operations. It's only
// valid for Open(..) and OpenWith(...) The logger provided can optionally
// implement the LogWriter interface as well which would override the default
// behavior for a logger (the default only emits postgres errors)
WithLogger hclog.Logger
// WithMinOpenConnections specifies and optional min open connections for the
// database. A value of zero means that there is no min.
WithMaxOpenConnections int
// WithMaxOpenConnections specifies and optional max open connections for the
// database. A value of zero equals unlimited connections
WithMinOpenConnections int
// WithDebug indicates that the given operation should invoke debug output
// mode
WithDebug bool
// WithOnConflict specifies an optional on conflict criteria which specify
// alternative actions to take when an insert results in a unique constraint or
// exclusion constraint error
WithOnConflict *OnConflict
// WithRowsAffected specifies an option for returning the rows affected
// and typically used with "bulk" write operations.
WithRowsAffected *int64
// WithTable specifies an option for setting a table name to use for the
// operation.
WithTable string
// WithBatchSize specifies an option for setting the batch size for bulk
// operations. If WithBatchSize == 0, then the default batch size is used.
WithBatchSize int
withLogLevel LogLevel
}
func getDefaultOptions() Options {
return Options{
WithFieldMaskPaths: []string{},
WithNullPaths: []string{},
WithBatchSize: DefaultBatchSize,
withLogLevel: Error,
}
}
// WithBeforeWrite provides and option to provide a func to be called before a
// write operation. The i interface{} passed at runtime will be the resource(s)
// being written.
func WithBeforeWrite(fn func(i interface{}) error) Option {
return func(o *Options) {
o.WithBeforeWrite = fn
}
}
// WithAfterWrite provides and option to provide a func to be called after a
// write operation. The i interface{} passed at runtime will be the resource(s)
// being written.
func WithAfterWrite(fn func(i interface{}, rowsAffected int) error) Option {
return func(o *Options) {
o.WithAfterWrite = fn
}
}
// WithLookup enables a lookup after a write operation.
func WithLookup(enable bool) Option {
return func(o *Options) {
o.WithLookup = enable
}
}
// WithFieldMaskPaths provides an option to provide field mask paths for update
// operations.
func WithFieldMaskPaths(paths []string) Option {
return func(o *Options) {
o.WithFieldMaskPaths = paths
}
}
// WithNullPaths provides an option to provide null paths for update operations.
func WithNullPaths(paths []string) Option {
return func(o *Options) {
o.WithNullPaths = paths
}
}
// WithLimit provides an option to provide a limit. Intentionally allowing
// negative integers. If WithLimit < 0, then unlimited results are returned.
// If WithLimit == 0, then default limits are used for results (see DefaultLimit
// const).
func WithLimit(limit int) Option {
return func(o *Options) {
o.WithLimit = limit
}
}
// WithVersion provides an option version number for update operations. Using
// this option requires that your resource has a version column that's
// incremented for every successful update operation. Version provides an
// optimistic locking mechanism for write operations.
func WithVersion(version *uint32) Option {
return func(o *Options) {
o.WithVersion = version
}
}
// WithSkipVetForWrite provides an option to allow skipping vet checks to allow
// testing lower-level SQL triggers and constraints
func WithSkipVetForWrite(enable bool) Option {
return func(o *Options) {
o.WithSkipVetForWrite = enable
}
}
// WithWhere provides an option to provide a where clause with arguments for an
// operation.
func WithWhere(whereClause string, args ...interface{}) Option {
return func(o *Options) {
o.WithWhereClause = whereClause
o.WithWhereClauseArgs = append(o.WithWhereClauseArgs, args...)
}
}
// WithOrder provides an option to provide an order when searching and looking
// up.
func WithOrder(withOrder string) Option {
return func(o *Options) {
o.WithOrder = withOrder
}
}
// WithPrngValues provides an option to provide values to seed an PRNG when generating IDs
func WithPrngValues(withPrngValues []string) Option {
return func(o *Options) {
o.WithPrngValues = withPrngValues
}
}
// WithLogger specifies an optional hclog to use for db operations. It's only
// valid for Open(..) and OpenWith(...). The logger provided can optionally
// implement the LogWriter interface as well which would override the default
// behavior for a logger (the default only emits postgres errors)
func WithLogger(l hclog.Logger) Option {
return func(o *Options) {
o.WithLogger = l
}
}
// WithMaxOpenConnections specifies and optional max open connections for the
// database. A value of zero equals unlimited connections
func WithMaxOpenConnections(max int) Option {
return func(o *Options) {
o.WithMaxOpenConnections = max
}
}
// WithMinOpenConnections specifies and optional min open connections for the
// database. A value of zero means that there is no min.
func WithMinOpenConnections(max int) Option {
return func(o *Options) {
o.WithMinOpenConnections = max
}
}
// WithDebug specifies the given operation should invoke debug mode for the
// database output
func WithDebug(with bool) Option {
return func(o *Options) {
o.WithDebug = with
}
}
// WithOnConflict specifies an optional on conflict criteria which specify
// alternative actions to take when an insert results in a unique constraint or
// exclusion constraint error
func WithOnConflict(onConflict *OnConflict) Option {
return func(o *Options) {
o.WithOnConflict = onConflict
}
}
// WithReturnRowsAffected specifies an option for returning the rows affected
// and typically used with "bulk" write operations.
func WithReturnRowsAffected(rowsAffected *int64) Option {
return func(o *Options) {
o.WithRowsAffected = rowsAffected
}
}
// WithTable specifies an option for setting a table name to use for the
// operation.
func WithTable(name string) Option {
return func(o *Options) {
o.WithTable = name
}
}
// WithLogLevel specifies an option for setting the log level
func WithLogLevel(l LogLevel) Option {
return func(o *Options) {
o.withLogLevel = l
}
}
// WithBatchSize specifies an option for setting the batch size for bulk
// operations like CreateItems. If WithBatchSize == 0, the default batch size is
// used (see DefaultBatchSize const).
func WithBatchSize(size int) Option {
return func(o *Options) {
o.WithBatchSize = size
}
}