forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathexample_custom_request_test.go
More file actions
296 lines (249 loc) · 6.57 KB
/
example_custom_request_test.go
File metadata and controls
296 lines (249 loc) · 6.57 KB
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
package tarantool_test
import (
"bytes"
"context"
"fmt"
"io"
"github.com/tarantool/go-iproto"
"github.com/vmihailenco/msgpack/v5"
"github.com/tarantool/go-tarantool/v3"
)
// customPingRequest demonstrates how to implement a custom Request type.
// It sends a ping request and uses DecodeBaseResponse to handle the response.
type customPingRequest struct {
ctx context.Context
}
func (r *customPingRequest) Type() iproto.Type {
return iproto.IPROTO_PING
}
func (r *customPingRequest) Body(_ tarantool.SchemaResolver, enc *msgpack.Encoder) error {
return enc.EncodeMapLen(0)
}
func (r *customPingRequest) Ctx() context.Context {
return r.ctx
}
func (r *customPingRequest) Async() bool {
return false
}
// Response creates a response for the custom request.
// It reads all data from body using DecodeBaseResponse helper.
func (r *customPingRequest) Response(
header tarantool.Header,
body io.Reader,
) (tarantool.Response, error) {
return tarantool.DecodeBaseResponse(header, body)
}
func (r *customPingRequest) Context(ctx context.Context) *customPingRequest {
r.ctx = ctx
return r
}
// ExampleRequest_Response demonstrates how to implement the Response method
// for a custom Request type.
func ExampleRequest_Response() {
conn := exampleConnect(dialer, opts)
defer func() { _ = conn.Close() }()
future := conn.Do(&customPingRequest{})
data, err := future.Get()
fmt.Println("Data", data)
fmt.Println("Error", err)
future.Release()
// Output:
// Data []
// Error <nil>
}
// manualRequest demonstrates how to implement a custom Request type
// with manual body decoding (without DecodeBaseResponse helper).
type manualRequest struct {
ctx context.Context
spaceNo uint32
key []interface{}
}
func (r *manualRequest) Type() iproto.Type {
return iproto.IPROTO_SELECT
}
func (r *manualRequest) Body(_ tarantool.SchemaResolver, enc *msgpack.Encoder) error {
if err := enc.EncodeMapLen(6); err != nil {
return err
}
if err := enc.EncodeUint(uint64(iproto.IPROTO_ITERATOR)); err != nil {
return err
}
if err := enc.EncodeUint(uint64(iproto.ITER_EQ)); err != nil {
return err
}
if err := enc.EncodeUint(uint64(iproto.IPROTO_OFFSET)); err != nil {
return err
}
if err := enc.EncodeUint(uint64(0)); err != nil {
return err
}
if err := enc.EncodeUint(uint64(iproto.IPROTO_LIMIT)); err != nil {
return err
}
if err := enc.EncodeUint(uint64(0xFFFFFFFF)); err != nil {
return err
}
if err := enc.EncodeUint(uint64(iproto.IPROTO_SPACE_ID)); err != nil {
return err
}
if err := enc.EncodeUint(uint64(r.spaceNo)); err != nil {
return err
}
if err := enc.EncodeUint(uint64(iproto.IPROTO_INDEX_ID)); err != nil {
return err
}
if err := enc.EncodeUint(uint64(0)); err != nil {
return err
}
if err := enc.EncodeUint(uint64(iproto.IPROTO_KEY)); err != nil {
return err
}
return enc.Encode(r.key)
}
func (r *manualRequest) Ctx() context.Context {
return r.ctx
}
func (r *manualRequest) Async() bool {
return false
}
// Response creates a response by reading all data from body.
// The body contains msgpack-encoded map with IPROTO_DATA key.
func (r *manualRequest) Response(
header tarantool.Header,
body io.Reader,
) (tarantool.Response, error) {
data, err := io.ReadAll(body)
if err != nil {
return nil, err
}
return &manualResponse{header: header, body: data}, nil
}
// manualResponse implements tarantool.Response interface.
type manualResponse struct {
header tarantool.Header
body []byte
}
func (r *manualResponse) Header() tarantool.Header { return r.header }
func (r *manualResponse) Release() { r.body = nil }
func (r *manualResponse) Decode() ([]interface{}, error) {
if len(r.body) == 0 {
return nil, nil
}
dec := msgpack.NewDecoder(bytes.NewReader(r.body))
mapLen, err := dec.DecodeMapLen()
if err != nil {
return nil, err
}
var result []interface{}
for i := 0; i < mapLen; i++ {
key, err := dec.DecodeInt()
if err != nil {
return nil, err
}
if iproto.Key(key) == iproto.IPROTO_DATA {
if result, err = dec.DecodeSlice(); err != nil {
return nil, err
}
} else {
if err := dec.Skip(); err != nil {
return nil, err
}
}
}
return result, nil
}
func (r *manualResponse) DecodeTyped(res interface{}) error {
if len(r.body) == 0 {
return nil
}
dec := msgpack.NewDecoder(bytes.NewReader(r.body))
mapLen, err := dec.DecodeMapLen()
if err != nil {
return err
}
for i := 0; i < mapLen; i++ {
key, err := dec.DecodeInt()
if err != nil {
return err
}
if iproto.Key(key) == iproto.IPROTO_DATA {
if err := dec.Decode(res); err != nil {
return err
}
} else {
if err := dec.Skip(); err != nil {
return err
}
}
}
return nil
}
// ExampleRequest_Response_manual demonstrates how to implement the Response method
// for a custom Request type with manual body decoding.
func ExampleRequest_Response_manual() {
conn := exampleConnect(dialer, opts)
defer func() { _ = conn.Close() }()
// Insert test data.
_, err := conn.Do(tarantool.NewReplaceRequest(spaceNo).
Tuple([]interface{}{uint(1111), "hello", "world"}),
).Get()
if err != nil {
fmt.Println("Error", err)
return
}
// Delete test data on exit.
defer func() {
_, _ = conn.Do(tarantool.NewDeleteRequest(spaceNo).
Index(indexNo).
Key([]interface{}{uint(1111)}),
).Get()
}()
// Execute custom select request.
req := &manualRequest{spaceNo: spaceNo, key: []interface{}{uint(1111)}}
future := conn.Do(req)
data, err := future.Get()
fmt.Println("Data", data)
fmt.Println("Error", err)
future.Release()
// Output:
// Data [[1111 hello world]]
// Error <nil>
}
// ExampleRequest_Response_manualDecodeTyped demonstrates how to implement DecodeTyped
// with a custom Request implementation.
func ExampleRequest_Response_manualDecodeTyped() {
conn := exampleConnect(dialer, opts)
defer func() { _ = conn.Close() }()
// Insert test data.
_, err := conn.Do(tarantool.NewReplaceRequest(spaceNo).
Tuple([]interface{}{uint(1111), "hello", "world"}),
).Get()
if err != nil {
fmt.Println("Error", err)
return
}
// Delete test data on exit.
defer func() {
_, _ = conn.Do(tarantool.NewDeleteRequest(spaceNo).
Index(indexNo).
Key([]interface{}{uint(1111)}),
).Get()
}()
// Execute custom select request.
req := &manualRequest{spaceNo: spaceNo, key: []interface{}{uint(1111)}}
future := conn.Do(req)
var tuples []Tuple
err = future.GetTyped(&tuples)
if len(tuples) > 0 {
fmt.Println("Id", tuples[0].Id)
fmt.Println("Msg", tuples[0].Msg)
fmt.Println("Name", tuples[0].Name)
}
fmt.Println("Error", err)
future.Release()
// Output:
// Id 1111
// Msg hello
// Name world
// Error <nil>
}