From 613127dfc3a89c1ef3d365e5c1928d0dbfc84ff2 Mon Sep 17 00:00:00 2001 From: Sergey Bronnikov Date: Wed, 30 Mar 2022 20:29:37 +0300 Subject: [PATCH] readme: move schema example to tests - split schema example to multiple examples - make examples executable Part of #123 --- README.md | 29 --------------------- example_test.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index ef0b1d168..00754ee3b 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,6 @@ faster than other packages according to public benchmarks. * [API reference](#api-reference) * [Walking\-through example in Go](#walking-through-example-in-go) * [Help](#help) -* [Schema](#schema) * [Custom (un)packing and typed selects and function calls](#custom-unpacking-and-typed-selects-and-function-calls) * [Options](#options) * [Tests](#tests) @@ -174,34 +173,6 @@ To contact `go-tarantool` developers on any problems, create an issue at The developers of the [Tarantool server](http://github.com/tarantool/tarantool) will also be happy to provide advice or receive feedback. -## Schema - -```go - // save Schema to local variable to avoid races - schema := client.Schema - - // access Space objects by name or id - space1 := schema.Spaces["some_space"] - space2 := schema.SpacesById[20] // it's a map - fmt.Printf("Space %d %s %s\n", space1.Id, space1.Name, space1.Engine) - fmt.Printf("Space %d %d\n", space1.FieldsCount, space1.Temporary) - - // access index information by name or id - index1 := space1.Indexes["some_index"] - index2 := space1.IndexesById[2] // it's a map - fmt.Printf("Index %d %s\n", index1.Id, index1.Name) - - // access index fields information by index - indexField1 := index1.Fields[0] // it's a slice - indexField2 := index1.Fields[1] // it's a slice - fmt.Printf("IndexFields %s %s\n", indexField1.Name, indexField1.Type) - - // access space fields information by name or id (index) - spaceField1 := space.Fields["some_field"] - spaceField2 := space.FieldsById[3] - fmt.Printf("SpaceField %s %s\n", spaceField1.Name, spaceField1.Type) -``` - ## Custom (un)packing and typed selects and function calls You can specify custom pack/unpack functions for your types. This will allow you diff --git a/example_test.go b/example_test.go index be6c01827..c6c9f03cf 100644 --- a/example_test.go +++ b/example_test.go @@ -305,3 +305,70 @@ func ExampleConnect() { // Output: // Connection is ready } + +// Example demonstrates how to retrieve information with space schema. +func ExampleSchema() { + conn := example_connect() + defer conn.Close() + + schema := conn.Schema + if schema.SpacesById == nil { + fmt.Println("schema.SpacesById is nil") + } + if schema.Spaces == nil { + fmt.Println("schema.Spaces is nil") + } + + space1 := schema.Spaces["test"] + space2 := schema.SpacesById[514] + fmt.Printf("Space 1 ID %d %s\n", space1.Id, space1.Name) + fmt.Printf("Space 2 ID %d %s\n", space2.Id, space2.Name) + // Output: + // Space 1 ID 512 test + // Space 2 ID 514 schematest +} + +// Example demonstrates how to retrieve information with space schema. +func ExampleSpace() { + conn := example_connect() + defer conn.Close() + + // Save Schema to a local variable to avoid races + schema := conn.Schema + if schema.SpacesById == nil { + fmt.Println("schema.SpacesById is nil") + } + if schema.Spaces == nil { + fmt.Println("schema.Spaces is nil") + } + + // Access Space objects by name or ID + space1 := schema.Spaces["test"] + space2 := schema.SpacesById[514] // It's a map + fmt.Printf("Space 1 ID %d %s %s\n", space1.Id, space1.Name, space1.Engine) + fmt.Printf("Space 1 ID %d %t\n", space1.FieldsCount, space1.Temporary) + + // Access index information by name or ID + index1 := space1.Indexes["primary"] + index2 := space2.IndexesById[3] // It's a map + fmt.Printf("Index %d %s\n", index1.Id, index1.Name) + + // Access index fields information by index + indexField1 := index1.Fields[0] // It's a slice + indexField2 := index2.Fields[1] // It's a slice + fmt.Println(indexField1, indexField2) + + // Access space fields information by name or id (index) + spaceField1 := space2.Fields["name0"] + spaceField2 := space2.FieldsById[3] + fmt.Printf("SpaceField 1 %s %s\n", spaceField1.Name, spaceField1.Type) + fmt.Printf("SpaceField 2 %s %s\n", spaceField2.Name, spaceField2.Type) + + // Output: + // Space 1 ID 512 test memtx + // Space 1 ID 0 false + // Index 0 primary + // &{0 unsigned} &{2 string} + // SpaceField 1 name0 unsigned + // SpaceField 2 name3 unsigned +}