Skip to content

Commit 4bf31b0

Browse files
docs: update README
Closes #TNTP-3736
1 parent da12087 commit 4bf31b0

File tree

1 file changed

+44
-56
lines changed

1 file changed

+44
-56
lines changed

README.md

Lines changed: 44 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@ high performance due to:
2222
* [Installation](#installation)
2323
* [Documentation](#documentation)
2424
* [Quick start](#quick-start)
25+
* [Using pre-generated optional types](#using-pre-generated-optional-types)
26+
* [Usage with go-tarantool](#usage-with-go-tarantool)
2527
* [Gentype Utility](#gentype-utility)
28+
* [Overview](#overview)
29+
* [Features](#features)
30+
* [Gentype installation](#gentype-installation)
31+
* [Generating Optional Types](#generating-optional-types)
32+
* [Using Generated Types](#using-generated-types)
2633
* [Development](#development)
34+
* [Run tests](#run-tests)
2735
* [License](#license)
2836

2937
## Installation
@@ -32,48 +40,46 @@ high performance due to:
3240
go install github.com/tarantool/go-option@latest
3341
```
3442

43+
## Documentation
44+
45+
You could run the `godoc` server on `localhost:6060` with the command:
46+
47+
```shell
48+
make godoc_run
49+
```
50+
51+
And open the generated documentation in another terminal or use the
52+
[link][godoc-url]:
53+
54+
```shell
55+
make godoc_open
56+
```
57+
3558
## Quick start
3659

37-
```Go
38-
package main
60+
### Using pre-generated optional types
3961

40-
import (
41-
"bytes"
42-
"fmt"
62+
Generated types follow the pattern Optional<TypeName> and provide methods for working
63+
with optional values:
4364

44-
"github.com/tarantool/go-option"
45-
msgpack "github.com/vmihailenco/msgpack/v5"
46-
)
65+
```go
66+
// Create an optional with a value.
67+
opt := SomeOptionalString("hello")
4768

48-
func main() {
49-
var buf bytes.Buffer
50-
51-
enc := msgpack.NewEncoder(&buf)
52-
dec := msgpack.NewDecoder(&buf)
53-
54-
someUint := option.SomeUint(12)
55-
err := someUint.EncodeMsgpack(enc)
56-
if err != nil {
57-
panic("encode fail")
58-
}
59-
60-
var unmarshaled option.Uint
61-
err = unmarshaled.DecodeMsgpack(dec)
62-
if err != nil {
63-
panic(fmt.Errorf("decode fail: %s", err))
64-
}
65-
66-
if !unmarshaled.IsSome() {
67-
panic("IsSome error")
68-
}
69-
70-
if unmarshaled.Unwrap() != 12 {
71-
panic("Unwrap error")
72-
}
69+
// Check if a value is present.
70+
if opt.IsSome() {
71+
value := opt.Unwrap()
72+
fmt.Println(value)
7373
}
74+
75+
// Use a default value if none.
76+
value := opt.UnwrapOr("default")
77+
78+
// Encode to MessagePack.
79+
err := opt.EncodeMsgpack(encoder)
7480
```
7581

76-
## Usage with go-tarantool
82+
### Usage with go-tarantool
7783

7884
It may be necessary to use an optional type in a structure. For example,
7985
to distinguish between a nil value and a missing one.
@@ -145,17 +151,15 @@ while ensuring proper encoding and decoding when using MessagePack.
145151
- Full MessagePack `CustomEncoder` and `CustomDecoder` implementation
146152
- Type-safe operations
147153

148-
### Installation
154+
### Gentype installation
149155

150156
```bash
151157
go install github.com/tarantool/go-option/cmd/gentypes@latest
152158
# OR (for go version 1.24+)
153159
go get -tool github.com/tarantool/go-option/cmd/gentypes@latest
154160
```
155161

156-
### Usage
157-
158-
#### Generating Optional Types
162+
### Generating Optional Types
159163

160164
To generate optional types for existing types in a package:
161165

@@ -179,7 +183,7 @@ Flags:
179183
-128 and 127, no default value)
180184
`-verbose`: Enable verbose output (default: `false`)
181185

182-
#### Using Generated Types
186+
### Using Generated Types
183187

184188
Generated types follow the pattern Optional<TypeName> and provide methods for working
185189
with optional values:
@@ -199,7 +203,6 @@ value := opt.UnwrapOr("default")
199203

200204
// Encode to MessagePack.
201205
err := opt.EncodeMsgpack(encoder)
202-
```
203206

204207
## Development
205208

@@ -216,27 +219,12 @@ make coverage
216219

217220
### Run tests
218221

219-
To run default set of tests:
222+
To run default set of tests directly:
220223

221224
```shell
222225
go test ./... -count=1
223226
```
224227

225-
### Documentation
226-
227-
You could run the `godoc` server on `localhost:6060` with the command:
228-
229-
```shell
230-
make godoc_run
231-
```
232-
233-
And open the generated documentation in another terminal or use the
234-
[link][godoc-url]:
235-
236-
```shell
237-
make godoc_open
238-
```
239-
240228
## License
241229

242230
BSD 2-Clause License

0 commit comments

Comments
 (0)