forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 60
crud: refactor optional types to use go-option #498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
babyTsakhes
wants to merge
1
commit into
master
Choose a base branch
from
babyTsakhes/gh-492-replace-usage-of-optional-types-in-crud-with-go-option
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| // crud/compile_test.go | ||
| package crud | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/tarantool/go-option" | ||
| ) | ||
|
|
||
| // TestOptionTypesCompilation verifies that all option types are compiled correctly. | ||
| func TestOptionTypesCompilation(t *testing.T) { | ||
| // Test BaseOpts | ||
| baseOpts := BaseOpts{ | ||
| Timeout: option.SomeFloat64(1.5), | ||
| VshardRouter: option.SomeString("router"), | ||
| } | ||
|
|
||
| // Check that Get() is working. | ||
| if timeout, exists := baseOpts.Timeout.Get(); !exists || timeout != 1.5 { | ||
| t.Errorf("BaseOpts.Timeout.Get() failed") | ||
| } | ||
|
|
||
| // Test SimpleOperationOpts. | ||
| simpleOpts := SimpleOperationOpts{ | ||
| Timeout: option.SomeFloat64(2.0), | ||
| VshardRouter: option.SomeString("router2"), | ||
| Fields: MakeOptAny([]interface{}{"field1", "field2"}), | ||
| BucketId: option.SomeUint(456), | ||
| FetchLatestMetadata: option.SomeBool(true), | ||
| Noreturn: option.SomeBool(false), | ||
| } | ||
|
|
||
| if bucket, exists := simpleOpts.BucketId.Get(); !exists || bucket != 456 { | ||
| t.Errorf("BucketId.Get() failed: got %v, %v", bucket, exists) | ||
| } | ||
|
|
||
| if fields, exists := simpleOpts.Fields.Get(); !exists { | ||
| t.Errorf("Fields.Get() failed") | ||
| } else { | ||
| t.Logf("Fields: %v", fields) | ||
| } | ||
|
|
||
| // Test OperationManyOpts. | ||
| manyOpts := OperationManyOpts{ | ||
| Timeout: option.SomeFloat64(3.0), | ||
| StopOnError: option.SomeBool(true), | ||
| } | ||
|
|
||
| if stop, exists := manyOpts.StopOnError.Get(); !exists || !stop { | ||
| t.Errorf("StopOnError.Get() failed") | ||
| } | ||
| } | ||
|
|
||
| // TestMakeOptAny checks the operation of MakeOptAny (replacing MakeOptTuple). | ||
| func TestMakeOptAny(t *testing.T) { | ||
| // Test with simple data types. | ||
| testCases := []struct { | ||
| name string | ||
| value interface{} | ||
| expected interface{} | ||
| }{ | ||
| {"string", "test", "test"}, | ||
| {"number", 42, 42}, | ||
| {"nil", nil, nil}, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| opt := MakeOptAny(tc.value) | ||
| val, exists := opt.Get() | ||
|
|
||
| if tc.value == nil { | ||
| if exists { | ||
| t.Errorf("Expected no value for nil input, but got %v", val) | ||
| } | ||
| } else { | ||
| if !exists { | ||
| t.Errorf("Expected value for %v, but got none", tc.value) | ||
| } | ||
| if val != tc.expected { | ||
| t.Errorf("Expected %v, got %v", tc.expected, val) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // Test with a slice - we check without comparing the values. | ||
| t.Run("slice", func(t *testing.T) { | ||
| sliceValue := []interface{}{"id", "name"} | ||
| opt := MakeOptAny(sliceValue) | ||
| val, exists := opt.Get() | ||
|
|
||
| if !exists { | ||
| t.Errorf("Expected value for slice, but got none") | ||
| } | ||
|
|
||
| // We check the type and length instead of direct comparison. | ||
| if valSlice, ok := val.([]interface{}); !ok { | ||
| t.Errorf("Expected slice type, got %T", val) | ||
| } else if len(valSlice) != 2 { | ||
| t.Errorf("Expected slice length 2, got %d", len(valSlice)) | ||
| } else { | ||
| t.Logf("Slice test passed: %v", valSlice) | ||
| } | ||
| }) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,35 +5,36 @@ import ( | |
|
|
||
| "github.com/vmihailenco/msgpack/v5" | ||
|
|
||
| "github.com/tarantool/go-option" | ||
| "github.com/tarantool/go-tarantool/v3" | ||
| ) | ||
|
|
||
| // GetOpts describes options for `crud.get` method. | ||
| type GetOpts struct { | ||
| // Timeout is a `vshard.call` timeout and vshard | ||
| // master discovery timeout (in seconds). | ||
| Timeout OptFloat64 | ||
| Timeout option.Float64 | ||
| // VshardRouter is cartridge vshard group name or | ||
| // vshard router instance. | ||
| VshardRouter OptString | ||
| VshardRouter option.String | ||
| // Fields is field names for getting only a subset of fields. | ||
| Fields OptTuple | ||
| Fields OptAny // Type Any is instead of a local type Tuple. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please, add the option.Any type into the |
||
| // BucketId is a bucket ID. | ||
| BucketId OptUint | ||
| BucketId option.Uint | ||
| // Mode is a parameter with `write`/`read` possible values, | ||
| // if `write` is specified then operation is performed on master. | ||
| Mode OptString | ||
| Mode option.String | ||
| // PreferReplica is a parameter to specify preferred target | ||
| // as one of the replicas. | ||
| PreferReplica OptBool | ||
| PreferReplica option.Bool | ||
| // Balance is a parameter to use replica according to vshard | ||
| // load balancing policy. | ||
| Balance OptBool | ||
| Balance option.Bool | ||
| // FetchLatestMetadata guarantees the up-to-date metadata (space format) | ||
| // in first return value, otherwise it may not take into account | ||
| // the latest migration of the data format. Performance overhead is up to 15%. | ||
| // Disabled by default. | ||
| FetchLatestMetadata OptBool | ||
| FetchLatestMetadata option.Bool | ||
| } | ||
|
|
||
| // EncodeMsgpack provides custom msgpack encoder. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, move the changelog entry in a separate commit.