forked from fl00r/go-tarantool-1.6
-
Notifications
You must be signed in to change notification settings - Fork 60
crud: improvments #273
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
Merged
Merged
crud: improvments #273
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
306ef71
crud: remove NewEncoder/NewDecoder from the API
oleg-jukovec 48337a4
crud: improve Result* types
oleg-jukovec 6d684ab
crud: allow any type as Tuple
oleg-jukovec 3a98b2c
crud: make less allocations
oleg-jukovec 48f383a
crud: make requests immutable
oleg-jukovec 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
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 |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package crud_test | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"time" | ||
|
||
"github.com/tarantool/go-tarantool" | ||
"github.com/tarantool/go-tarantool/crud" | ||
) | ||
|
||
const ( | ||
exampleServer = "127.0.0.1:3013" | ||
exampleSpace = "test" | ||
) | ||
|
||
var exampleOpts = tarantool.Opts{ | ||
Timeout: 500 * time.Millisecond, | ||
User: "test", | ||
Pass: "test", | ||
} | ||
|
||
func exampleConnect() *tarantool.Connection { | ||
conn, err := tarantool.Connect(exampleServer, exampleOpts) | ||
if err != nil { | ||
panic("Connection is not established: " + err.Error()) | ||
} | ||
return conn | ||
} | ||
|
||
// ExampleResult_rowsInterface demonstrates how to use a helper type Result | ||
// to decode a crud response. In this example, rows are decoded as an | ||
// interface{} type. | ||
func ExampleResult_rowsInterface() { | ||
conn := exampleConnect() | ||
req := crud.MakeReplaceRequest(exampleSpace). | ||
Tuple([]interface{}{uint(2010), nil, "bla"}) | ||
|
||
ret := crud.Result{} | ||
if err := conn.Do(req).GetTyped(&ret); err != nil { | ||
fmt.Printf("Failed to execute request: %s", err) | ||
return | ||
} | ||
|
||
fmt.Println(ret.Metadata) | ||
fmt.Println(ret.Rows) | ||
// Output: | ||
// [{id unsigned false} {bucket_id unsigned true} {name string false}] | ||
// [[2010 45 bla]] | ||
} | ||
|
||
// ExampleResult_rowsCustomType demonstrates how to use a helper type Result | ||
// to decode a crud response. In this example, rows are decoded as a | ||
// custom type. | ||
func ExampleResult_rowsCustomType() { | ||
conn := exampleConnect() | ||
req := crud.MakeReplaceRequest(exampleSpace). | ||
Tuple([]interface{}{uint(2010), nil, "bla"}) | ||
|
||
type Tuple struct { | ||
_msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused | ||
Id uint64 | ||
BucketId uint64 | ||
Name string | ||
} | ||
ret := crud.MakeResult(reflect.TypeOf(Tuple{})) | ||
|
||
if err := conn.Do(req).GetTyped(&ret); err != nil { | ||
fmt.Printf("Failed to execute request: %s", err) | ||
return | ||
} | ||
|
||
fmt.Println(ret.Metadata) | ||
rows := ret.Rows.([]Tuple) | ||
fmt.Println(rows) | ||
// Output: | ||
// [{id unsigned false} {bucket_id unsigned true} {name string false}] | ||
// [{{} 2010 45 bla}] | ||
} | ||
|
||
// ExampleResult_many demonstrates that there is no difference in a | ||
// response from *ManyRequest. | ||
func ExampleResult_many() { | ||
conn := exampleConnect() | ||
req := crud.MakeReplaceManyRequest(exampleSpace). | ||
Tuples([]crud.Tuple{ | ||
[]interface{}{uint(2010), nil, "bla"}, | ||
[]interface{}{uint(2011), nil, "bla"}, | ||
}) | ||
|
||
ret := crud.Result{} | ||
if err := conn.Do(req).GetTyped(&ret); err != nil { | ||
fmt.Printf("Failed to execute request: %s", err) | ||
return | ||
} | ||
|
||
fmt.Println(ret.Metadata) | ||
fmt.Println(ret.Rows) | ||
// Output: | ||
// [{id unsigned false} {bucket_id unsigned true} {name string false}] | ||
// [[2010 45 bla] [2011 4 bla]] | ||
} | ||
|
||
// ExampleResult_error demonstrates how to use a helper type Result | ||
// to handle a crud error. | ||
func ExampleResult_error() { | ||
conn := exampleConnect() | ||
req := crud.MakeReplaceRequest("not_exist"). | ||
Tuple([]interface{}{uint(2010), nil, "bla"}) | ||
|
||
ret := crud.Result{} | ||
if err := conn.Do(req).GetTyped(&ret); err != nil { | ||
crudErr := err.(crud.Error) | ||
fmt.Printf("Failed to execute request: %s", crudErr) | ||
} else { | ||
fmt.Println(ret.Metadata) | ||
fmt.Println(ret.Rows) | ||
} | ||
// Output: | ||
// Failed to execute request: ReplaceError: Space "not_exist" doesn't exist | ||
} | ||
|
||
// ExampleResult_errorMany demonstrates how to use a helper type Result | ||
// to handle a crud error for a *ManyRequest. | ||
func ExampleResult_errorMany() { | ||
conn := exampleConnect() | ||
initReq := crud.MakeReplaceRequest("not_exist"). | ||
Tuple([]interface{}{uint(2010), nil, "bla"}) | ||
if _, err := conn.Do(initReq).Get(); err != nil { | ||
fmt.Printf("Failed to initialize the example: %s\n", err) | ||
} | ||
|
||
req := crud.MakeInsertManyRequest(exampleSpace). | ||
Tuples([]crud.Tuple{ | ||
[]interface{}{uint(2010), nil, "bla"}, | ||
[]interface{}{uint(2010), nil, "bla"}, | ||
}) | ||
ret := crud.Result{} | ||
if err := conn.Do(req).GetTyped(&ret); err != nil { | ||
crudErr := err.(crud.ErrorMany) | ||
// We need to trim the error message to make the example repeatable. | ||
errmsg := crudErr.Error()[:10] | ||
fmt.Printf("Failed to execute request: %s", errmsg) | ||
} else { | ||
fmt.Println(ret.Metadata) | ||
fmt.Println(ret.Rows) | ||
} | ||
// Output: | ||
// Failed to execute request: CallError: | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.