fn: add conversions between Result and Option

This commit is contained in:
Keagan McClelland
2024-08-06 15:38:14 -07:00
parent a741c54175
commit 5faad77f33
4 changed files with 58 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
package fn
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
@@ -232,3 +233,21 @@ func OptionToRight[O, L, R any](o Option[O], l L) Either[L, O] {
return NewLeft[L, O](l)
}
// SomeToOk allows you to convert an Option value to a Result with your own
// error. If the Option contained a Some, then the supplied error is ignored
// and Some is converted to Ok.
func (o Option[A]) SomeToOk(err error) Result[A] {
return Result[A]{
OptionToLeft[A, A, error](o, err),
}
}
// SomeToOkf allows you to convert an Option value to a Result with your own
// error message. If the Option contains a Some, then the supplied message is
// ignored and Some is converted to Ok.
func (o Option[A]) SomeToOkf(errString string, args ...interface{}) Result[A] {
return Result[A]{
OptionToLeft[A, A, error](o, fmt.Errorf(errString, args...)),
}
}

View File

@@ -1,6 +1,8 @@
package fn
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/require"
@@ -9,3 +11,18 @@ import (
func TestOptionUnwrapOrFail(t *testing.T) {
require.Equal(t, Some(1).UnwrapOrFail(t), 1)
}
func TestSomeToOk(t *testing.T) {
err := errors.New("err")
require.Equal(t, Some(1).SomeToOk(err), Ok(1))
require.Equal(t, None[uint8]().SomeToOk(err), Err[uint8](err))
}
func TestSomeToOkf(t *testing.T) {
errStr := "err"
require.Equal(t, Some(1).SomeToOkf(errStr), Ok(1))
require.Equal(
t, None[uint8]().SomeToOkf(errStr),
Err[uint8](fmt.Errorf(errStr)),
)
}

View File

@@ -79,15 +79,29 @@ func (r Result[T]) MapErr(f func(error) error) Result[T] {
}
// Option returns the success value as an Option.
//
// Deprecated: Use OkToSome instead.
func (r Result[T]) Option() Option[T] {
return r.Either.LeftToOption()
}
// OkToSome mutes the error value of the result.
func (r Result[T]) OkToSome() Option[T] {
return r.Either.LeftToOption()
}
// WhenResult executes the given function if the Result is a success.
//
// Deprecated: Use WhenOk instead.
func (r Result[T]) WhenResult(f func(T)) {
r.WhenLeft(f)
}
// WhenOk executes the given function if the Result is a success.
func (r Result[T]) WhenOk(f func(T)) {
r.WhenLeft(f)
}
// WhenErr executes the given function if the Result is an error.
func (r Result[T]) WhenErr(f func(error)) {
r.WhenRight(f)

View File

@@ -1,6 +1,7 @@
package fn
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
@@ -9,3 +10,10 @@ import (
func TestResultUnwrapOrFail(t *testing.T) {
require.Equal(t, Ok(1).UnwrapOrFail(t), 1)
}
func TestOkToSome(t *testing.T) {
require.Equal(t, Ok(1).OkToSome(), Some(1))
require.Equal(
t, Err[uint8](errors.New("err")).OkToSome(), None[uint8](),
)
}