Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ with optional values:

```go
// Create an optional with a value.
opt := SomeOptionalString("hello")
opt := option.SomeString("hello")

// Check if a value is present.
if opt.IsSome() {
Expand All @@ -79,6 +79,30 @@ value := opt.UnwrapOr("default")
err := opt.EncodeMsgpack(encoder)
```

### Using generic approach

```go
type SomeType struct {
name string
number int
}

// Create an optional with a value.
opt := option.Some(SomeType{"hello", 42})

// Check if a value is present.
if opt.IsSome() {
value := opt.Unwrap()
fmt.Println(value)
}

// Use a default value if none.
value := opt.UnwrapOr(SomeType{"default", 0})

// Encode to MessagePack.
err := opt.EncodeMsgpack(encoder)
```

### Usage with go-tarantool

It may be necessary to use an optional type in a structure. For example,
Expand Down Expand Up @@ -147,7 +171,7 @@ while ensuring proper encoding and decoding when using MessagePack.
- `SomeXxx(value)` - Create an optional with a value
- `NoneXxx()` - Create an empty optional
- `Unwrap()`, `UnwrapOr()`, `UnwrapOrElse()` - Value extraction
- `IsSome()`, `IsNone()` - Presence checking
- `IsSome()`, `IsNil()` - Presence checking
- Full MessagePack `CustomEncoder` and `CustomDecoder` implementation
- Type-safe operations

Expand Down Expand Up @@ -240,12 +264,13 @@ For example, to generate an optional type for `github.com/google/uuid.UUID`:

### Using Generated Types

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

```go
// Create an optional with a value.
opt := SomeOptionalString("hello")
opt := option.SomeString("hello")

// Check if a value is present.
if opt.IsSome() {
Expand Down Expand Up @@ -329,7 +354,7 @@ At this point we can see already that the alternatives (based on pointer and sli

Now let's check encoding and decoding.

## Encode + Decode
#### Encode + Decode

```
# int
Expand Down
24 changes: 0 additions & 24 deletions bool_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 112 additions & 0 deletions bool_gen_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 0 additions & 24 deletions byte_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading