Skip to content

Commit 1dba94a

Browse files
author
New year
committed
crud: replace usage of optional types in crud with go-option
fixed #492
1 parent 69baf40 commit 1dba94a

File tree

8 files changed

+85
-70
lines changed

8 files changed

+85
-70
lines changed

crud/count.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/vmihailenco/msgpack/v5"
77

88
"github.com/tarantool/go-tarantool/v3"
9+
"github.com/tarantool/go-option"
910
)
1011

1112
// CountResult describes result for `crud.count` method.
@@ -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
@@ -6,34 +6,35 @@ import (
66
"github.com/vmihailenco/msgpack/v5"
77

88
"github.com/tarantool/go-tarantool/v3"
9+
"github.com/tarantool/go-option"
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/options.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ func (o *OptTuple) Get() (interface{}, bool) {
140140

141141
// Defining an alias for Generic[interface{}] to replace OptTuple
142142
type OptAny = option.Generic[interface{}]
143+
144+
// MakeOptAny creates an optional value from interface{} (replacing MakeOptTuple).
145+
func MakeOptAny(value interface{}) OptAny {
146+
if value == nil {
147+
return option.None[interface{}]()
148+
}
149+
return option.Some[interface{}](value)
150+
}
143151
// BaseOpts describes base options for CRUD operations.
144152
type BaseOpts struct {
145153
// Timeout is a `vshard.call` timeout and vshard
@@ -217,7 +225,7 @@ type SimpleOperationObjectOpts struct {
217225
// Fields is field names for getting only a subset of fields.
218226
Fields OptAny
219227
// BucketId is a bucket ID.
220-
BucketId option.Unit64
228+
BucketId option.Uint64
221229
// SkipNullabilityCheckOnFlatten is a parameter to allow
222230
// setting null values to non-nullable fields.
223231
SkipNullabilityCheckOnFlatten option.Bool

crud/request_test.go

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/tarantool/go-tarantool/v3"
1313
"github.com/tarantool/go-tarantool/v3/crud"
14+
"github.com/tarantool/go-option"
1415
)
1516

1617
const validSpace = "test" // Any valid value != default.
@@ -102,7 +103,7 @@ func BenchmarkLenRequest(b *testing.B) {
102103
buf.Reset()
103104
req := crud.MakeLenRequest(spaceName).
104105
Opts(crud.LenOpts{
105-
Timeout: crud.MakeOptFloat64(3.5),
106+
Timeout: option.SomeFloat64(3.5),
106107
})
107108
if err := req.Body(nil, enc); err != nil {
108109
b.Error(err)
@@ -121,9 +122,9 @@ func BenchmarkSelectRequest(b *testing.B) {
121122
buf.Reset()
122123
req := crud.MakeSelectRequest(spaceName).
123124
Opts(crud.SelectOpts{
124-
Timeout: crud.MakeOptFloat64(3.5),
125-
VshardRouter: crud.MakeOptString("asd"),
126-
Balance: crud.MakeOptBool(true),
125+
Timeout: option.SomeFloat64(3.5),
126+
VshardRouter: option.SomeString("asd"),
127+
Balance: option.SomeBool(true),
127128
})
128129
if err := req.Body(nil, enc); err != nil {
129130
b.Error(err)
@@ -636,7 +637,7 @@ func TestRequestsVshardRouter(t *testing.T) {
636637
map[string]interface{}{"vshard_router": "custom_router"},
637638
}),
638639
target: crud.MakeInsertRequest(validSpace).Opts(crud.InsertOpts{
639-
VshardRouter: crud.MakeOptString("custom_router"),
640+
VshardRouter: option.SomeString("custom_router"),
640641
}),
641642
},
642643
{
@@ -647,7 +648,7 @@ func TestRequestsVshardRouter(t *testing.T) {
647648
map[string]interface{}{"vshard_router": "custom_router"},
648649
}),
649650
target: crud.MakeInsertObjectRequest(validSpace).Opts(crud.InsertObjectOpts{
650-
VshardRouter: crud.MakeOptString("custom_router"),
651+
VshardRouter: option.SomeString("custom_router"),
651652
}),
652653
},
653654
{
@@ -658,7 +659,7 @@ func TestRequestsVshardRouter(t *testing.T) {
658659
map[string]interface{}{"vshard_router": "custom_router"},
659660
}),
660661
target: crud.MakeInsertManyRequest(validSpace).Opts(crud.InsertManyOpts{
661-
VshardRouter: crud.MakeOptString("custom_router"),
662+
VshardRouter: option.SomeString("custom_router"),
662663
}),
663664
},
664665
{
@@ -669,7 +670,7 @@ func TestRequestsVshardRouter(t *testing.T) {
669670
map[string]interface{}{"vshard_router": "custom_router"},
670671
}),
671672
target: crud.MakeInsertObjectManyRequest(validSpace).Opts(crud.InsertObjectManyOpts{
672-
VshardRouter: crud.MakeOptString("custom_router"),
673+
VshardRouter: option.SomeString("custom_router"),
673674
}),
674675
},
675676
{
@@ -680,7 +681,7 @@ func TestRequestsVshardRouter(t *testing.T) {
680681
map[string]interface{}{"vshard_router": "custom_router"},
681682
}),
682683
target: crud.MakeGetRequest(validSpace).Opts(crud.GetOpts{
683-
VshardRouter: crud.MakeOptString("custom_router"),
684+
VshardRouter: option.SomeString("custom_router"),
684685
}),
685686
},
686687
{
@@ -692,7 +693,7 @@ func TestRequestsVshardRouter(t *testing.T) {
692693
map[string]interface{}{"vshard_router": "custom_router"},
693694
}),
694695
target: crud.MakeUpdateRequest(validSpace).Opts(crud.UpdateOpts{
695-
VshardRouter: crud.MakeOptString("custom_router"),
696+
VshardRouter: option.SomeString("custom_router"),
696697
}),
697698
},
698699
{
@@ -703,7 +704,7 @@ func TestRequestsVshardRouter(t *testing.T) {
703704
map[string]interface{}{"vshard_router": "custom_router"},
704705
}),
705706
target: crud.MakeDeleteRequest(validSpace).Opts(crud.DeleteOpts{
706-
VshardRouter: crud.MakeOptString("custom_router"),
707+
VshardRouter: option.SomeString("custom_router"),
707708
}),
708709
},
709710
{
@@ -714,7 +715,7 @@ func TestRequestsVshardRouter(t *testing.T) {
714715
map[string]interface{}{"vshard_router": "custom_router"},
715716
}),
716717
target: crud.MakeReplaceRequest(validSpace).Opts(crud.ReplaceOpts{
717-
VshardRouter: crud.MakeOptString("custom_router"),
718+
VshardRouter: option.SomeString("custom_router"),
718719
}),
719720
},
720721
{
@@ -725,7 +726,7 @@ func TestRequestsVshardRouter(t *testing.T) {
725726
map[string]interface{}{"vshard_router": "custom_router"},
726727
}),
727728
target: crud.MakeReplaceObjectRequest(validSpace).Opts(crud.ReplaceObjectOpts{
728-
VshardRouter: crud.MakeOptString("custom_router"),
729+
VshardRouter: option.SomeString("custom_router"),
729730
}),
730731
},
731732
{
@@ -736,7 +737,7 @@ func TestRequestsVshardRouter(t *testing.T) {
736737
map[string]interface{}{"vshard_router": "custom_router"},
737738
}),
738739
target: crud.MakeReplaceManyRequest(validSpace).Opts(crud.ReplaceManyOpts{
739-
VshardRouter: crud.MakeOptString("custom_router"),
740+
VshardRouter: option.SomeString("custom_router"),
740741
}),
741742
},
742743
{
@@ -747,7 +748,7 @@ func TestRequestsVshardRouter(t *testing.T) {
747748
map[string]interface{}{"vshard_router": "custom_router"},
748749
}),
749750
target: crud.MakeReplaceObjectManyRequest(validSpace).Opts(crud.ReplaceObjectManyOpts{
750-
VshardRouter: crud.MakeOptString("custom_router"),
751+
VshardRouter: option.SomeString("custom_router"),
751752
}),
752753
},
753754
{
@@ -759,7 +760,7 @@ func TestRequestsVshardRouter(t *testing.T) {
759760
map[string]interface{}{"vshard_router": "custom_router"},
760761
}),
761762
target: crud.MakeUpsertRequest(validSpace).Opts(crud.UpsertOpts{
762-
VshardRouter: crud.MakeOptString("custom_router"),
763+
VshardRouter: option.SomeString("custom_router"),
763764
}),
764765
},
765766
{
@@ -771,7 +772,7 @@ func TestRequestsVshardRouter(t *testing.T) {
771772
map[string]interface{}{"vshard_router": "custom_router"},
772773
}),
773774
target: crud.MakeUpsertObjectRequest(validSpace).Opts(crud.UpsertObjectOpts{
774-
VshardRouter: crud.MakeOptString("custom_router"),
775+
VshardRouter: option.SomeString("custom_router"),
775776
}),
776777
},
777778
{
@@ -782,7 +783,7 @@ func TestRequestsVshardRouter(t *testing.T) {
782783
map[string]interface{}{"vshard_router": "custom_router"},
783784
}),
784785
target: crud.MakeUpsertManyRequest(validSpace).Opts(crud.UpsertManyOpts{
785-
VshardRouter: crud.MakeOptString("custom_router"),
786+
VshardRouter: option.SomeString("custom_router"),
786787
}),
787788
},
788789
{
@@ -793,7 +794,7 @@ func TestRequestsVshardRouter(t *testing.T) {
793794
map[string]interface{}{"vshard_router": "custom_router"},
794795
}),
795796
target: crud.MakeUpsertObjectManyRequest(validSpace).Opts(crud.UpsertObjectManyOpts{
796-
VshardRouter: crud.MakeOptString("custom_router"),
797+
VshardRouter: option.SomeString("custom_router"),
797798
}),
798799
},
799800
{
@@ -804,7 +805,7 @@ func TestRequestsVshardRouter(t *testing.T) {
804805
map[string]interface{}{"vshard_router": "custom_router"},
805806
}),
806807
target: crud.MakeSelectRequest(validSpace).Opts(crud.SelectOpts{
807-
VshardRouter: crud.MakeOptString("custom_router"),
808+
VshardRouter: option.SomeString("custom_router"),
808809
}),
809810
},
810811
{
@@ -815,7 +816,7 @@ func TestRequestsVshardRouter(t *testing.T) {
815816
map[string]interface{}{"vshard_router": "custom_router"},
816817
}),
817818
target: crud.MakeMinRequest(validSpace).Opts(crud.MinOpts{
818-
VshardRouter: crud.MakeOptString("custom_router"),
819+
VshardRouter: option.SomeString("custom_router"),
819820
}),
820821
},
821822
{
@@ -826,7 +827,7 @@ func TestRequestsVshardRouter(t *testing.T) {
826827
map[string]interface{}{"vshard_router": "custom_router"},
827828
}),
828829
target: crud.MakeMaxRequest(validSpace).Opts(crud.MaxOpts{
829-
VshardRouter: crud.MakeOptString("custom_router"),
830+
VshardRouter: option.SomeString("custom_router"),
830831
}),
831832
},
832833
{
@@ -836,7 +837,7 @@ func TestRequestsVshardRouter(t *testing.T) {
836837
map[string]interface{}{"vshard_router": "custom_router"},
837838
}),
838839
target: crud.MakeTruncateRequest(validSpace).Opts(crud.TruncateOpts{
839-
VshardRouter: crud.MakeOptString("custom_router"),
840+
VshardRouter: option.SomeString("custom_router"),
840841
}),
841842
},
842843
{
@@ -846,7 +847,7 @@ func TestRequestsVshardRouter(t *testing.T) {
846847
map[string]interface{}{"vshard_router": "custom_router"},
847848
}),
848849
target: crud.MakeLenRequest(validSpace).Opts(crud.LenOpts{
849-
VshardRouter: crud.MakeOptString("custom_router"),
850+
VshardRouter: option.SomeString("custom_router"),
850851
}),
851852
},
852853
{
@@ -857,7 +858,7 @@ func TestRequestsVshardRouter(t *testing.T) {
857858
map[string]interface{}{"vshard_router": "custom_router"},
858859
}),
859860
target: crud.MakeCountRequest(validSpace).Opts(crud.CountOpts{
860-
VshardRouter: crud.MakeOptString("custom_router"),
861+
VshardRouter: option.SomeString("custom_router"),
861862
}),
862863
},
863864
{
@@ -866,7 +867,7 @@ func TestRequestsVshardRouter(t *testing.T) {
866867
map[string]interface{}{"vshard_router": "custom_router"},
867868
}),
868869
target: crud.MakeStorageInfoRequest().Opts(crud.StorageInfoOpts{
869-
VshardRouter: crud.MakeOptString("custom_router"),
870+
VshardRouter: option.SomeString("custom_router"),
870871
}),
871872
},
872873
{
@@ -876,7 +877,7 @@ func TestRequestsVshardRouter(t *testing.T) {
876877
map[string]interface{}{"vshard_router": "custom_router"},
877878
}),
878879
target: crud.MakeSchemaRequest().Opts(crud.SchemaOpts{
879-
VshardRouter: crud.MakeOptString("custom_router"),
880+
VshardRouter: option.SomeString("custom_router"),
880881
}),
881882
},
882883
{
@@ -886,7 +887,7 @@ func TestRequestsVshardRouter(t *testing.T) {
886887
map[string]interface{}{"vshard_router": "custom_router"},
887888
}),
888889
target: crud.MakeSchemaRequest().Space(validSpace).Opts(crud.SchemaOpts{
889-
VshardRouter: crud.MakeOptString("custom_router"),
890+
VshardRouter: option.SomeString("custom_router"),
890891
}),
891892
},
892893
}

0 commit comments

Comments
 (0)