From 1c3064ce17eb080d3114b57429b223aae88358ea Mon Sep 17 00:00:00 2001 From: Joseph Fourny Date: Wed, 27 Mar 2024 21:09:43 -0400 Subject: [PATCH] Renamed factory functions in res package --- pkg/res/result.go | 26 +++++++++++++------------- pkg/res/result_test.go | 36 ++++++++++++++++++------------------ 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/pkg/res/result.go b/pkg/res/result.go index e98e829..24361ad 100644 --- a/pkg/res/result.go +++ b/pkg/res/result.go @@ -43,29 +43,29 @@ type Result[T any] interface { Error() opt.Optional[error] } -// OfSuccess returns a successful result with the provided value. -func OfSuccess[T any](val T) Success[T] { +// OK returns a successful result with the provided value. +func OK[T any](val T) Success[T] { return Success[T]{Val: val} } -// OfPartialSuccess returns a partially successful result with the provided value and error. -func OfPartialSuccess[T any](val T, err error) PartialSuccess[T] { +// Partial returns a partially-successful result with the provided value and error. +func Partial[T any](val T, err error) PartialSuccess[T] { return PartialSuccess[T]{Val: val, Err: err} } -// OfFailure returns a failed result with the provided error. -func OfFailure[T any](err error) Failure[T] { +// Fail returns a failed result with the provided error. +func Fail[T any](err error) Failure[T] { return Failure[T]{Err: err} } -// Of returns a result with the provided value and error. +// Maybe returns a result based on the provided value and error. // If the error is nil, the result is successful; otherwise, it is failed. // This is a convenience function that creates a successful or failed result based on the provided error. -func Of[T any](val T, err error) Result[T] { +func Maybe[T any](val T, err error) Result[T] { if err != nil { - return OfFailure[T](err) + return Fail[T](err) } - return OfSuccess[T](val) + return OK[T](val) } // MapValue maps the value of the result to a new value using the provided mapper function. @@ -87,10 +87,10 @@ func MapError[T any](r Result[T], errorMapper func(error) error) Result[T] { // The error mapper function maps the error of the result to a new error. func Map[T, U any](r Result[T], valueMapper func(T) U, errorMapper func(error) error) Result[U] { if r.Succeeded() { - return OfSuccess[U](valueMapper(r.Value().GetOrZero())) + return OK[U](valueMapper(r.Value().GetOrZero())) } if r.PartiallySucceeded() { - return OfPartialSuccess[U](valueMapper(r.Value().GetOrZero()), errorMapper(r.Error().GetOrZero())) + return Partial[U](valueMapper(r.Value().GetOrZero()), errorMapper(r.Error().GetOrZero())) } - return OfFailure[U](errorMapper(r.Error().GetOrZero())) + return Fail[U](errorMapper(r.Error().GetOrZero())) } diff --git a/pkg/res/result_test.go b/pkg/res/result_test.go index bce697e..64663fe 100644 --- a/pkg/res/result_test.go +++ b/pkg/res/result_test.go @@ -6,8 +6,8 @@ import ( "testing" ) -func TestOfSuccess(t *testing.T) { - r := OfSuccess(42) +func TestOK(t *testing.T) { + r := OK(42) if !r.Succeeded() { t.Errorf("expected Succeeded() to be true") } @@ -34,8 +34,8 @@ func TestOfSuccess(t *testing.T) { } } -func TestOfFailure(t *testing.T) { - r := OfFailure[any](errors.New("error")) +func TestFail(t *testing.T) { + r := Fail[any](errors.New("error")) if r.Succeeded() { t.Errorf("expected Succeeded() to be false") } @@ -62,8 +62,8 @@ func TestOfFailure(t *testing.T) { } } -func TestOfPartialSuccess(t *testing.T) { - r := OfPartialSuccess(42, errors.New("error")) +func TestPartial(t *testing.T) { + r := Partial(42, errors.New("error")) if r.Succeeded() { t.Errorf("expected Succeeded() to be false") } @@ -90,9 +90,9 @@ func TestOfPartialSuccess(t *testing.T) { } } -func TestOf(t *testing.T) { +func TestMaybe(t *testing.T) { t.Run("Success", func(t *testing.T) { - r := Of(42, nil) + r := Maybe(42, nil) if !r.Succeeded() { t.Errorf("expected Succeeded() to be true") } @@ -120,7 +120,7 @@ func TestOf(t *testing.T) { }) t.Run("Failure", func(t *testing.T) { - r := Of(42, errors.New("error")) + r := Maybe(42, errors.New("error")) if r.Succeeded() { t.Errorf("expected Succeeded() to be false") } @@ -150,7 +150,7 @@ func TestOf(t *testing.T) { func TestMapValue(t *testing.T) { t.Run("Success", func(t *testing.T) { - r := OfSuccess(42) + r := OK(42) mapped := MapValue(r, func(val int) string { return fmt.Sprintf("%d", val) }) @@ -181,7 +181,7 @@ func TestMapValue(t *testing.T) { }) t.Run("Failure", func(t *testing.T) { - r := OfFailure[int](errors.New("error")) + r := Fail[int](errors.New("error")) mapped := MapValue(r, func(val int) string { return fmt.Sprintf("%d", val) }) @@ -212,7 +212,7 @@ func TestMapValue(t *testing.T) { }) t.Run("PartialSuccess", func(t *testing.T) { - r := OfPartialSuccess(42, errors.New("error")) + r := Partial(42, errors.New("error")) mapped := MapValue(r, func(val int) string { return fmt.Sprintf("%d", val) }) @@ -245,8 +245,8 @@ func TestMapValue(t *testing.T) { func TestMapError(t *testing.T) { t.Run("Success", func(t *testing.T) { - r := OfSuccess(42) - mapped := MapError(r, func(err error) error { + r := OK(42) + mapped := MapError[int](r, func(err error) error { return errors.New("mapped error") }) if !mapped.Succeeded() { @@ -276,8 +276,8 @@ func TestMapError(t *testing.T) { }) t.Run("Failure", func(t *testing.T) { - r := OfFailure[int](errors.New("error")) - mapped := MapError(r, func(err error) error { + r := Fail[int](errors.New("error")) + mapped := MapError[int](r, func(err error) error { return errors.New("mapped error") }) if mapped.Succeeded() { @@ -307,8 +307,8 @@ func TestMapError(t *testing.T) { }) t.Run("PartialSuccess", func(t *testing.T) { - r := OfPartialSuccess(42, errors.New("error")) - mapped := MapError(r, func(err error) error { + r := Partial(42, errors.New("error")) + mapped := MapError[int](r, func(err error) error { return errors.New("mapped error") }) if mapped.Succeeded() {