Skip to content

Commit 48cf0c7

Browse files
committed
doc: add examples for GetTyped() and custom keys
The patch adds examples for: * Connection.GetTyped() method * IntKey type * UintKey type * StringKey type * IntIntKey type Closes #196
1 parent 15391b2 commit 48cf0c7

File tree

3 files changed

+123
-10
lines changed

3 files changed

+123
-10
lines changed

client_tools.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package tarantool
22

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

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

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

39-
// IntIntKey is utility type for passing two integer keys to Select*, Update* and Delete*.
40-
// It serializes to array with two integer elements.
40+
// IntIntKey is utility type for passing two integer keys to Select*, Update*,
41+
// Delete* and GetTyped. It serializes to array with two integer elements.
4142
type IntIntKey struct {
4243
I1, I2 int
4344
}

config.lua

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,29 @@ box.once("init", function()
4545
if_not_exists = true
4646
})
4747

48-
local s = box.schema.space.create('SQL_TEST', {
48+
local s = box.schema.space.create('teststring', {
4949
id = 518,
5050
if_not_exists = true,
51+
})
52+
s:create_index('primary', {
53+
type = 'tree',
54+
parts = {1, 'string'},
55+
if_not_exists = true
56+
})
57+
58+
local s = box.schema.space.create('testintint', {
59+
id = 519,
60+
if_not_exists = true,
61+
})
62+
s:create_index('primary', {
63+
type = 'tree',
64+
parts = {1, 'int', 2, 'int'},
65+
if_not_exists = true
66+
})
67+
68+
local s = box.schema.space.create('SQL_TEST', {
69+
id = 520,
70+
if_not_exists = true,
5171
format = {
5272
{name = "NAME0", type = "unsigned"},
5373
{name = "NAME1", type = "string"},
@@ -62,7 +82,7 @@ box.once("init", function()
6282
s:insert{1, "test", "test"}
6383

6484
local s = box.schema.space.create('test_perf', {
65-
id = 519,
85+
id = 521,
6686
temporary = true,
6787
if_not_exists = true,
6888
field_count = 3,

example_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,98 @@ func ExampleConnection_SelectAsync() {
127127
// Future 2 Data [[18 val 18 bla]]
128128
}
129129

130+
func ExampleConnection_GetTyped() {
131+
conn := example_connect()
132+
defer conn.Close()
133+
134+
const space = "test"
135+
const index = "primary"
136+
conn.Replace(space, []interface{}{uint(1111), "hello", "world"})
137+
138+
var t Tuple
139+
err := conn.GetTyped(space, index, []interface{}{1111}, &t)
140+
fmt.Println("Error", err)
141+
fmt.Println("Data", t)
142+
// Output:
143+
// Error <nil>
144+
// Data {{} 1111 hello world}
145+
}
146+
147+
func ExampleIntKey() {
148+
conn := example_connect()
149+
defer conn.Close()
150+
151+
const space = "test"
152+
const index = "primary"
153+
conn.Replace(space, []interface{}{int(1111), "hello", "world"})
154+
155+
var t Tuple
156+
err := conn.GetTyped(space, index, tarantool.IntKey{1111}, &t)
157+
fmt.Println("Error", err)
158+
fmt.Println("Data", t)
159+
// Output:
160+
// Error <nil>
161+
// Data {{} 1111 hello world}
162+
}
163+
164+
func ExampleUintKey() {
165+
conn := example_connect()
166+
defer conn.Close()
167+
168+
const space = "test"
169+
const index = "primary"
170+
conn.Replace(space, []interface{}{uint(1111), "hello", "world"})
171+
172+
var t Tuple
173+
err := conn.GetTyped(space, index, tarantool.UintKey{1111}, &t)
174+
fmt.Println("Error", err)
175+
fmt.Println("Data", t)
176+
// Output:
177+
// Error <nil>
178+
// Data {{} 1111 hello world}
179+
}
180+
181+
func ExampleStringKey() {
182+
conn := example_connect()
183+
defer conn.Close()
184+
185+
const space = "teststring"
186+
const index = "primary"
187+
conn.Replace(space, []interface{}{"any", []byte{0x01, 0x02}})
188+
189+
t := struct {
190+
Key string
191+
Value []byte
192+
}{}
193+
err := conn.GetTyped(space, index, tarantool.StringKey{"any"}, &t)
194+
fmt.Println("Error", err)
195+
fmt.Println("Data", t)
196+
// Output:
197+
// Error <nil>
198+
// Data {any [1 2]}
199+
}
200+
201+
func ExampleIntIntKey() {
202+
conn := example_connect()
203+
defer conn.Close()
204+
205+
const space = "testintint"
206+
const index = "primary"
207+
conn.Replace(space, []interface{}{1, 2, "foo"})
208+
209+
t := struct {
210+
Key1 int
211+
Key2 int
212+
Value string
213+
}{}
214+
err := conn.GetTyped(space, index, tarantool.IntIntKey{1, 2}, &t)
215+
fmt.Println("Error", err)
216+
fmt.Println("Data", t)
217+
// Output:
218+
// Error <nil>
219+
// Data {1 2 foo}
220+
}
221+
130222
func ExampleSelectRequest() {
131223
conn := example_connect()
132224
defer conn.Close()

0 commit comments

Comments
 (0)