Skip to content

Commit 2e92b3c

Browse files
committed
doc: add usage examples
Closes TNTP-3732
1 parent bb21fcd commit 2e92b3c

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,13 @@ For example, to generate an optional type for `github.com/google/uuid.UUID`:
240240

241241
### Using Generated Types
242242

243-
Generated types follow the pattern Optional<TypeName> and provide methods for working
244-
with optional values:
243+
Generated types provide methods for working with optional values and 2 constructors for every single type:
244+
`Some<TypeName>` creates option with some value and `None<TypeName>` creates the empty one
245+
(<TypeName> is the original type name started with upper case):
245246

246247
```go
247248
// Create an optional with a value.
248-
opt := SomeOptionalString("hello")
249+
opt := option.SomeString("hello")
249250
250251
// Check if a value is present.
251252
if opt.IsSome() {
@@ -260,6 +261,30 @@ value := opt.UnwrapOr("default")
260261
err := opt.EncodeMsgpack(encoder)
261262
```
262263

264+
### Using Generic approach
265+
266+
```go
267+
type SomeType struct {
268+
name string
269+
number int
270+
}
271+
272+
// Create an optional with a value.
273+
opt := option.Some(SomeType{"hello", 42})
274+
275+
// Check if a value is present.
276+
if opt.IsSome() {
277+
value := opt.Unwrap()
278+
fmt.Println(value)
279+
}
280+
281+
// Use a default value if none.
282+
value := opt.UnwrapOr(SomeType{"default", 0})
283+
284+
// Encode to MessagePack.
285+
err := opt.EncodeMsgpack(encoder)
286+
```
287+
263288
## Development
264289

265290
You could use our Makefile targets:
@@ -329,7 +354,7 @@ At this point we can see already that the alternatives (based on pointer and sli
329354
330355
Now let's check encoding and decoding.
331356
332-
## Encode + Decode
357+
#### Encode + Decode
333358
334359
```
335360
# int

0 commit comments

Comments
 (0)