@@ -22,8 +22,16 @@ high performance due to:
22
22
* [ Installation] ( #installation )
23
23
* [ Documentation] ( #documentation )
24
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 )
25
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 )
26
33
* [ Development] ( #development )
34
+ * [ Run tests] ( #run-tests )
27
35
* [ License] ( #license )
28
36
29
37
## Installation
@@ -32,48 +40,46 @@ high performance due to:
32
40
go install github.com/tarantool/go-option@latest
33
41
```
34
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
+
35
58
## Quick start
36
59
37
- ``` Go
38
- package main
60
+ ### Using pre-generated optional types
39
61
40
- import (
41
- " bytes"
42
- " fmt"
62
+ Generated types follow the pattern Optional<TypeName > and provide methods for working
63
+ with optional values:
43
64
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 " )
47
68
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)
73
73
}
74
+
75
+ // Use a default value if none.
76
+ value := opt.UnwrapOr (" default" )
77
+
78
+ // Encode to MessagePack.
79
+ err := opt.EncodeMsgpack (encoder)
74
80
```
75
81
76
- ## Usage with go-tarantool
82
+ ### Usage with go-tarantool
77
83
78
84
It may be necessary to use an optional type in a structure. For example,
79
85
to distinguish between a nil value and a missing one.
@@ -145,17 +151,15 @@ while ensuring proper encoding and decoding when using MessagePack.
145
151
- Full MessagePack ` CustomEncoder ` and ` CustomDecoder ` implementation
146
152
- Type-safe operations
147
153
148
- ### Installation
154
+ ### Gentype installation
149
155
150
156
``` bash
151
157
go install github.com/tarantool/go-option/cmd/gentypes@latest
152
158
# OR (for go version 1.24+)
153
159
go get -tool github.com/tarantool/go-option/cmd/gentypes@latest
154
160
```
155
161
156
- ### Usage
157
-
158
- #### Generating Optional Types
162
+ ### Generating Optional Types
159
163
160
164
To generate optional types for existing types in a package:
161
165
@@ -179,7 +183,7 @@ Flags:
179
183
-128 and 127, no default value)
180
184
• ` -verbose ` : Enable verbose output (default: ` false ` )
181
185
182
- #### Using Generated Types
186
+ ### Using Generated Types
183
187
184
188
Generated types follow the pattern Optional<TypeName > and provide methods for working
185
189
with optional values:
@@ -199,7 +203,6 @@ value := opt.UnwrapOr("default")
199
203
200
204
// Encode to MessagePack.
201
205
err := opt.EncodeMsgpack (encoder)
202
- ```
203
206
204
207
## Development
205
208
@@ -216,27 +219,12 @@ make coverage
216
219
217
220
### Run tests
218
221
219
- To run default set of tests:
222
+ To run default set of tests directly :
220
223
221
224
` ` ` shell
222
225
go test ./... -count=1
223
226
` ` `
224
227
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
-
240
228
## License
241
229
242
230
BSD 2 -Clause License
0 commit comments