https://github.com/josestg/optional
Optional Value: a simple value to replace if-else statement for value initialization.
https://github.com/josestg/optional
Last synced: about 1 month ago
JSON representation
Optional Value: a simple value to replace if-else statement for value initialization.
- Host: GitHub
- URL: https://github.com/josestg/optional
- Owner: josestg
- License: mit
- Created: 2023-01-14T18:35:48.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-01-14T18:38:41.000Z (over 2 years ago)
- Last Synced: 2025-01-31T06:47:04.627Z (3 months ago)
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Go Optional Value
A simple value to replace if-else statement for value initialization.
## Installation
```bash
go get github.com/josestg/optional
```> Minimum Go version: 1.18, because it uses generics.
## Motivation
For example, we have function that gets account id from context, and if not found, we want to generate a new one.
Basically, we can do it using if-else statement like this:```go
package mainimport (
"context"
)func RequestIDFromContext(ctx context.Context) (string, bool) {
v, ok := ctx.Value("request-id").(string)
return v, ok
}func main() {
ctx := context.Background()
id, ok := RequestIDFromContext(ctx)
if !ok {
id = "generated-id"
}println(id) // generated-id
}```
But, we can simplify it using `optional.Value` like this:
```go
package main
import (
"context"
"github.com/josestg/optional"
)func RequestIDFromContext(ctx context.Context) optional.Value[string, bool] {
return optional.New(
optional.OK,
func() (string, bool) {
v, ok := ctx.Value("request-id").(string)
return v, ok
},
)
}func main() {
ctx := context.Background()
id := RequestIDFromContext(ctx).Or("generated-id")
println(id) // generated-id
}
```How it works:
- `optional.New` takes 2 arguments, first is `optional.Predicate[P]` and the second one is `optional.Supplier[T, P]`.
- `optional.Supplier[T, P]` is a function that returns `T` (value) and `P` (predicate), the supplier the function that produces the value.
- `optional.Predicate[P]` is a function that takes `P` as argument to determine if the value is present or absent.
- when predicate returns `false` or absent, the value will be replaced with the value from `optional.Value.Or` method or `optional.Value.OrFetch` method.This is another example, by using custom predicate:
```go
package mainimport (
"github.com/josestg/optional"
)func OddPredicate(v int) bool {
return v%2 == 1
}func OnlyOdd() optional.Value[string, int] {
return optional.New(
OddPredicate,
func() (string, int) {
return "only-odd", 112
},
)
}func main() {
id := OnlyOdd().Or("my-fallback")
println(id) // my-fallback
}
```