Skip to content

Commit

Permalink
Merge pull request #22 from isac322/or_else
Browse files Browse the repository at this point in the history
feat: add `OrElse()`
  • Loading branch information
moznion authored Dec 8, 2022
2 parents 54d6aaa + 175be4e commit e517051
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ 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]#Or(fallbackOptionValue Option[T]) Option[T]](https://pkg.go.dev/github.com/moznion/go-optional#Option.Or)
- [Option[T]#OrElse(fallbackOptionFunc func() 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
20 changes: 17 additions & 3 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -551,14 +551,28 @@ func ExampleFlatMapOrWithError() {
//
}

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

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

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

// Output:
// Some[actual]
// Some[fallback]
}

func ExampleOption_OrElse() {
fallbackFunc := func() Option[string] { return Some[string]("fallback") }

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

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

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

// OrElse returns the Option value according to the actual value existence.
// Or 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] {
func (o Option[T]) Or(fallbackOptionValue Option[T]) Option[T] {
if o.IsNone() {
return fallbackOptionValue
}
return o
}

// 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 executes `fallbackOptionFunc` and returns the result value of that function.
func (o Option[T]) OrElse(fallbackOptionFunc func() Option[T]) Option[T] {
if o.IsNone() {
return fallbackOptionFunc()
}
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
13 changes: 10 additions & 3 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,16 @@ func TestOption_String(t *testing.T) {
assert.Equal(t, "None[]", None[*MyStringer]().String())
}

func TestOption_OrElse(t *testing.T) {
func TestOption_Or(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")
assert.EqualValues(t, Some[string]("actual").Or(fallback).Unwrap(), "actual")
assert.EqualValues(t, None[string]().Or(fallback).Unwrap(), "fallback")
}

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

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

0 comments on commit e517051

Please sign in to comment.