fn: add conversions between Result and Option
This commit is contained in:
4 files changed
+58
No files matched your search
@@ -1,6 +1,7 @@
|
|||||||
package fn
|
package fn
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"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)
|
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...)),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package fn
|
package fn
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -9,3 +11,18 @@ import (
|
|||||||
func TestOptionUnwrapOrFail(t *testing.T) {
|
func TestOptionUnwrapOrFail(t *testing.T) {
|
||||||
require.Equal(t, Some(1).UnwrapOrFail(t), 1)
|
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)),
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -79,15 +79,29 @@ func (r Result[T]) MapErr(f func(error) error) Result[T] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Option returns the success value as an Option.
|
// Option returns the success value as an Option.
|
||||||
|
//
|
||||||
|
// Deprecated: Use OkToSome instead.
|
||||||
func (r Result[T]) Option() Option[T] {
|
func (r Result[T]) Option() Option[T] {
|
||||||
return r.Either.LeftToOption()
|
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.
|
// WhenResult executes the given function if the Result is a success.
|
||||||
|
//
|
||||||
|
// Deprecated: Use WhenOk instead.
|
||||||
func (r Result[T]) WhenResult(f func(T)) {
|
func (r Result[T]) WhenResult(f func(T)) {
|
||||||
r.WhenLeft(f)
|
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.
|
// WhenErr executes the given function if the Result is an error.
|
||||||
func (r Result[T]) WhenErr(f func(error)) {
|
func (r Result[T]) WhenErr(f func(error)) {
|
||||||
r.WhenRight(f)
|
r.WhenRight(f)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package fn
|
package fn
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -9,3 +10,10 @@ import (
|
|||||||
func TestResultUnwrapOrFail(t *testing.T) {
|
func TestResultUnwrapOrFail(t *testing.T) {
|
||||||
require.Equal(t, Ok(1).UnwrapOrFail(t), 1)
|
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](),
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in new issue
Block a user