Skip to content

Commit

Permalink
readme: move schema example to tests
Browse files Browse the repository at this point in the history
- split schema example to multiple examples
- make examples executable

Part of #123
  • Loading branch information
ligurio committed Apr 14, 2022
1 parent 0f2ad63 commit 613127d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 29 deletions.
29 changes: 0 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
67 changes: 67 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 613127d

Please sign in to comment.