mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-18 05:42:09 +01:00
fn: add Sink to Result
This commit is contained in:
parent
5dec35426c
commit
9bbd327a10
10
fn/result.go
10
fn/result.go
@ -224,6 +224,16 @@ func AndThen2[A, B, C any](ra Result[A], rb Result[B],
|
||||
})
|
||||
}
|
||||
|
||||
// Sink consumes a Result, either propagating its error or processing its
|
||||
// success value with a function that can fail.
|
||||
func (r Result[A]) Sink(f func(A) error) error {
|
||||
if r.IsErr() {
|
||||
return r.right
|
||||
}
|
||||
|
||||
return f(r.left)
|
||||
}
|
||||
|
||||
// TransposeResOpt transposes the Result[Option[A]] into a Option[Result[A]].
|
||||
// This has the effect of leaving an A value alone while inverting the Result
|
||||
// and Option layers. If there is no internal A value, it will convert the
|
||||
|
@ -70,3 +70,29 @@ func TestPropTransposeResOptInverts(t *testing.T) {
|
||||
|
||||
require.NoError(t, quick.Check(f, nil))
|
||||
}
|
||||
|
||||
func TestSinkOnErrNoContinutationCall(t *testing.T) {
|
||||
called := false
|
||||
res := Err[uint8](errors.New("err")).Sink(
|
||||
func(a uint8) error {
|
||||
called = true
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
require.False(t, called)
|
||||
require.NotNil(t, res)
|
||||
}
|
||||
|
||||
func TestSinkOnOkContinuationCall(t *testing.T) {
|
||||
called := false
|
||||
res := Ok(uint8(1)).Sink(
|
||||
func(a uint8) error {
|
||||
called = true
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
require.True(t, called)
|
||||
require.Nil(t, res)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user