Skip to content

Commit 744c6ac

Browse files
committed
readme: move schema example to tests
- Make example executable - Add a description with steps how to run example manually
1 parent 0673a35 commit 744c6ac

File tree

2 files changed

+70
-29
lines changed

2 files changed

+70
-29
lines changed

README.md

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ faster than other packages according to public benchmarks.
2323
* [API reference](#api-reference)
2424
* [Walking\-through example](#walking-through-example)
2525
* [Help](#help)
26-
* [Schema](#schema)
2726
* [Custom (un)packing and typed selects and function calls](#custom-unpacking-and-typed-selects-and-function-calls)
2827
* [Options](#options)
2928
* [Tests](#tests)
@@ -171,34 +170,6 @@ To contact `go-tarantool` developers on any problems, create an issue at
171170
The developers of the [Tarantool server](http://github.com/tarantool/tarantool)
172171
will also be happy to provide advice or receive feedback.
173172

174-
## Schema
175-
176-
```go
177-
// save Schema to local variable to avoid races
178-
schema := client.Schema
179-
180-
// access Space objects by name or id
181-
space1 := schema.Spaces["some_space"]
182-
space2 := schema.SpacesById[20] // it's a map
183-
fmt.Printf("Space %d %s %s\n", space1.Id, space1.Name, space1.Engine)
184-
fmt.Printf("Space %d %d\n", space1.FieldsCount, space1.Temporary)
185-
186-
// access index information by name or id
187-
index1 := space1.Indexes["some_index"]
188-
index2 := space1.IndexesById[2] // it's a map
189-
fmt.Printf("Index %d %s\n", index1.Id, index1.Name)
190-
191-
// access index fields information by index
192-
indexField1 := index1.Fields[0] // it's a slice
193-
indexField2 := index1.Fields[1] // it's a slice
194-
fmt.Printf("IndexFields %s %s\n", indexField1.Name, indexField1.Type)
195-
196-
// access space fields information by name or id (index)
197-
spaceField1 := space.Fields["some_field"]
198-
spaceField2 := space.FieldsById[3]
199-
fmt.Printf("SpaceField %s %s\n", spaceField1.Name, spaceField1.Type)
200-
```
201-
202173
## Custom (un)packing and typed selects and function calls
203174

204175
You can specify custom pack/unpack functions for your types. This will allow you

example_schema_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Start Tarantool instance before example execution:
2+
// Terminal 1:
3+
// $ TT_LISTEN=3013 TT_WORK_DIR=$(mktemp -d -t 'tarantool.XXX') tarantool config.lua
4+
//
5+
// Terminal 2:
6+
// $ go test -v example_schema_test.go
7+
package tarantool_test
8+
9+
import (
10+
"fmt"
11+
"log"
12+
"time"
13+
14+
"github.com/tarantool/go-tarantool"
15+
)
16+
17+
func Example_schema() {
18+
server := "127.0.0.1:3013"
19+
opts := tarantool.Opts{
20+
Timeout: 500 * time.Millisecond,
21+
Reconnect: 1 * time.Second,
22+
MaxReconnects: 3,
23+
User: "test",
24+
Pass: "test",
25+
}
26+
client, err := tarantool.Connect(server, opts)
27+
if err != nil {
28+
log.Fatalf("Failed to connect: %s", err.Error())
29+
}
30+
31+
// Save Schema to local variable to avoid races
32+
schema := client.Schema
33+
if schema.SpacesById == nil {
34+
log.Fatalf("schema.SpacesById is nil")
35+
}
36+
if schema.Spaces == nil {
37+
log.Fatalf("schema.Spaces is nil")
38+
}
39+
40+
// Access Space objects by name or id
41+
space1 := schema.Spaces["test"]
42+
space2 := schema.SpacesById[514] // it's a map
43+
fmt.Printf("Space 1 ID %d %s %s\n", space1.Id, space1.Name, space1.Engine)
44+
fmt.Printf("Space 1 ID %d %t\n", space1.FieldsCount, space1.Temporary)
45+
46+
// Access index information by name or id
47+
index1 := space1.Indexes["primary"]
48+
index2 := space2.IndexesById[3] // it's a map
49+
fmt.Printf("Index %d %s\n", index1.Id, index1.Name)
50+
51+
// Access index fields information by index
52+
indexField1 := index1.Fields[0] // It's a slice
53+
indexField2 := index2.Fields[1] // It's a slice
54+
fmt.Println(indexField1, indexField2)
55+
56+
// Access space fields information by name or id (index)
57+
spaceField1 := space2.Fields["name0"]
58+
spaceField2 := space2.FieldsById[3]
59+
fmt.Printf("SpaceField 1 %s %s\n", spaceField1.Name, spaceField1.Type)
60+
fmt.Printf("SpaceField 2 %s %s\n", spaceField2.Name, spaceField2.Type)
61+
62+
// Output:
63+
// Space 1 ID 512 test memtx
64+
// Space 1 ID 0 false
65+
// Index 0 primary
66+
// &{0 unsigned} &{2 string}
67+
// SpaceField 1 name0 unsigned
68+
// SpaceField 2 name3 unsigned
69+
70+
}

0 commit comments

Comments
 (0)