Skip to content

Commit 8373af1

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

File tree

3 files changed

+140
-53
lines changed

3 files changed

+140
-53
lines changed

crud/compile_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// crud/compile_test.go
2+
package crud
3+
4+
import (
5+
"testing"
6+
"github.com/tarantool/go-option"
7+
)
8+
9+
// TestOptionTypesCompilation проверяет что все типы option компилируются правильно
10+
func TestOptionTypesCompilation(t *testing.T) {
11+
// Test BaseOpts
12+
baseOpts := BaseOpts{
13+
Timeout: option.SomeFloat64(1.5),
14+
VshardRouter: option.SomeString("router"),
15+
}
16+
17+
// Проверяем что Get() работает
18+
if timeout, exists := baseOpts.Timeout.Get(); !exists || timeout != 1.5 {
19+
t.Errorf("BaseOpts.Timeout.Get() failed")
20+
}
21+
22+
// Test SimpleOperationOpts
23+
simpleOpts := SimpleOperationOpts{
24+
Timeout: option.SomeFloat64(2.0),
25+
VshardRouter: option.SomeString("router2"),
26+
Fields: MakeOptAny([]interface{}{"field1", "field2"}),
27+
BucketId: option.SomeUint(456), // Теперь это правильный тип
28+
FetchLatestMetadata: option.SomeBool(true),
29+
Noreturn: option.SomeBool(false),
30+
}
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 проверяет работу MakeOptAny (замена MakeOptTuple)
55+
func TestMakeOptAny(t *testing.T) {
56+
// Test с различными типами данных
57+
testCases := []struct {
58+
name string
59+
value interface{}
60+
}{
61+
{"slice", []interface{}{"id", "name"}},
62+
{"string", "test"},
63+
{"number", 42},
64+
{"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")
75+
}
76+
} else {
77+
if !exists {
78+
t.Errorf("Expected value for %v", tc.value)
79+
}
80+
if val != tc.value {
81+
t.Errorf("Expected %v, got %v", tc.value, val)
82+
}
83+
}
84+
})
85+
}
86+
}

crud/options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ type SimpleOperationOpts struct {
183183
// Fields is field names for getting only a subset of fields.
184184
Fields OptAny
185185
// BucketId is a bucket ID.
186-
BucketId option.Uint64
186+
BucketId option.Uint
187187
// FetchLatestMetadata guarantees the up-to-date metadata (space format)
188188
// in first return value, otherwise it may not take into account
189189
// the latest migration of the data format. Performance overhead is up to 15%.
@@ -225,7 +225,7 @@ type SimpleOperationObjectOpts struct {
225225
// Fields is field names for getting only a subset of fields.
226226
Fields OptAny
227227
// BucketId is a bucket ID.
228-
BucketId option.Uint64
228+
BucketId option.Uint
229229
// SkipNullabilityCheckOnFlatten is a parameter to allow
230230
// setting null values to non-nullable fields.
231231
SkipNullabilityCheckOnFlatten option.Bool

0 commit comments

Comments
 (0)