Skip to content

Commit 54ff5d9

Browse files
patapenka-alexeybigbes
authored andcommitted
docs: update README
Closes #TNTP-3736
1 parent 93a7028 commit 54ff5d9

File tree

3 files changed

+164
-19
lines changed

3 files changed

+164
-19
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@ coveralls-deps:
3333
@echo "Installing coveralls"
3434
@go get github.com/mattn/goveralls
3535
@go install github.com/mattn/goveralls
36+
37+
.PHONY: godoc_run
38+
godoc_run:
39+
godoc -http=:6060
40+
41+
.PHONY: godoc_open
42+
godoc_open:
43+
xdg-open http://localhost:6060/pkg/$(shell go list -f "{{.ImportPath}}")

README.md

Lines changed: 153 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,121 @@
1010

1111
# go-option: library to work with optional types
1212

13-
## Pre-generated basic optional types
13+
Package `option` implements effective and useful instruments to work
14+
with optional types in Go. It eliminates code doubling and provides
15+
high performance due to:
16+
- no memory allocations
17+
- serialization without reflection (at least for pre-generated types)
18+
- support for basic and custom types
19+
20+
## Table of contents
21+
22+
* [Installation](#installation)
23+
* [Documentation](#documentation)
24+
* [Quick start](#quick-start)
25+
* [Using pre-generated optional types](#using-pre-generated-optional-types)
26+
* [Usage with go-tarantool](#usage-with-go-tarantool)
27+
* [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)
33+
* [Development](#development)
34+
* [Run tests](#run-tests)
35+
* [License](#license)
36+
37+
## Installation
38+
39+
```shell
40+
go install github.com/tarantool/go-option@latest
41+
```
42+
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+
58+
## Quick start
59+
60+
### Using pre-generated optional types
61+
62+
Generated types follow the pattern Optional<TypeName> and provide methods for working
63+
with optional values:
64+
65+
```go
66+
// Create an optional with a value.
67+
opt := SomeOptionalString("hello")
68+
69+
// Check if a value is present.
70+
if opt.IsSome() {
71+
value := opt.Unwrap()
72+
fmt.Println(value)
73+
}
74+
75+
// Use a default value if none.
76+
value := opt.UnwrapOr("default")
77+
78+
// Encode to MessagePack.
79+
err := opt.EncodeMsgpack(encoder)
80+
```
81+
82+
### Usage with go-tarantool
83+
84+
It may be necessary to use an optional type in a structure. For example,
85+
to distinguish between a nil value and a missing one.
86+
87+
```Go
88+
package main
89+
90+
import (
91+
"github.com/tarantool/go-option"
92+
tarantool "github.com/tarantool/go-tarantool/v2"
93+
)
94+
95+
type User struct {
96+
// may be used in conjunciton with 'msgpack:",omitempty"' directive to skip fields
97+
_msgpack struct{} `msgpack:",asArray"` //nolint: structcheck,unused
98+
Name string
99+
Phone option.String
100+
}
101+
102+
func main() {
103+
var conn *tarantool.Doer
104+
// Initialize tarantool connection
105+
106+
// Imagine you get a slice of users from Tarantool.
107+
users := []User{
108+
{
109+
Name: "Nosipho Nnenne",
110+
Phone: option.SomeString("+15056463408"),
111+
},
112+
{
113+
Name: "Maryamu Efe",
114+
Phone: option.NoneString(),
115+
},
116+
{
117+
Name: "Venera Okafor",
118+
},
119+
}
120+
121+
for id, user := range users {
122+
conn.Do(
123+
tarantool.NewInsertRequest("users").Tuple(user),
124+
)
125+
}
126+
}
127+
```
14128

15129
## Gentype Utility
16130

@@ -21,7 +135,7 @@ serialization support.
21135

22136
Gentype generates wrapper types for various Go primitives and
23137
custom types that implement optional (some/none) semantics with
24-
full MessagePack serialization capabilities. These generated types
138+
full MessagePack serialization capabilities. These generated types
25139
are useful for representing values that may or may not be present,
26140
while ensuring proper encoding and decoding when using MessagePack.
27141

@@ -30,24 +144,22 @@ while ensuring proper encoding and decoding when using MessagePack.
30144
- Generates optional types for built-in types (bool, int, float, string, etc.)
31145
- Supports custom types with MessagePack extension serialization
32146
- Provides common optional type operations:
33-
- `SomeXxx(value)` - Create an optional with a value
34-
- `NoneXxx()` - Create an empty optional
35-
- `Unwrap()`, `UnwrapOr()`, `UnwrapOrElse()` - Value extraction
36-
- `IsSome()`, `IsNone()` - Presence checking
147+
- `SomeXxx(value)` - Create an optional with a value
148+
- `NoneXxx()` - Create an empty optional
149+
- `Unwrap()`, `UnwrapOr()`, `UnwrapOrElse()` - Value extraction
150+
- `IsSome()`, `IsNone()` - Presence checking
37151
- Full MessagePack `CustomEncoder` and `CustomDecoder` implementation
38152
- Type-safe operations
39153

40-
### Installation
154+
### Gentype installation
41155

42156
```bash
43157
go install github.com/tarantool/go-option/cmd/gentypes@latest
44158
# OR (for go version 1.24+)
45159
go get -tool github.com/tarantool/go-option/cmd/gentypes@latest
46160
```
47161

48-
### Usage
49-
50-
#### Generating Optional Types
162+
### Generating Optional Types
51163

52164
To generate optional types for existing types in a package:
53165

@@ -66,12 +178,12 @@ Or you can use it to generate file from go:
66178

67179
Flags:
68180

69-
`-package`: Path to the Go package containing types to wrap (default: `"."`)
70-
`-ext-code`: MessagePack extension code to use for custom types (must be between
71-
-128 and 127, no default value)
72-
`-verbose`: Enable verbose output (default: `false`)
181+
`-package`: Path to the Go package containing types to wrap (default: `"."`)
182+
`-ext-code`: MessagePack extension code to use for custom types (must be between
183+
-128 and 127, no default value)
184+
`-verbose`: Enable verbose output (default: `false`)
73185

74-
#### Using Generated Types
186+
### Using Generated Types
75187

76188
Generated types follow the pattern Optional<TypeName> and provide methods for working
77189
with optional values:
@@ -93,6 +205,31 @@ value := opt.UnwrapOr("default")
93205
err := opt.EncodeMsgpack(encoder)
94206
```
95207

208+
## Development
209+
210+
You could use our Makefile targets:
211+
212+
```shell
213+
make codespell
214+
make test
215+
make testrace
216+
make coveralls-deps
217+
make coveralls
218+
make coverage
219+
```
220+
221+
### Run tests
222+
223+
To run default set of tests directly:
224+
225+
```shell
226+
go test ./... -count=1
227+
```
228+
229+
## License
230+
231+
BSD 2-Clause License
232+
96233
[godoc-badge]: https://pkg.go.dev/badge/github.com/tarantool/go-option.svg
97234
[godoc-url]: https://pkg.go.dev/github.com/tarantool/go-option
98235
[actions-badge]: https://github.com/tarantool/go-option/actions/workflows/testing.yaml/badge.svg
@@ -101,4 +238,4 @@ err := opt.EncodeMsgpack(encoder)
101238
[coverage-url]: https://coveralls.io/github/tarantool/go-option?branch=master
102239
[telegram-badge]: https://img.shields.io/badge/Telegram-join%20chat-blue.svg
103240
[telegram-en-url]: http://telegram.me/tarantool
104-
[telegram-ru-url]: http://telegram.me/tarantoolru
241+
[telegram-ru-url]: http://telegram.me/tarantoolru

doc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// A Generic[T] can either contain a value of type T (Some) or be empty (None).
33
//
44
// This is useful for:
5-
// - Clearly representing nullable fields in structs.
6-
// - Avoiding nil pointer dereferences.
7-
// - Providing explicit intent about optional values.
5+
// - Clearly representing nullable fields in structs.
6+
// - Avoiding nil pointer dereferences.
7+
// - Providing explicit intent about optional values.
88
package option

0 commit comments

Comments
 (0)