Skip to content

Commit

Permalink
Renamed factory functions in res package
Browse files Browse the repository at this point in the history
  • Loading branch information
jpfourny committed Mar 28, 2024
1 parent 76e9449 commit 1c3064c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
26 changes: 13 additions & 13 deletions pkg/res/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()))
}
36 changes: 18 additions & 18 deletions pkg/res/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 1c3064c

Please sign in to comment.