forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathexample_test.go
420 lines (374 loc) · 10.8 KB
/
example_test.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package crud_test
import (
"context"
"fmt"
"reflect"
"time"
"github.com/tarantool/go-tarantool/v2"
"github.com/tarantool/go-tarantool/v2/crud"
)
const (
exampleServer = "127.0.0.1:3013"
exampleSpace = "test"
)
var exampleOpts = tarantool.Opts{
Timeout: 5 * time.Second,
}
var exampleDialer = tarantool.NetDialer{
Address: exampleServer,
User: "test",
Password: "test",
}
func exampleConnect() *tarantool.Connection {
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
conn, err := tarantool.Connect(ctx, exampleDialer, exampleOpts)
if err != nil {
panic("Connection is not established: " + err.Error())
}
return conn
}
// ExampleResult_rowsInterface demonstrates how to use a helper type Result
// to decode a crud response. In this example, rows are decoded as an
// interface{} type.
func ExampleResult_rowsInterface() {
conn := exampleConnect()
req := crud.MakeReplaceRequest(exampleSpace).
Tuple([]interface{}{uint(2010), nil, "bla"})
ret := crud.Result{}
if err := conn.Do(req).GetTyped(&ret); err != nil {
fmt.Printf("Failed to execute request: %s", err)
return
}
fmt.Println(ret.Metadata)
fmt.Println(ret.Rows)
// Output:
// [{id unsigned false} {bucket_id unsigned true} {name string false}]
// [[2010 45 bla]]
}
// ExampleResult_rowsCustomType demonstrates how to use a helper type Result
// to decode a crud response. In this example, rows are decoded as a
// custom type.
func ExampleResult_rowsCustomType() {
conn := exampleConnect()
req := crud.MakeReplaceRequest(exampleSpace).
Tuple([]interface{}{uint(2010), nil, "bla"})
type Tuple struct {
_msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused
Id uint64
BucketId uint64
Name string
}
ret := crud.MakeResult(reflect.TypeOf(Tuple{}))
if err := conn.Do(req).GetTyped(&ret); err != nil {
fmt.Printf("Failed to execute request: %s", err)
return
}
fmt.Println(ret.Metadata)
rows := ret.Rows.([]Tuple)
fmt.Println(rows)
// Output:
// [{id unsigned false} {bucket_id unsigned true} {name string false}]
// [{{} 2010 45 bla}]
}
// ExampleTuples_customType demonstrates how to use a slice of objects of a
// custom type as Tuples to make a ReplaceManyRequest.
func ExampleTuples_customType() {
conn := exampleConnect()
// The type will be encoded/decoded as an array.
type Tuple struct {
_msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused
Id uint64
BucketId *uint64
Name string
}
req := crud.MakeReplaceManyRequest(exampleSpace).Tuples([]Tuple{
Tuple{
Id: 2010,
BucketId: nil,
Name: "bla",
},
})
ret := crud.MakeResult(reflect.TypeOf(Tuple{}))
if err := conn.Do(req).GetTyped(&ret); err != nil {
fmt.Printf("Failed to execute request: %s", err)
return
}
fmt.Println(ret.Metadata)
rows := ret.Rows.([]Tuple)
if len(rows) == 1 {
fmt.Println(rows[0].Id)
fmt.Println(*rows[0].BucketId)
fmt.Println(rows[0].Name)
} else {
fmt.Printf("Unexpected result tuples count: %d", len(rows))
}
// Output:
// [{id unsigned false} {bucket_id unsigned true} {name string false}]
// 2010
// 45
// bla
}
// ExampleObjects_customType demonstrates how to use a slice of objects of
// a custom type as Objects to make a ReplaceObjectManyRequest.
func ExampleObjects_customType() {
conn := exampleConnect()
// The type will be encoded/decoded as a map.
type Tuple struct {
Id uint64 `msgpack:"id,omitempty"`
BucketId *uint64 `msgpack:"bucket_id,omitempty"`
Name string `msgpack:"name,omitempty"`
}
req := crud.MakeReplaceObjectManyRequest(exampleSpace).Objects([]Tuple{
Tuple{
Id: 2010,
BucketId: nil,
Name: "bla",
},
})
ret := crud.MakeResult(reflect.TypeOf(Tuple{}))
if err := conn.Do(req).GetTyped(&ret); err != nil {
fmt.Printf("Failed to execute request: %s", err)
return
}
fmt.Println(ret.Metadata)
rows := ret.Rows.([]Tuple)
if len(rows) == 1 {
fmt.Println(rows[0].Id)
fmt.Println(*rows[0].BucketId)
fmt.Println(rows[0].Name)
} else {
fmt.Printf("Unexpected result tuples count: %d", len(rows))
}
// Output:
// [{id unsigned false} {bucket_id unsigned true} {name string false}]
// 2010
// 45
// bla
}
// ExampleResult_operationData demonstrates how to obtain information
// about erroneous objects from crud.Error using `OperationData` field.
func ExampleResult_operationData() {
conn := exampleConnect()
req := crud.MakeInsertObjectManyRequest(exampleSpace).Objects([]crud.Object{
crud.MapObject{
"id": 2,
"bucket_id": 3,
"name": "Makar",
},
crud.MapObject{
"id": 2,
"bucket_id": 3,
"name": "Vasya",
},
crud.MapObject{
"id": 3,
"bucket_id": 5,
},
})
ret := crud.Result{}
if err := conn.Do(req).GetTyped(&ret); err != nil {
crudErrs := err.(crud.ErrorMany)
fmt.Println("Erroneous data:")
for _, crudErr := range crudErrs.Errors {
fmt.Println(crudErr.OperationData)
}
} else {
fmt.Println(ret.Metadata)
fmt.Println(ret.Rows)
}
// Output:
// Erroneous data:
// [2 3 Vasya]
// map[bucket_id:5 id:3]
}
// ExampleResult_operationDataCustomType demonstrates the ability
// to cast `OperationData` field, extracted from a CRUD error during decoding
// using crud.Result, to a custom type.
// The type of `OperationData` is determined as the crud.Result row type.
func ExampleResult_operationDataCustomType() {
conn := exampleConnect()
req := crud.MakeInsertObjectManyRequest(exampleSpace).Objects([]crud.Object{
crud.MapObject{
"id": 1,
"bucket_id": 3,
"name": "Makar",
},
crud.MapObject{
"id": 1,
"bucket_id": 3,
"name": "Vasya",
},
crud.MapObject{
"id": 3,
"bucket_id": 5,
},
})
type Tuple struct {
Id uint64 `msgpack:"id,omitempty"`
BucketId uint64 `msgpack:"bucket_id,omitempty"`
Name string `msgpack:"name,omitempty"`
}
ret := crud.MakeResult(reflect.TypeOf(Tuple{}))
if err := conn.Do(req).GetTyped(&ret); err != nil {
crudErrs := err.(crud.ErrorMany)
fmt.Println("Erroneous data:")
for _, crudErr := range crudErrs.Errors {
operationData := crudErr.OperationData.(Tuple)
fmt.Println(operationData)
}
} else {
fmt.Println(ret.Metadata)
fmt.Println(ret.Rows)
}
// Output:
// Erroneous data:
// {1 3 Vasya}
// {3 5 }
}
// ExampleResult_many demonstrates that there is no difference in a
// response from *ManyRequest.
func ExampleResult_many() {
conn := exampleConnect()
req := crud.MakeReplaceManyRequest(exampleSpace).
Tuples([]crud.Tuple{
[]interface{}{uint(2010), nil, "bla"},
[]interface{}{uint(2011), nil, "bla"},
})
ret := crud.Result{}
if err := conn.Do(req).GetTyped(&ret); err != nil {
fmt.Printf("Failed to execute request: %s", err)
return
}
fmt.Println(ret.Metadata)
fmt.Println(ret.Rows)
// Output:
// [{id unsigned false} {bucket_id unsigned true} {name string false}]
// [[2010 45 bla] [2011 4 bla]]
}
// ExampleResult_noreturn demonstrates noreturn request: a data change
// request where you don't need to retrieve the result, just want to know
// whether it was successful or not.
func ExampleResult_noreturn() {
conn := exampleConnect()
req := crud.MakeReplaceManyRequest(exampleSpace).
Tuples([]crud.Tuple{
[]interface{}{uint(2010), nil, "bla"},
[]interface{}{uint(2011), nil, "bla"},
}).
Opts(crud.ReplaceManyOpts{
Noreturn: crud.MakeOptBool(true),
})
ret := crud.Result{}
if err := conn.Do(req).GetTyped(&ret); err != nil {
fmt.Printf("Failed to execute request: %s", err)
return
}
fmt.Println(ret.Metadata)
fmt.Println(ret.Rows)
// Output:
// []
// <nil>
}
// ExampleResult_error demonstrates how to use a helper type Result
// to handle a crud error.
func ExampleResult_error() {
conn := exampleConnect()
req := crud.MakeReplaceRequest("not_exist").
Tuple([]interface{}{uint(2010), nil, "bla"})
ret := crud.Result{}
if err := conn.Do(req).GetTyped(&ret); err != nil {
crudErr := err.(crud.Error)
fmt.Printf("Failed to execute request: %s", crudErr)
} else {
fmt.Println(ret.Metadata)
fmt.Println(ret.Rows)
}
// Output:
// Failed to execute request: ReplaceError: Space "not_exist" doesn't exist
}
// ExampleResult_errorMany demonstrates how to use a helper type Result
// to handle a crud error for a *ManyRequest.
func ExampleResult_errorMany() {
conn := exampleConnect()
initReq := crud.MakeReplaceRequest("not_exist").
Tuple([]interface{}{uint(2010), nil, "bla"})
if _, err := conn.Do(initReq).Get(); err != nil {
fmt.Printf("Failed to initialize the example: %s\n", err)
}
req := crud.MakeInsertManyRequest(exampleSpace).
Tuples([]crud.Tuple{
[]interface{}{uint(2010), nil, "bla"},
[]interface{}{uint(2010), nil, "bla"},
})
ret := crud.Result{}
if err := conn.Do(req).GetTyped(&ret); err != nil {
crudErr := err.(crud.ErrorMany)
// We need to trim the error message to make the example repeatable.
errmsg := crudErr.Error()[:10]
fmt.Printf("Failed to execute request: %s", errmsg)
} else {
fmt.Println(ret.Metadata)
fmt.Println(ret.Rows)
}
// Output:
// Failed to execute request: CallError:
}
func ExampleSelectRequest_pagination() {
conn := exampleConnect()
const (
fromTuple = 5
allTuples = 10
)
var tuple interface{}
for i := 0; i < allTuples; i++ {
req := crud.MakeReplaceRequest(exampleSpace).
Tuple([]interface{}{uint(3000 + i), nil, "bla"})
ret := crud.Result{}
if err := conn.Do(req).GetTyped(&ret); err != nil {
fmt.Printf("Failed to initialize the example: %s\n", err)
return
}
if i == fromTuple {
tuple = ret.Rows.([]interface{})[0]
}
}
req := crud.MakeSelectRequest(exampleSpace).
Opts(crud.SelectOpts{
First: crud.MakeOptInt(2),
After: crud.MakeOptTuple(tuple),
})
ret := crud.Result{}
if err := conn.Do(req).GetTyped(&ret); err != nil {
fmt.Printf("Failed to execute request: %s", err)
return
}
fmt.Println(ret.Metadata)
fmt.Println(ret.Rows)
// Output:
// [{id unsigned false} {bucket_id unsigned true} {name string false}]
// [[3006 32 bla] [3007 33 bla]]
}
func ExampleSchema() {
conn := exampleConnect()
req := crud.MakeSchemaRequest()
var result crud.SchemaResult
if err := conn.Do(req).GetTyped(&result); err != nil {
fmt.Printf("Failed to execute request: %s", err)
return
}
// Schema may differ between different Tarantool versions.
// https://github.com/tarantool/tarantool/issues/4091
// https://github.com/tarantool/tarantool/commit/17c9c034933d726925910ce5bf8b20e8e388f6e3
for spaceName, spaceSchema := range result.Value {
fmt.Printf("Space format for '%s' is as follows:\n", spaceName)
for _, field := range spaceSchema.Format {
fmt.Printf(" - field '%s' with type '%s'\n", field.Name, field.Type)
}
}
// Output:
// Space format for 'test' is as follows:
// - field 'id' with type 'unsigned'
// - field 'bucket_id' with type 'unsigned'
// - field 'name' with type 'string'
}