Skip to content

Commit 440350e

Browse files
committed
example: make examples more compact
Previously each example created a connection and processed error. To make examples more compact and clear for documentation reader patch handles connection error in example_connect() used in examples and panics in case of error. Part of #123
1 parent 70216b8 commit 440350e

File tree

1 file changed

+19
-75
lines changed

1 file changed

+19
-75
lines changed

example_test.go

Lines changed: 19 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,21 @@ type Tuple struct {
1616
Name string
1717
}
1818

19-
func example_connect() (*tarantool.Connection, error) {
19+
func example_connect() *tarantool.Connection {
2020
conn, err := tarantool.Connect(server, opts)
2121
if err != nil {
22-
return nil, err
22+
panic("Connection is not established")
2323
}
24-
_, err = conn.Replace(spaceNo, []interface{}{uint(1111), "hello", "world"})
25-
if err != nil {
26-
conn.Close()
27-
return nil, err
28-
}
29-
_, err = conn.Replace(spaceNo, []interface{}{uint(1112), "hallo", "werld"})
30-
if err != nil {
31-
conn.Close()
32-
return nil, err
33-
}
34-
return conn, nil
24+
return conn
3525
}
3626

3727
func ExampleConnection_Select() {
38-
var conn *tarantool.Connection
39-
conn, err := example_connect()
40-
if err != nil {
41-
fmt.Printf("error in prepare is %v", err)
42-
return
43-
}
28+
conn := example_connect()
4429
defer conn.Close()
30+
31+
conn.Replace(spaceNo, []interface{}{uint(1111), "hello", "world"})
32+
conn.Replace(spaceNo, []interface{}{uint(1112), "hallo", "werld"})
33+
4534
resp, err := conn.Select(512, 0, 0, 100, tarantool.IterEq, []interface{}{uint(1111)})
4635
if err != nil {
4736
fmt.Printf("error in select is %v", err)
@@ -61,15 +50,10 @@ func ExampleConnection_Select() {
6150
}
6251

6352
func ExampleConnection_SelectTyped() {
64-
var conn *tarantool.Connection
65-
conn, err := example_connect()
66-
if err != nil {
67-
fmt.Printf("error in prepare is %v", err)
68-
return
69-
}
53+
conn := example_connect()
7054
defer conn.Close()
7155
var res []Tuple
72-
err = conn.SelectTyped(512, 0, 0, 100, tarantool.IterEq, tarantool.IntKey{1111}, &res)
56+
err := conn.SelectTyped(512, 0, 0, 100, tarantool.IterEq, tarantool.IntKey{1111}, &res)
7357
if err != nil {
7458
fmt.Printf("error in select is %v", err)
7559
return
@@ -87,12 +71,7 @@ func ExampleConnection_SelectTyped() {
8771
}
8872

8973
func ExampleConnection_SelectAsync() {
90-
var conn *tarantool.Connection
91-
conn, err := example_connect()
92-
if err != nil {
93-
fmt.Printf("error in prepare is %v", err)
94-
return
95-
}
74+
conn := example_connect()
9675
defer conn.Close()
9776

9877
conn.Insert(spaceNo, []interface{}{uint(16), "test", "one"})
@@ -104,7 +83,7 @@ func ExampleConnection_SelectAsync() {
10483
futs[1] = conn.SelectAsync("test", "primary", 0, 1, tarantool.IterEq, tarantool.UintKey{17})
10584
futs[2] = conn.SelectAsync("test", "primary", 0, 1, tarantool.IterEq, tarantool.UintKey{18})
10685
var t []Tuple
107-
err = futs[0].GetTyped(&t)
86+
err := futs[0].GetTyped(&t)
10887
fmt.Println("Future", 0, "Error", err)
10988
fmt.Println("Future", 0, "Data", t)
11089

@@ -125,12 +104,7 @@ func ExampleConnection_SelectAsync() {
125104
}
126105

127106
func ExampleConnection_Ping() {
128-
var conn *tarantool.Connection
129-
conn, err := example_connect()
130-
if err != nil {
131-
fmt.Printf("error in prepare is %v", err)
132-
return
133-
}
107+
conn := example_connect()
134108
defer conn.Close()
135109

136110
// Ping a Tarantool instance to check connection.
@@ -145,12 +119,7 @@ func ExampleConnection_Ping() {
145119
}
146120

147121
func ExampleConnection_Insert() {
148-
var conn *tarantool.Connection
149-
conn, err := example_connect()
150-
if err != nil {
151-
fmt.Printf("error in prepare is %v", err)
152-
return
153-
}
122+
conn := example_connect()
154123
defer conn.Close()
155124

156125
// Insert a new tuple { 31, 1 }.
@@ -183,12 +152,7 @@ func ExampleConnection_Insert() {
183152
}
184153

185154
func ExampleConnection_Delete() {
186-
var conn *tarantool.Connection
187-
conn, err := example_connect()
188-
if err != nil {
189-
fmt.Printf("error in prepare is %v", err)
190-
return
191-
}
155+
conn := example_connect()
192156
defer conn.Close()
193157

194158
// Insert a new tuple { 35, 1 }.
@@ -221,12 +185,7 @@ func ExampleConnection_Delete() {
221185
}
222186

223187
func ExampleConnection_Replace() {
224-
var conn *tarantool.Connection
225-
conn, err := example_connect()
226-
if err != nil {
227-
fmt.Printf("error in prepare is %v", err)
228-
return
229-
}
188+
conn := example_connect()
230189
defer conn.Close()
231190

232191
// Insert a new tuple { 13, 1 }.
@@ -275,12 +234,7 @@ func ExampleConnection_Replace() {
275234
}
276235

277236
func ExampleConnection_Update() {
278-
var conn *tarantool.Connection
279-
conn, err := example_connect()
280-
if err != nil {
281-
fmt.Printf("error in prepare is %v", err)
282-
return
283-
}
237+
conn := example_connect()
284238
defer conn.Close()
285239

286240
// Insert a new tuple { 14, 1 }.
@@ -300,12 +254,7 @@ func ExampleConnection_Update() {
300254
}
301255

302256
func ExampleConnection_Call17() {
303-
var conn *tarantool.Connection
304-
conn, err := example_connect()
305-
if err != nil {
306-
fmt.Printf("error in prepare is %v", err)
307-
return
308-
}
257+
conn := example_connect()
309258
defer conn.Close()
310259

311260
// Call a function 'func_name' with arguments.
@@ -322,12 +271,7 @@ func ExampleConnection_Call17() {
322271
}
323272

324273
func ExampleConnection_Eval() {
325-
var conn *tarantool.Connection
326-
conn, err := example_connect()
327-
if err != nil {
328-
fmt.Printf("error in prepare is %v", err)
329-
return
330-
}
274+
conn := example_connect()
331275
defer conn.Close()
332276

333277
// Run raw Lua code.

0 commit comments

Comments
 (0)