Skip to content

Commit 21bd65d

Browse files
author
New year
committed
crud: refactor optional types to use go-option
- Remove: OptUint, OptInt, OptFloat64, OptString, OptBool, OptTuple. - Remove: MakeOptUint, MakeOptInt, MakeOptFloat64, MakeOptString, MakeOptBool, MakeOptTuple. - Update: All option structs to use option.* types. - Add type Any from go-option. - Fix: Test type inconsistencies. Closes #492
1 parent eec84ad commit 21bd65d

File tree

14 files changed

+292
-270
lines changed

14 files changed

+292
-270
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
2626
and Future.GetIterator() methods, ResponseIterator and TimeoutResponseIterator types,
2727
Future.pushes[] (#480).
2828
* `LogAppendPushFailed` replaced with `LogBoxSessionPushUnsupported` (#480)
29+
* Replaced the use of optional types in crud with go-option library (#492).
2930

3031
### Fixed
3132

crud/compile_test.go

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

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: option.SomeAny(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 option.Any // 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)