forked from couchbase/gocb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults.go
336 lines (280 loc) · 7.71 KB
/
results.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
package gocb
import (
"encoding/json"
gocbcore "github.com/couchbase/gocbcore/v8"
"github.com/pkg/errors"
)
// Result is the base type for the return types of operations
type Result struct {
cas Cas
}
// Cas returns the cas of the result.
func (d *Result) Cas() Cas {
return d.cas
}
// GetResult is the return type of Get operations.
type GetResult struct {
Result
transcoder Transcoder
flags uint32
contents []byte
expiry *uint32
}
// Content assigns the value of the result into the valuePtr using default decoding.
func (d *GetResult) Content(valuePtr interface{}) error {
return d.transcoder.Decode(d.contents, d.flags, valuePtr)
}
// Expiry returns the expiry value for the result.
func (d *GetResult) Expiry() *uint32 {
return d.expiry
}
func (d *GetResult) fromFullProjection(ops []LookupInSpec, result *LookupInResult, fields []string) error {
if fields == nil || len(fields) == 0 {
// This is a special case where user specified a full doc fetch with expiration.
d.contents = result.contents[0].data
return nil
}
if len(result.contents) != 1 {
return makeInvalidArgumentsError("fromFullProjection should only be called with 1 subdoc result")
}
resultContent := result.contents[0]
if resultContent.err != nil {
return resultContent.err
}
var content map[string]interface{}
err := json.Unmarshal(resultContent.data, &content)
if err != nil {
return err
}
newContent := make(map[string]interface{})
for _, field := range fields {
parts := d.pathParts(field)
d.set(parts, newContent, content[field])
}
bytes, err := json.Marshal(newContent)
if err != nil {
return errors.Wrap(err, "could not marshal result contents")
}
d.contents = bytes
return nil
}
func (d *GetResult) fromSubDoc(ops []LookupInSpec, result *LookupInResult) error {
content := make(map[string]interface{})
for i, op := range ops {
err := result.contents[i].err
if err != nil {
// We return the first error that has occured, this will be
// a SubDocument error and will indicate the real reason.
return err
}
parts := d.pathParts(op.path)
d.set(parts, content, result.contents[i].data)
}
bytes, err := json.Marshal(content)
if err != nil {
return errors.Wrap(err, "could not marshal result contents")
}
d.contents = bytes
return nil
}
type subdocPath struct {
path string
elem int
isArray bool
}
func (d *GetResult) pathParts(pathStr string) []subdocPath {
pathLen := len(pathStr)
var elemIdx int
var i int
var paths []subdocPath
for i < pathLen {
ch := pathStr[i]
i++
if ch == '[' {
// opening of an array
isArr := false
arrayStart := i
for i < pathLen {
arrCh := pathStr[i]
if arrCh == ']' {
isArr = true
i++
break
} else if arrCh == '.' {
i++
break
}
i++
}
if isArr {
paths = append(paths, subdocPath{path: pathStr[elemIdx : arrayStart-1], isArray: true})
} else {
paths = append(paths, subdocPath{path: pathStr[elemIdx:i], isArray: false})
}
elemIdx = i
if i < pathLen && pathStr[i] == '.' {
i++
elemIdx = i
}
} else if ch == '.' {
paths = append(paths, subdocPath{path: pathStr[elemIdx : i-1]})
elemIdx = i
}
}
if elemIdx != i {
// this should only ever be an object as an array would have ended in [...]
paths = append(paths, subdocPath{path: pathStr[elemIdx:i]})
}
return paths
}
func (d *GetResult) set(paths []subdocPath, content interface{}, value interface{}) interface{} {
path := paths[0]
if len(paths) == 1 {
if path.isArray {
arr := make([]interface{}, 0)
arr = append(arr, value)
content.(map[string]interface{})[path.path] = arr
} else {
if _, ok := content.([]interface{}); ok {
elem := make(map[string]interface{})
elem[path.path] = value
content = append(content.([]interface{}), elem)
} else {
content.(map[string]interface{})[path.path] = value
}
}
return content
}
if path.isArray {
if cMap, ok := content.(map[string]interface{}); ok {
cMap[path.path] = make([]interface{}, 0)
cMap[path.path] = d.set(paths[1:], cMap[path.path], value)
return content
}
} else {
if arr, ok := content.([]interface{}); ok {
m := make(map[string]interface{})
m[path.path] = make(map[string]interface{})
content = append(arr, m)
d.set(paths[1:], m[path.path], value)
return content
}
cMap, ok := content.(map[string]interface{})
if !ok {
// this isn't possible but the linter won't play nice without it
}
cMap[path.path] = make(map[string]interface{})
return d.set(paths[1:], cMap[path.path], value)
}
return content
}
// LookupInResult is the return type for LookupIn.
type LookupInResult struct {
Result
contents []lookupInPartial
pathMap map[string]int
}
type lookupInPartial struct {
data json.RawMessage
err error
}
func (pr *lookupInPartial) as(valuePtr interface{}) error {
if pr.err != nil {
return pr.err
}
if valuePtr == nil {
return nil
}
if valuePtr, ok := valuePtr.(*[]byte); ok {
*valuePtr = pr.data
return nil
}
return json.Unmarshal(pr.data, valuePtr)
}
func (pr *lookupInPartial) exists() bool {
err := pr.as(nil)
return err == nil
}
// ContentAt retrieves the value of the operation by its index. The index is the position of
// the operation as it was added to the builder.
func (lir *LookupInResult) ContentAt(idx int, valuePtr interface{}) error {
if idx >= len(lir.contents) {
return makeInvalidArgumentsError("invalid index")
}
return lir.contents[idx].as(valuePtr)
}
// Exists verifies that the item at idx exists.
func (lir *LookupInResult) Exists(idx int) bool {
if idx >= len(lir.contents) {
return false
}
return lir.contents[idx].exists()
}
// ExistsResult is the return type of Exist operations.
type ExistsResult struct {
Result
keyState gocbcore.KeyState
}
// Exists returns whether or not the document exists.
func (d *ExistsResult) Exists() bool {
return d.keyState != gocbcore.KeyStateNotFound && d.keyState != gocbcore.KeyStateDeleted
}
// MutationResult is the return type of any store related operations. It contains Cas and mutation tokens.
type MutationResult struct {
Result
mt *MutationToken
}
// MutationToken returns the mutation token belonging to an operation.
func (mr MutationResult) MutationToken() *MutationToken {
return mr.mt
}
// MutateInResult is the return type of any mutate in related operations.
// It contains Cas, mutation tokens and any returned content.
type MutateInResult struct {
MutationResult
contents []mutateInPartial
}
type mutateInPartial struct {
data json.RawMessage
}
func (pr *mutateInPartial) as(valuePtr interface{}) error {
if valuePtr == nil {
return nil
}
if valuePtr, ok := valuePtr.(*[]byte); ok {
*valuePtr = pr.data
return nil
}
return json.Unmarshal(pr.data, valuePtr)
}
// ContentAt retrieves the value of the operation by its index. The index is the position of
// the operation as it was added to the builder.
func (mir MutateInResult) ContentAt(idx int, valuePtr interface{}) error {
return mir.contents[idx].as(valuePtr)
}
// CounterResult is the return type of counter operations.
type CounterResult struct {
MutationResult
content uint64
}
// MutationToken returns the mutation token belonging to an operation.
func (mr CounterResult) MutationToken() *MutationToken {
return mr.mt
}
// Cas returns the Cas value for a document following an operation.
func (mr CounterResult) Cas() Cas {
return mr.cas
}
// Content returns the new value for the counter document.
func (mr CounterResult) Content() uint64 {
return mr.content
}
// GetReplicaResult is the return type of GetReplica operations.
type GetReplicaResult struct {
GetResult
isReplica bool
}
// IsReplica returns whether or not this result came from a replica server.
func (r *GetReplicaResult) IsReplica() bool {
return r.isReplica
}