Skip to content

Commit 48d0ae7

Browse files
committed
crud: refactor optional types to use go-option
- Remove: OptUint, OptInt, OptFloat64, OptString, OptBool, OptTuple. - Remove: MakeOptUint, MakeOptInt, MakeOptFloat64, MakeOptString, MakeOptBool, MakeOptTuple. - Add: OptAny = option.Generic[interface{}] . - Add: MakeOptAny constructor. - Update: All option structs to use option.* types. - Fix: Test type inconsistencies. Changed #492
1 parent e72726b commit 48d0ae7

File tree

12 files changed

+282
-265
lines changed

12 files changed

+282
-265
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
1212

1313
* New types for MessagePack extensions compatible with go-option (#459).
1414
* Added `box.MustNew` wrapper for `box.New` without an error (#448).
15+
* Added missing IPROTO feature flags to greeting negotiation
16+
(iproto.IPROTO_FEATURE_IS_SYNC, iproto.IPROTO_FEATURE_INSERT_ARROW) (#466).
1517

1618
### Changed
1719

@@ -24,6 +26,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
2426
and Future.GetIterator() methods, ResponseIterator and TimeoutResponseIterator types,
2527
Future.pushes[] (#480).
2628
* `LogAppendPushFailed` replaced with `LogBoxSessionPushUnsupported` (#480)
29+
* Replaced the use of optional types in crud with go-option library (#492).
2730

2831
### Fixed
2932

@@ -45,8 +48,7 @@ flag handling, and fixes watcher panic.
4548
Now you can check this error with `errors.Is(err, tarantool.ErrConcurrentSchemaUpdate)`.
4649
- Implemented support for `IPROTO_IS_SYNC` flag in stream transactions,
4750
added `IsSync(bool)` method for `BeginRequest`/`CommitRequest` (#447).
48-
- Added missing IPROTO feature flags to greeting negotiation
49-
(iproto.IPROTO_FEATURE_IS_SYNC, iproto.IPROTO_FEATURE_INSERT_ARROW) (#466).
51+
5052

5153
### Fixed
5254

crud/compile_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// crud/compile_test.go
2+
package crud
3+
4+
import (
5+
"testing"
6+
7+
"github.com/tarantool/go-option"
8+
)
9+
10+
// TestOptionTypesCompilation verifies that all option types are compiled correctly.
11+
func TestOptionTypesCompilation(t *testing.T) {
12+
// Test BaseOpts
13+
baseOpts := BaseOpts{
14+
Timeout: option.SomeFloat64(1.5),
15+
VshardRouter: option.SomeString("router"),
16+
}
17+
18+
// Check that Get() is working.
19+
if timeout, exists := baseOpts.Timeout.Get(); !exists || timeout != 1.5 {
20+
t.Errorf("BaseOpts.Timeout.Get() failed")
21+
}
22+
23+
// Test SimpleOperationOpts.
24+
simpleOpts := SimpleOperationOpts{
25+
Timeout: option.SomeFloat64(2.0),
26+
VshardRouter: option.SomeString("router2"),
27+
Fields: MakeOptAny([]interface{}{"field1", "field2"}),
28+
BucketId: option.SomeUint(456),
29+
FetchLatestMetadata: option.SomeBool(true),
30+
Noreturn: option.SomeBool(false),
31+
}
32+
33+
if bucket, exists := simpleOpts.BucketId.Get(); !exists || bucket != 456 {
34+
t.Errorf("BucketId.Get() failed: got %v, %v", bucket, exists)
35+
}
36+
37+
if fields, exists := simpleOpts.Fields.Get(); !exists {
38+
t.Errorf("Fields.Get() failed")
39+
} else {
40+
t.Logf("Fields: %v", fields)
41+
}
42+
43+
// Test OperationManyOpts.
44+
manyOpts := OperationManyOpts{
45+
Timeout: option.SomeFloat64(3.0),
46+
StopOnError: option.SomeBool(true),
47+
}
48+
49+
if stop, exists := manyOpts.StopOnError.Get(); !exists || !stop {
50+
t.Errorf("StopOnError.Get() failed")
51+
}
52+
}
53+
54+
// TestMakeOptAny checks the operation of MakeOptAny (replacing MakeOptTuple).
55+
func TestMakeOptAny(t *testing.T) {
56+
// Test with simple data types.
57+
testCases := []struct {
58+
name string
59+
value interface{}
60+
expected interface{}
61+
}{
62+
{"string", "test", "test"},
63+
{"number", 42, 42},
64+
{"nil", nil, nil},
65+
}
66+
67+
for _, tc := range testCases {
68+
t.Run(tc.name, func(t *testing.T) {
69+
opt := MakeOptAny(tc.value)
70+
val, exists := opt.Get()
71+
72+
if tc.value == nil {
73+
if exists {
74+
t.Errorf("Expected no value for nil input, but got %v", val)
75+
}
76+
} else {
77+
if !exists {
78+
t.Errorf("Expected value for %v, but got none", tc.value)
79+
}
80+
if val != tc.expected {
81+
t.Errorf("Expected %v, got %v", tc.expected, val)
82+
}
83+
}
84+
})
85+
}
86+
87+
// Test with a slice - we check without comparing the values.
88+
t.Run("slice", func(t *testing.T) {
89+
sliceValue := []interface{}{"id", "name"}
90+
opt := MakeOptAny(sliceValue)
91+
val, exists := opt.Get()
92+
93+
if !exists {
94+
t.Errorf("Expected value for slice, but got none")
95+
}
96+
97+
// We check the type and length instead of direct comparison.
98+
if valSlice, ok := val.([]interface{}); !ok {
99+
t.Errorf("Expected slice type, got %T", val)
100+
} else if len(valSlice) != 2 {
101+
t.Errorf("Expected slice length 2, got %d", len(valSlice))
102+
} else {
103+
t.Logf("Slice test passed: %v", valSlice)
104+
}
105+
})
106+
}

crud/count.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/vmihailenco/msgpack/v5"
77

8+
"github.com/tarantool/go-option"
89
"github.com/tarantool/go-tarantool/v3"
910
)
1011

@@ -15,30 +16,30 @@ type CountResult = NumberResult
1516
type CountOpts struct {
1617
// Timeout is a `vshard.call` timeout and vshard
1718
// master discovery timeout (in seconds).
18-
Timeout OptFloat64
19+
Timeout option.Float64
1920
// VshardRouter is cartridge vshard group name or
2021
// vshard router instance.
21-
VshardRouter OptString
22+
VshardRouter option.String
2223
// Mode is a parameter with `write`/`read` possible values,
2324
// if `write` is specified then operation is performed on master.
24-
Mode OptString
25+
Mode option.String
2526
// PreferReplica is a parameter to specify preferred target
2627
// as one of the replicas.
27-
PreferReplica OptBool
28+
PreferReplica option.Bool
2829
// Balance is a parameter to use replica according to vshard
2930
// load balancing policy.
30-
Balance OptBool
31+
Balance option.Bool
3132
// YieldEvery describes number of tuples processed to yield after.
3233
// Should be positive.
33-
YieldEvery OptUint
34+
YieldEvery option.Uint
3435
// BucketId is a bucket ID.
35-
BucketId OptUint
36+
BucketId option.Uint
3637
// ForceMapCall describes the map call is performed without any
3738
// optimizations even if full primary key equal condition is specified.
38-
ForceMapCall OptBool
39+
ForceMapCall option.Bool
3940
// Fullscan describes if a critical log entry will be skipped on
4041
// potentially long count.
41-
Fullscan OptBool
42+
Fullscan option.Bool
4243
}
4344

4445
// EncodeMsgpack provides custom msgpack encoder.

crud/example_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"reflect"
77
"time"
88

9+
"github.com/tarantool/go-option"
910
"github.com/tarantool/go-tarantool/v3"
1011
"github.com/tarantool/go-tarantool/v3/crud"
1112
)
@@ -288,7 +289,7 @@ func ExampleResult_noreturn() {
288289
[]interface{}{uint(2011), nil, "bla"},
289290
}).
290291
Opts(crud.ReplaceManyOpts{
291-
Noreturn: crud.MakeOptBool(true),
292+
Noreturn: option.SomeBool(true),
292293
})
293294

294295
ret := crud.Result{}
@@ -375,8 +376,8 @@ func ExampleSelectRequest_pagination() {
375376

376377
req := crud.MakeSelectRequest(exampleSpace).
377378
Opts(crud.SelectOpts{
378-
First: crud.MakeOptInt(2),
379-
After: crud.MakeOptTuple(tuple),
379+
First: option.SomeInt64(2),
380+
After: crud.MakeOptAny(tuple),
380381
})
381382
ret := crud.Result{}
382383
if err := conn.Do(req).GetTyped(&ret); err != nil {

crud/get.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,36 @@ import (
55

66
"github.com/vmihailenco/msgpack/v5"
77

8+
"github.com/tarantool/go-option"
89
"github.com/tarantool/go-tarantool/v3"
910
)
1011

1112
// GetOpts describes options for `crud.get` method.
1213
type GetOpts struct {
1314
// Timeout is a `vshard.call` timeout and vshard
1415
// master discovery timeout (in seconds).
15-
Timeout OptFloat64
16+
Timeout option.Float64
1617
// VshardRouter is cartridge vshard group name or
1718
// vshard router instance.
18-
VshardRouter OptString
19+
VshardRouter option.String
1920
// Fields is field names for getting only a subset of fields.
20-
Fields OptTuple
21+
Fields OptAny // Type Any is instead of a local type Tuple.
2122
// BucketId is a bucket ID.
22-
BucketId OptUint
23+
BucketId option.Uint
2324
// Mode is a parameter with `write`/`read` possible values,
2425
// if `write` is specified then operation is performed on master.
25-
Mode OptString
26+
Mode option.String
2627
// PreferReplica is a parameter to specify preferred target
2728
// as one of the replicas.
28-
PreferReplica OptBool
29+
PreferReplica option.Bool
2930
// Balance is a parameter to use replica according to vshard
3031
// load balancing policy.
31-
Balance OptBool
32+
Balance option.Bool
3233
// FetchLatestMetadata guarantees the up-to-date metadata (space format)
3334
// in first return value, otherwise it may not take into account
3435
// the latest migration of the data format. Performance overhead is up to 15%.
3536
// Disabled by default.
36-
FetchLatestMetadata OptBool
37+
FetchLatestMetadata option.Bool
3738
}
3839

3940
// EncodeMsgpack provides custom msgpack encoder.

crud/insert.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package crud
33
import (
44
"context"
55

6-
"github.com/vmihailenco/msgpack/v5"
7-
86
"github.com/tarantool/go-tarantool/v3"
7+
"github.com/vmihailenco/msgpack/v5"
98
)
109

1110
// InsertOpts describes options for `crud.insert` method.

0 commit comments

Comments
 (0)