Skip to content

Commit

Permalink
Merge pull request #21 from moznion/support_OrElse
Browse files Browse the repository at this point in the history
Support `#OrElse()` that supports to return the `Option` value according to the actual value existence
  • Loading branch information
moznion authored Dec 7, 2022
2 parents c4d60f5 + dccaf07 commit 54d6aaa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ and more detailed examples are here: [./examples_test.go](./examples_test.go).
- [Option[T]#Take() (T, error)](https://pkg.go.dev/github.com/moznion/go-optional#Option.Take)
- [Option[T]#TakeOr(fallbackValue T) T](https://pkg.go.dev/github.com/moznion/go-optional#Option.TakeOr)
- [Option[T]#TakeOrElse(fallbackFunc func() T) T](https://pkg.go.dev/github.com/moznion/go-optional#Option.TakeOrElse)
- [Option[T]#OrElse(fallbackOptionValue Option[T]) Option[T]](https://pkg.go.dev/github.com/moznion/go-optional#Option.OrElse)
- [Option[T]#Filter(predicate func(v T) bool) Option[T]](https://pkg.go.dev/github.com/moznion/go-optional#Option.Filter)
- [Option[T]#IfSome(f func(v T))](https://pkg.go.dev/github.com/moznion/go-optional#Option.IfSome)
- [Option[T]#IfSomeWithError(f func(v T) error) error](https://pkg.go.dev/github.com/moznion/go-optional#Option.IfSomeWithError)
Expand Down
14 changes: 14 additions & 0 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,3 +550,17 @@ func ExampleFlatMapOrWithError() {
// err is nil: false
//
}

func ExampleOption_OrElse() {
fallback := Some[string]("fallback")

some := Some[string]("actual")
fmt.Printf("%s\n", some.OrElse(fallback))

none := None[string]()
fmt.Printf("%s\n", none.OrElse(fallback))

// Output:
// Some[actual]
// Some[fallback]
}
9 changes: 9 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ func (o Option[T]) TakeOrElse(fallbackFunc func() T) T {
return o[value]
}

// OrElse returns the Option value according to the actual value existence.
// If the receiver's Option value is Some, this function pass-through that to return. Otherwise, this value returns the `fallbackOptionValue`.
func (o Option[T]) OrElse(fallbackOptionValue Option[T]) Option[T] {
if o.IsNone() {
return fallbackOptionValue
}
return o
}

// Filter returns self if the Option has a value and the value matches the condition of the predicate function.
// In other cases (i.e. it doesn't match with the predicate or the Option is None), this returns None value.
func (o Option[T]) Filter(predicate func(v T) bool) Option[T] {
Expand Down
7 changes: 7 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,10 @@ func TestOption_String(t *testing.T) {
assert.Equal(t, "Some[mystr]", Some[*MyStringer](&MyStringer{}).String())
assert.Equal(t, "None[]", None[*MyStringer]().String())
}

func TestOption_OrElse(t *testing.T) {
fallback := Some[string]("fallback")

assert.EqualValues(t, Some[string]("actual").OrElse(fallback).Unwrap(), "actual")
assert.EqualValues(t, None[string]().OrElse(fallback).Unwrap(), "fallback")
}

0 comments on commit 54d6aaa

Please sign in to comment.