Skip to content

Commit 613127d

Browse files
committed
readme: move schema example to tests
- split schema example to multiple examples - make examples executable Part of #123
1 parent 0f2ad63 commit 613127d

File tree

2 files changed

+67
-29
lines changed

2 files changed

+67
-29
lines changed

README.md

-29
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ faster than other packages according to public benchmarks.
2626
* [API reference](#api-reference)
2727
* [Walking\-through example in Go](#walking-through-example-in-go)
2828
* [Help](#help)
29-
* [Schema](#schema)
3029
* [Custom (un)packing and typed selects and function calls](#custom-unpacking-and-typed-selects-and-function-calls)
3130
* [Options](#options)
3231
* [Tests](#tests)
@@ -174,34 +173,6 @@ To contact `go-tarantool` developers on any problems, create an issue at
174173
The developers of the [Tarantool server](http://github.com/tarantool/tarantool)
175174
will also be happy to provide advice or receive feedback.
176175

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

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

example_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,70 @@ func ExampleConnect() {
305305
// Output:
306306
// Connection is ready
307307
}
308+
309+
// Example demonstrates how to retrieve information with space schema.
310+
func ExampleSchema() {
311+
conn := example_connect()
312+
defer conn.Close()
313+
314+
schema := conn.Schema
315+
if schema.SpacesById == nil {
316+
fmt.Println("schema.SpacesById is nil")
317+
}
318+
if schema.Spaces == nil {
319+
fmt.Println("schema.Spaces is nil")
320+
}
321+
322+
space1 := schema.Spaces["test"]
323+
space2 := schema.SpacesById[514]
324+
fmt.Printf("Space 1 ID %d %s\n", space1.Id, space1.Name)
325+
fmt.Printf("Space 2 ID %d %s\n", space2.Id, space2.Name)
326+
// Output:
327+
// Space 1 ID 512 test
328+
// Space 2 ID 514 schematest
329+
}
330+
331+
// Example demonstrates how to retrieve information with space schema.
332+
func ExampleSpace() {
333+
conn := example_connect()
334+
defer conn.Close()
335+
336+
// Save Schema to a local variable to avoid races
337+
schema := conn.Schema
338+
if schema.SpacesById == nil {
339+
fmt.Println("schema.SpacesById is nil")
340+
}
341+
if schema.Spaces == nil {
342+
fmt.Println("schema.Spaces is nil")
343+
}
344+
345+
// Access Space objects by name or ID
346+
space1 := schema.Spaces["test"]
347+
space2 := schema.SpacesById[514] // It's a map
348+
fmt.Printf("Space 1 ID %d %s %s\n", space1.Id, space1.Name, space1.Engine)
349+
fmt.Printf("Space 1 ID %d %t\n", space1.FieldsCount, space1.Temporary)
350+
351+
// Access index information by name or ID
352+
index1 := space1.Indexes["primary"]
353+
index2 := space2.IndexesById[3] // It's a map
354+
fmt.Printf("Index %d %s\n", index1.Id, index1.Name)
355+
356+
// Access index fields information by index
357+
indexField1 := index1.Fields[0] // It's a slice
358+
indexField2 := index2.Fields[1] // It's a slice
359+
fmt.Println(indexField1, indexField2)
360+
361+
// Access space fields information by name or id (index)
362+
spaceField1 := space2.Fields["name0"]
363+
spaceField2 := space2.FieldsById[3]
364+
fmt.Printf("SpaceField 1 %s %s\n", spaceField1.Name, spaceField1.Type)
365+
fmt.Printf("SpaceField 2 %s %s\n", spaceField2.Name, spaceField2.Type)
366+
367+
// Output:
368+
// Space 1 ID 512 test memtx
369+
// Space 1 ID 0 false
370+
// Index 0 primary
371+
// &{0 unsigned} &{2 string}
372+
// SpaceField 1 name0 unsigned
373+
// SpaceField 2 name3 unsigned
374+
}

0 commit comments

Comments
 (0)