Skip to content

Commit

Permalink
doc: add examples for GetTyped() and custom keys
Browse files Browse the repository at this point in the history
The patch adds examples for:

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

Closes #196
  • Loading branch information
oleg-jukovec committed Nov 1, 2022
1 parent bebaab9 commit 7b3a4d8
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 10 deletions.
17 changes: 9 additions & 8 deletions client_tools.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tarantool

// IntKey is utility type for passing integer key to Select*, Update* and Delete*.
// It serializes to array with single integer element.
// IntKey is utility type for passing integer key to Select*, Update*,
// Delete* and GetTyped. It serializes to array with single integer element.
type IntKey struct {
I int
}
Expand All @@ -12,8 +12,9 @@ func (k IntKey) EncodeMsgpack(enc *encoder) error {
return nil
}

// UintKey is utility type for passing unsigned integer key to Select*, Update* and Delete*.
// It serializes to array with single integer element.
// UintKey is utility type for passing unsigned integer key to Select*,
// Update*, Delete* and GetTyped. It serializes to array with single unsigned
// integer element.
type UintKey struct {
I uint
}
Expand All @@ -24,8 +25,8 @@ func (k UintKey) EncodeMsgpack(enc *encoder) error {
return nil
}

// UintKey is utility type for passing string key to Select*, Update* and Delete*.
// It serializes to array with single string element.
// StringKey is utility type for passing string key to Select*, Update*,
// Delete* and GetTyped. It serializes to array with single string element.
type StringKey struct {
S string
}
Expand All @@ -36,8 +37,8 @@ func (k StringKey) EncodeMsgpack(enc *encoder) error {
return nil
}

// IntIntKey is utility type for passing two integer keys to Select*, Update* and Delete*.
// It serializes to array with two integer elements.
// IntIntKey is utility type for passing two integer keys to Select*, Update*,
// Delete* and GetTyped. It serializes to array with two integer elements.
type IntIntKey struct {
I1, I2 int
}
Expand Down
24 changes: 22 additions & 2 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,29 @@ box.once("init", function()
if_not_exists = true
})

local s = box.schema.space.create('SQL_TEST', {
local s = box.schema.space.create('teststring', {
id = 518,
if_not_exists = true,
})
s:create_index('primary', {
type = 'tree',
parts = {1, 'string'},
if_not_exists = true
})

local s = box.schema.space.create('testintint', {
id = 519,
if_not_exists = true,
})
s:create_index('primary', {
type = 'tree',
parts = {1, 'int', 2, 'int'},
if_not_exists = true
})

local s = box.schema.space.create('SQL_TEST', {
id = 520,
if_not_exists = true,
format = {
{name = "NAME0", type = "unsigned"},
{name = "NAME1", type = "string"},
Expand All @@ -62,7 +82,7 @@ box.once("init", function()
s:insert{1, "test", "test"}

local s = box.schema.space.create('test_perf', {
id = 519,
id = 521,
temporary = true,
if_not_exists = true,
field_count = 3,
Expand Down
92 changes: 92 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,98 @@ func ExampleConnection_SelectAsync() {
// Future 2 Data [[18 val 18 bla]]
}

func ExampleConnection_GetTyped() {
conn := example_connect()
defer conn.Close()

const space = "test"
const index = "primary"
conn.Replace(space, []interface{}{uint(1111), "hello", "world"})

var t Tuple
err := conn.GetTyped(space, index, []interface{}{1111}, &t)
fmt.Println("Error", err)
fmt.Println("Data", t)
// Output:
// Error <nil>
// Data {{} 1111 hello world}
}

func ExampleIntKey() {
conn := example_connect()
defer conn.Close()

const space = "test"
const index = "primary"
conn.Replace(space, []interface{}{int(1111), "hello", "world"})

var t Tuple
err := conn.GetTyped(space, index, tarantool.IntKey{1111}, &t)
fmt.Println("Error", err)
fmt.Println("Data", t)
// Output:
// Error <nil>
// Data {{} 1111 hello world}
}

func ExampleUintKey() {
conn := example_connect()
defer conn.Close()

const space = "test"
const index = "primary"
conn.Replace(space, []interface{}{uint(1111), "hello", "world"})

var t Tuple
err := conn.GetTyped(space, index, tarantool.UintKey{1111}, &t)
fmt.Println("Error", err)
fmt.Println("Data", t)
// Output:
// Error <nil>
// Data {{} 1111 hello world}
}

func ExampleStringKey() {
conn := example_connect()
defer conn.Close()

const space = "teststring"
const index = "primary"
conn.Replace(space, []interface{}{"any", []byte{0x01, 0x02}})

t := struct {
Key string
Value []byte
}{}
err := conn.GetTyped(space, index, tarantool.StringKey{"any"}, &t)
fmt.Println("Error", err)
fmt.Println("Data", t)
// Output:
// Error <nil>
// Data {any [1 2]}
}

func ExampleIntIntKey() {
conn := example_connect()
defer conn.Close()

const space = "testintint"
const index = "primary"
conn.Replace(space, []interface{}{1, 2, "foo"})

t := struct {
Key1 int
Key2 int
Value string
}{}
err := conn.GetTyped(space, index, tarantool.IntIntKey{1, 2}, &t)
fmt.Println("Error", err)
fmt.Println("Data", t)
// Output:
// Error <nil>
// Data {1 2 foo}
}

func ExampleSelectRequest() {
conn := example_connect()
defer conn.Close()
Expand Down

0 comments on commit 7b3a4d8

Please sign in to comment.