Skip to content

Commit 92b6ff5

Browse files
DifferentialOrangeoleg-jukovec
authored andcommitted
crud: support schema opts
Follows #336
1 parent 9892410 commit 92b6ff5

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1919
- Support `operation_data` in `crud.Error` (#330)
2020
- Support `fetch_latest_metadata` option for crud requests with metadata (#335)
2121
- Support `noreturn` option for data change crud requests (#335)
22-
- Support `crud.schema` request (#336)
22+
- Support `crud.schema` request (#336, #351)
2323
- Support `IPROTO_WATCH_ONCE` request type for Tarantool
2424
version >= 3.0.0-alpha1 (#337)
2525
- Support `yield_every` option for crud select requests (#350)

crud/options.go

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const (
2323
batchSizeOptName = "batch_size"
2424
fetchLatestMetadataOptName = "fetch_latest_metadata"
2525
noreturnOptName = "noreturn"
26+
cachedOptName = "cached"
2627
)
2728

2829
// OptUint is an optional uint.

crud/request_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ func TestRequestsCodes(t *testing.T) {
159159
{req: crud.MakeCountRequest(validSpace), rtype: CrudRequestType},
160160
{req: crud.MakeStorageInfoRequest(), rtype: CrudRequestType},
161161
{req: crud.MakeStatsRequest(), rtype: CrudRequestType},
162+
{req: crud.MakeSchemaRequest(), rtype: CrudRequestType},
162163
}
163164

164165
for _, test := range tests {
@@ -196,6 +197,7 @@ func TestRequestsAsync(t *testing.T) {
196197
{req: crud.MakeCountRequest(validSpace), async: false},
197198
{req: crud.MakeStorageInfoRequest(), async: false},
198199
{req: crud.MakeStatsRequest(), async: false},
200+
{req: crud.MakeSchemaRequest(), async: false},
199201
}
200202

201203
for _, test := range tests {
@@ -233,6 +235,7 @@ func TestRequestsCtx_default(t *testing.T) {
233235
{req: crud.MakeCountRequest(validSpace), expected: nil},
234236
{req: crud.MakeStorageInfoRequest(), expected: nil},
235237
{req: crud.MakeStatsRequest(), expected: nil},
238+
{req: crud.MakeSchemaRequest(), expected: nil},
236239
}
237240

238241
for _, test := range tests {
@@ -271,6 +274,7 @@ func TestRequestsCtx_setter(t *testing.T) {
271274
{req: crud.MakeCountRequest(validSpace).Context(ctx), expected: ctx},
272275
{req: crud.MakeStorageInfoRequest().Context(ctx), expected: ctx},
273276
{req: crud.MakeStatsRequest().Context(ctx), expected: ctx},
277+
{req: crud.MakeSchemaRequest().Context(ctx), expected: ctx},
274278
}
275279

276280
for _, test := range tests {
@@ -427,6 +431,12 @@ func TestRequestsDefaultValues(t *testing.T) {
427431
[]interface{}{}),
428432
target: crud.MakeStatsRequest(),
429433
},
434+
{
435+
name: "SchemaRequest",
436+
ref: tarantool.NewCall17Request("crud.schema").Args(
437+
[]interface{}{nil, map[string]interface{}{}}),
438+
target: crud.MakeSchemaRequest(),
439+
},
430440
}
431441

432442
for _, tc := range testCases {
@@ -589,6 +599,20 @@ func TestRequestsSetters(t *testing.T) {
589599
[]interface{}{spaceName}),
590600
target: crud.MakeStatsRequest().Space(spaceName),
591601
},
602+
{
603+
name: "SchemaRequest",
604+
ref: tarantool.NewCall17Request("crud.schema").Args(
605+
[]interface{}{nil, schemaOpts},
606+
),
607+
target: crud.MakeSchemaRequest().Opts(schemaOpts),
608+
},
609+
{
610+
name: "SchemaRequestWithSpace",
611+
ref: tarantool.NewCall17Request("crud.schema").Args(
612+
[]interface{}{spaceName, schemaOpts},
613+
),
614+
target: crud.MakeSchemaRequest().Space(spaceName).Opts(schemaOpts),
615+
},
592616
}
593617

594618
for _, tc := range testCases {

crud/schema.go

+37-2
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,39 @@ func msgpackIsMap(code byte) bool {
1414
return code == msgpcode.Map16 || code == msgpcode.Map32 || msgpcode.IsFixedMap(code)
1515
}
1616

17+
// SchemaOpts describes options for `crud.schema` method.
18+
type SchemaOpts struct {
19+
// Timeout is a `vshard.call` timeout and vshard
20+
// master discovery timeout (in seconds).
21+
Timeout OptFloat64
22+
// VshardRouter is cartridge vshard group name or
23+
// vshard router instance.
24+
VshardRouter OptString
25+
// Cached defines whether router should reload storage schema on call.
26+
Cached OptBool
27+
}
28+
29+
// EncodeMsgpack provides custom msgpack encoder.
30+
func (opts SchemaOpts) EncodeMsgpack(enc *msgpack.Encoder) error {
31+
const optsCnt = 3
32+
33+
names := [optsCnt]string{timeoutOptName, vshardRouterOptName,
34+
cachedOptName}
35+
values := [optsCnt]interface{}{}
36+
exists := [optsCnt]bool{}
37+
values[0], exists[0] = opts.Timeout.Get()
38+
values[1], exists[1] = opts.VshardRouter.Get()
39+
values[2], exists[2] = opts.Cached.Get()
40+
41+
return encodeOptions(enc, names[:], values[:], exists[:])
42+
}
43+
1744
// SchemaRequest helps you to create request object to call `crud.schema`
1845
// for execution by a Connection.
1946
type SchemaRequest struct {
2047
baseRequest
2148
space OptString
49+
opts SchemaOpts
2250
}
2351

2452
// MakeSchemaRequest returns a new empty SchemaRequest.
@@ -35,12 +63,19 @@ func (req SchemaRequest) Space(space string) SchemaRequest {
3563
return req
3664
}
3765

66+
// Opts sets the options for the SchemaRequest request.
67+
// Note: default value is nil.
68+
func (req SchemaRequest) Opts(opts SchemaOpts) SchemaRequest {
69+
req.opts = opts
70+
return req
71+
}
72+
3873
// Body fills an encoder with the call request body.
3974
func (req SchemaRequest) Body(res tarantool.SchemaResolver, enc *msgpack.Encoder) error {
4075
if value, ok := req.space.Get(); ok {
41-
req.impl = req.impl.Args([]interface{}{value})
76+
req.impl = req.impl.Args([]interface{}{value, req.opts})
4277
} else {
43-
req.impl = req.impl.Args([]interface{}{})
78+
req.impl = req.impl.Args([]interface{}{nil, req.opts})
4479
}
4580

4681
return req.impl.Body(res, enc)

crud/tarantool_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ var opObjManyOpts = crud.OperationObjectManyOpts{
8787
Timeout: crud.MakeOptFloat64(timeout),
8888
}
8989

90+
var schemaOpts = crud.SchemaOpts{
91+
Timeout: crud.MakeOptFloat64(timeout),
92+
Cached: crud.MakeOptBool(false),
93+
}
94+
9095
var conditions = []crud.Condition{
9196
{
9297
Operator: crud.Lt,
@@ -216,6 +221,11 @@ var testProcessDataCases = []struct {
216221
1,
217222
crud.MakeStorageInfoRequest().Opts(baseOpts),
218223
},
224+
{
225+
"Schema",
226+
1,
227+
crud.MakeSchemaRequest().Opts(schemaOpts),
228+
},
219229
}
220230

221231
var testResultWithErrCases = []struct {

0 commit comments

Comments
 (0)