Skip to content

example for connection.GetTyped() and a tuple with slice/array members #196

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

Closed
zachgrayio opened this issue Jul 21, 2022 · 2 comments · Fixed by #225
Closed

example for connection.GetTyped() and a tuple with slice/array members #196

zachgrayio opened this issue Jul 21, 2022 · 2 comments · Fixed by #225
Assignees
Labels
good first issue Good for newcomers

Comments

@zachgrayio
Copy link

So far I've been unable to work out how to implement MsgPack decoder for the simple tuple:

type Tuple struct {
	Key      string
	Value    []byte
}

and the examples don't really show how to do this. no matter what I try, I'm met with ER_INVALID_MSGPACK: Invalid MsgPack - packet body

Can someone point me in the right direction?

@zachgrayio
Copy link
Author

zachgrayio commented Jul 21, 2022

Update for others who might be having similar issues:

Since I'm just storing a []byte as value, I used a simple string encoder and just cast the []byte value back and forth as needed. Thus the msgpack decode is trivial.

Secondly, while I didn't find it in any of the examples, I did realize that conn.SelectAsync is called under the hood by conn.GetTyped and it expects a tarantool.* Key wrapper around the key value; so changing my call to use tarantool.StringKey{mystring} solved the issue; it was a Key error, not an issue with the MsgPack encoding of my payload itself (I wasn't getting that far).

@oleg-jukovec
Copy link
Collaborator

oleg-jukovec commented Jul 27, 2022

Thank you for highlighting the problem. We probably need to add examples for StringKey, IntIntKey or a paragraph to the documentation: how-to use own key.

A quick example for the issue (just an example, we need to find an elegant solution):

diff --git a/config.lua b/config.lua
index abea457..cedd60d 100644
--- a/config.lua
+++ b/config.lua
@@ -11,6 +11,12 @@ box.once("init", function()
     })
     s:create_index('primary', {type = 'tree', parts = {1, 'uint'}, if_not_exists = true})
 
+    local s = box.schema.space.create('test_string_byte', {
+        id = 667,
+        if_not_exists = true,
+    })
+    s:create_index('primary', {type = 'tree', parts = {1, 'string'}, if_not_exists = true})
+
     local sp = box.schema.space.create('SQL_TEST', {
         id = 519,
         if_not_exists = true,
diff --git a/example_test.go b/example_test.go
index cd4c787..5d7d552 100644
--- a/example_test.go
+++ b/example_test.go
@@ -18,6 +18,32 @@ type Tuple struct {
        Name     string
 }
 
+type TupleStringByte struct {
+       Key             string
+       Value   []byte
+}
+
+func ExampleStringKey() {
+       conn := example_connect()
+       defer conn.Close()
+
+       _, err := conn.Replace("test_string_byte", []interface{}{"asd", []byte{0x01, 0x02, 0x03, 0x04, 0x00, 0x00}})
+       if err != nil {
+               fmt.Printf("error in replace is %v", err)
+               return
+       }
+
+       var res []TupleStringByte
+       err = conn.SelectTyped("test_string_byte", 0, 0, 100, tarantool.IterEq, tarantool.StringKey{"asd"}, &res)
+       if err != nil {
+               fmt.Printf("error in select is %v", err)
+               return
+       }
+       fmt.Printf("response is %#v\n", res)
+       // Output:
+       // response is []tarantool_test.TupleByte{tarantool_test.TupleByte{Key:"asd", Value:[]uint8{0x1, 0x2, 0x3, 0x4, 0x0, 0x0}}}
+}
+
 func example_connect() *tarantool.Connection {
        conn, err := tarantool.Connect(server, opts)
        if err != nil {

@oleg-jukovec oleg-jukovec added the good first issue Good for newcomers label Jul 27, 2022
@kyukhin kyukhin added teamE bug Something isn't working labels Aug 5, 2022
@LeonidVas LeonidVas removed the bug Something isn't working label Aug 5, 2022
@LeonidVas LeonidVas added the 1sp label Sep 14, 2022
@oleg-jukovec oleg-jukovec self-assigned this Nov 1, 2022
oleg-jukovec added a commit that referenced this issue Nov 1, 2022
The patch adds examples for:

* Connection.GetTyped() method
* IntKey type
* UintKey type
* StringKey type
* IntIntKey type

Closes #196
oleg-jukovec added a commit that referenced this issue Nov 1, 2022
The patch adds examples for:

* Connection.GetTyped() method
* IntKey type
* UintKey type
* StringKey type
* IntIntKey type

Closes #196
oleg-jukovec added a commit that referenced this issue Nov 1, 2022
oleg-jukovec added a commit that referenced this issue Nov 1, 2022
The patch adds examples for:

* Connection.GetTyped() method
* IntKey type
* UintKey type
* StringKey type
* IntIntKey type

Closes #196
oleg-jukovec added a commit that referenced this issue Nov 5, 2022
oleg-jukovec added a commit that referenced this issue Nov 5, 2022
The patch adds examples for:

* Connection.GetTyped() method
* IntKey type
* UintKey type
* StringKey type
* IntIntKey type

Closes #196
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants