fn: fix UnwrapOrFail semantics

This commit is contained in:
Keagan McClelland
2024-08-12 12:03:56 -07:00
parent 4778b146cc
commit 7a7f3bdb2c
4 changed files with 35 additions and 14 deletions

View File

@@ -3,6 +3,8 @@ package fn
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
// Result represents a value that can either be a success (T) or an error.
@@ -114,13 +116,11 @@ func (r Result[T]) UnwrapOrElse(f func() T) T {
func (r Result[T]) UnwrapOrFail(t *testing.T) T {
t.Helper()
if r.IsErr() {
t.Fatalf("Result[%T] contained error: %v", r.left, r.right)
}
require.True(
t, r.IsOk(), "Result[%T] contained error: %v", r.left, r.right,
)
var zero T
return zero
return r.left
}
// FlatMap applies a function that returns a Result to the success value if it