|
8 | 8 | [![Telegram EN][telegram-badge]][telegram-en-url]
|
9 | 9 | [![Telegram RU][telegram-badge]][telegram-ru-url]
|
10 | 10 |
|
| 11 | +# go-option: library to work with optional types |
| 12 | + |
| 13 | +## Pre-generated basic optional types |
| 14 | + |
| 15 | +## Gentype Utility |
| 16 | + |
| 17 | +A Go code generator for creating optional types with MessagePack |
| 18 | +serialization support. |
| 19 | + |
| 20 | +### Overview |
| 21 | + |
| 22 | +Gentype generates wrapper types for various Go primitives and |
| 23 | +custom types that implement optional (some/none) semantics with |
| 24 | +full MessagePack serialization capabilities. These generated types |
| 25 | +are useful for representing values that may or may not be present, |
| 26 | +while ensuring proper encoding and decoding when using MessagePack. |
| 27 | + |
| 28 | +### Features |
| 29 | + |
| 30 | +- Generates optional types for built-in types (bool, int, float, string, etc.) |
| 31 | +- Supports custom types with MessagePack extension serialization |
| 32 | +- 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 |
| 37 | +- Full MessagePack `CustomEncoder` and `CustomDecoder` implementation |
| 38 | +- Type-safe operations |
| 39 | + |
| 40 | +### Installation |
| 41 | + |
| 42 | +```bash |
| 43 | +go install github.com/tarantool/go-option/cmd/gentypes@latest |
| 44 | +# OR (for go version 1.24+) |
| 45 | +go get -tool github.com/tarantool/go-option/cmd/gentypes@latest |
| 46 | +``` |
| 47 | + |
| 48 | +### Usage |
| 49 | + |
| 50 | +#### Generating Optional Types |
| 51 | + |
| 52 | +To generate optional types for existing types in a package: |
| 53 | + |
| 54 | +```bash |
| 55 | +gentypes -package ./path/to/package -ext-code 123 |
| 56 | +# OR (for go version 1.24+) |
| 57 | +go tool gentypes -package ./path/to/package -ext-code 123 |
| 58 | +``` |
| 59 | + |
| 60 | +Or you can use it to generate file from go: |
| 61 | +```go |
| 62 | +//go:generate go run github.com/tarantool/go-option/cmd/gentypes@latest -ext-code 123 |
| 63 | +// OR (for go version 1.24+) |
| 64 | +//go:generate go tool gentypes -ext-code 123 |
| 65 | +``` |
| 66 | + |
| 67 | +Flags: |
| 68 | + |
| 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`) |
| 73 | + |
| 74 | +#### Using Generated Types |
| 75 | + |
| 76 | +Generated types follow the pattern Optional<TypeName> and provide methods for working |
| 77 | +with optional values: |
| 78 | + |
| 79 | +```go |
| 80 | +// Create an optional with a value. |
| 81 | +opt := SomeOptionalString("hello") |
| 82 | + |
| 83 | +// Check if a value is present. |
| 84 | +if opt.IsSome() { |
| 85 | + value := opt.Unwrap() |
| 86 | + fmt.Println(value) |
| 87 | +} |
| 88 | + |
| 89 | +// Use a default value if none. |
| 90 | +value := opt.UnwrapOr("default") |
| 91 | + |
| 92 | +// Encode to MessagePack. |
| 93 | +err := opt.EncodeMsgpack(encoder) |
| 94 | +``` |
| 95 | + |
11 | 96 | [godoc-badge]: https://pkg.go.dev/badge/github.com/tarantool/go-option.svg
|
12 | 97 | [godoc-url]: https://pkg.go.dev/github.com/tarantool/go-option
|
13 | 98 | [actions-badge]: https://github.com/tarantool/go-option/actions/workflows/testing.yaml/badge.svg
|
|
0 commit comments