mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-29 23:21:12 +02:00
fn: add conversions between Result and Option
This commit is contained in:
19
fn/option.go
19
fn/option.go
@@ -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...)),
|
||||
}
|
||||
}
|
||||
|
@@ -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)),
|
||||
)
|
||||
}
|
||||
|
14
fn/result.go
14
fn/result.go
@@ -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)
|
||||
|
@@ -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](),
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user