-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontext.go
340 lines (279 loc) · 10.7 KB
/
context.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2019 Aporeto Inc.
// 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
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package manipulate
import (
"context"
"fmt"
"net/url"
"go.aporeto.io/elemental"
)
// ReadConsistency represents the desired consistency of the request.
// Not all driver may implement this.
type ReadConsistency string
// Various values for Consistency
const (
ReadConsistencyDefault ReadConsistency = "default"
ReadConsistencyNearest ReadConsistency = "nearest"
ReadConsistencyEventual ReadConsistency = "eventual"
ReadConsistencyMonotonic ReadConsistency = "monotonic"
ReadConsistencyStrong ReadConsistency = "strong"
ReadConsistencyWeakest ReadConsistency = "weakest"
)
// WriteConsistency represents the desired consistency of the request.
// Not all driver may implement this.
type WriteConsistency string
// Various values for Consistency
const (
WriteConsistencyDefault WriteConsistency = "default"
WriteConsistencyNone WriteConsistency = "none"
WriteConsistencyStrong WriteConsistency = "strong"
WriteConsistencyStrongest WriteConsistency = "strongest"
)
// A FinalizerFunc is the type of a function that can be used as a creation finalizer.
// This is only supported by manipulators that generate an ID to let a chance to the user.
// to now the intended ID before actually creating the object.
type FinalizerFunc func(o elemental.Identifiable) error
// A RetryFunc is a function that can be called during an
// auto retry.
// The current manipulate.Context is given, a Stringer interface containing,
// more info about the current request, the error that
// caused the retry and the try number.
// If this function returns an error, the retry procedure will
// be interupted and this error will be returns to the caller
// of the operation.
type RetryFunc func(RetryInfo) error
// A RetryInfo is the interface that can be passed to RetryFunc
// that will contain retry information. Content will depend on
// the manipulator implementation.
type RetryInfo interface {
Err() error
Context() Context
Try() int
}
// A Context holds all information regarding a particular manipulate operation.
type Context interface {
Count() int
SetCount(count int)
Filter() *elemental.Filter
Finalizer() FinalizerFunc
Version() int
TransactionID() TransactionID
Page() int
PageSize() int
After() string
Limit() int
Next() string
SetNext(string)
Override() bool
Recursive() bool
Namespace() string
Propagated() bool
Credentials() (string, string)
Parameters() url.Values
Parent() elemental.Identifiable
ExternalTrackingID() string
ExternalTrackingType() string
Order() []string
Context() context.Context
Derive(...ContextOption) Context
Fields() []string
ReadConsistency() ReadConsistency
WriteConsistency() WriteConsistency
Messages() []string
SetMessages([]string)
ClientIP() string
RetryFunc() RetryFunc
RetryRatio() int64
fmt.Stringer
}
type mcontext struct {
clientIP string
countTotal int
createFinalizer FinalizerFunc
ctx context.Context
externalTrackingID string
externalTrackingType string
fields []string
filter *elemental.Filter
idempotencyKey string
messages []string
namespace string
propagated bool
order []string
overrideProtection bool
page int
pageSize int
after string
limit int
next string
parameters url.Values
parent elemental.Identifiable
password string
readConsistency ReadConsistency
recursive bool
retryFunc RetryFunc
retryRatio int64
transactionID TransactionID
username string
version int
writeConsistency WriteConsistency
opaque map[string]any
}
// NewContext creates a context with the given ContextOption.
func NewContext(ctx context.Context, options ...ContextOption) Context {
if ctx == nil {
panic("nil context")
}
mctx := &mcontext{
ctx: ctx,
writeConsistency: WriteConsistencyDefault,
readConsistency: ReadConsistencyDefault,
retryRatio: 4,
opaque: map[string]any{},
}
for _, opt := range options {
opt(mctx)
}
return mctx
}
// Derive creates a copy of the context but updates the values of the given options.
// Values that are parts of a response like Count or Messages or IdempotencyKey
// are reset for the derived context.
func (c *mcontext) Derive(options ...ContextOption) Context {
var opaqueCopy map[string]any
if len(c.opaque) > 0 {
opaqueCopy = make(map[string]any, len(c.opaque))
for k, v := range c.opaque {
opaqueCopy[k] = v
}
}
var paramsCopy url.Values
if len(c.parameters) > 0 {
paramsCopy = url.Values{}
for k, v := range c.parameters {
paramsCopy[k] = v
}
}
copied := &mcontext{
clientIP: c.clientIP,
createFinalizer: c.createFinalizer,
ctx: c.ctx,
externalTrackingID: c.externalTrackingID,
externalTrackingType: c.externalTrackingType,
fields: append([]string{}, c.fields...),
filter: c.filter,
namespace: c.namespace,
propagated: c.propagated,
order: append([]string{}, c.order...),
overrideProtection: c.overrideProtection,
page: c.page,
pageSize: c.pageSize,
after: c.after,
limit: c.limit,
parameters: paramsCopy,
parent: c.parent,
password: c.password,
readConsistency: c.readConsistency,
recursive: c.recursive,
retryFunc: c.retryFunc,
retryRatio: c.retryRatio,
transactionID: c.transactionID,
username: c.username,
version: c.version,
writeConsistency: c.writeConsistency,
opaque: opaqueCopy,
}
for _, opt := range options {
opt(copied)
}
return copied
}
// Count returns the count
func (c *mcontext) Count() int { return c.countTotal }
// SetCount returns the total count.
func (c *mcontext) SetCount(count int) { c.countTotal = count }
// Filter returns the filter.
func (c *mcontext) Filter() *elemental.Filter { return c.filter }
// Finalizer returns the finalizer.
func (c *mcontext) Finalizer() FinalizerFunc { return c.createFinalizer }
// Version returns the version.
func (c *mcontext) Version() int { return c.version }
// TransactionID returns the transactionID.
func (c *mcontext) TransactionID() TransactionID { return c.transactionID }
// Page returns the page number.
func (c *mcontext) Page() int { return c.page }
// PageSize returns the pageSize.
func (c *mcontext) PageSize() int { return c.pageSize }
// After returns the after parameter used in lazy pagination.
func (c *mcontext) After() string { return c.after }
// Limit returns the limit parameter used in lazy pagination.
func (c *mcontext) Limit() int { return c.limit }
// After returns the after parameter used in lazy pagination.
func (c *mcontext) Next() string { return c.next }
// After returns the after parameter used in lazy pagination.
func (c *mcontext) SetNext(next string) { c.next = next }
// Override returns the override value.
func (c *mcontext) Override() bool { return c.overrideProtection }
// Recursive returns the recursive value.
func (c *mcontext) Recursive() bool { return c.recursive }
// Namespace returns the namespace value.
func (c *mcontext) Namespace() string { return c.namespace }
// Propagated returns the propagate value
func (c *mcontext) Propagated() bool { return c.propagated }
// Parameters returns the parameters.
func (c *mcontext) Parameters() url.Values { return c.parameters }
// Parent returns the parent.
func (c *mcontext) Parent() elemental.Identifiable { return c.parent }
// ExternalTrackingID returns the ExternalTrackingID.
func (c *mcontext) ExternalTrackingID() string { return c.externalTrackingID }
// ExternalTrackingType returns the ExternalTrackingType.
func (c *mcontext) ExternalTrackingType() string { return c.externalTrackingType }
// Order returns the Order.
func (c *mcontext) Order() []string { return c.order }
// Fields returns the fields.
func (c *mcontext) Fields() []string { return c.fields }
// WriteConsistency returns the desired write consistency.
func (c *mcontext) WriteConsistency() WriteConsistency { return c.writeConsistency }
// ReadConsistency returns the desired read consistency.
func (c *mcontext) ReadConsistency() ReadConsistency { return c.readConsistency }
// Messages returns the eventual list of messages regarding a manipulation.
func (c *mcontext) Messages() []string { return c.messages }
// SetMessages sets the message in the context.
// You should not need to use this.
func (c *mcontext) SetMessages(messages []string) { c.messages = messages }
// Context returns the internal context.Context.
func (c *mcontext) Context() context.Context { return c.ctx }
// IdempotencyKey returns the idempotency key.
func (c *mcontext) IdempotencyKey() string { return c.idempotencyKey }
// SetIdempotencyKey sets the IdempotencyKey. This is used internally
// by manipulator implementation supporting it.
func (c *mcontext) SetIdempotencyKey(k string) { c.idempotencyKey = k }
// DelegationToken returns any delegation token provided by options.
func (c *mcontext) Credentials() (string, string) { return c.username, c.password }
// Headers returns the optional headers.
func (c *mcontext) ClientIP() string { return c.clientIP }
// RetryRatio returns the context retry ratio.
func (c *mcontext) RetryRatio() int64 { return c.retryRatio }
// RetryFunc returns the retry function that is called when a retry occurs.
// If this function returns an error, retrying stops and the returned error
// returned by the manipulate operation.
func (c *mcontext) RetryFunc() RetryFunc { return c.retryFunc }
// Opaque returns the context opaque data.
func (c *mcontext) Opaque() map[string]any { return c.opaque }
// SetDelegationToken sets the delegation token for this context.
func (c *mcontext) SetCredentials(username, password string) {
c.username = username
c.password = password
}
// String returns the string representation of the Context.
func (c *mcontext) String() string {
return fmt.Sprintf("<Context page:%d pagesize:%d filter:%v version:%d>", c.page, c.pageSize, c.filter, c.version)
}