Skip to content

Commit

Permalink
feat: adding Errf
Browse files Browse the repository at this point in the history
  • Loading branch information
samber committed Sep 25, 2023
1 parent 20cc301 commit 8ccc38e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
inputs:
semver:
type: string
description: Release semver
description: 'Semver (eg: v1.2.3)'
required: true

jobs:
Expand Down Expand Up @@ -44,7 +44,7 @@ jobs:
git config --global user.email "${{ github.triggering_actor}}@users.noreply.github.com"
git add .
git commit -m 'bump ${{ inputs.semver }}'
git commit --allow-empty -m 'bump ${{ inputs.semver }}'
git tag ${{ inputs.semver }}
git push origin ${{ inputs.semver }}
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ Constructors:

- `mo.Ok()` [doc](https://pkg.go.dev/github.com/samber/mo#Ok) - [play](https://go.dev/play/p/PDwADdzNoyZ)
- `mo.Err()` [doc](https://pkg.go.dev/github.com/samber/mo#Err) - [play](https://go.dev/play/p/PDwADdzNoyZ)
- `mo.Errf()` [doc](https://pkg.go.dev/github.com/samber/mo#Errf) - [play](https://go.dev/play/p/N43w92SM-Bs)
- `mo.TupleToResult()` [doc](https://pkg.go.dev/github.com/samber/mo#TupleToResult) - [play](https://go.dev/play/p/KWjfqQDHQwa)
- `mo.Try()` [doc](https://pkg.go.dev/github.com/samber/mo#Try) - [play](https://go.dev/play/p/ilOlQx-Mx42)

Expand Down Expand Up @@ -385,7 +386,7 @@ make watch-test

Give a ⭐️ if this project helped you!

[![support us](https://c5.patreon.com/external/logo/become_a_patron_button.png)](https://www.patreon.com/samber)
[![GitHub Sponsors](https://img.shields.io/github/sponsors/samber?style=for-the-badge)](https://github.com/sponsors/samber)

## 📝 License

Expand Down
9 changes: 9 additions & 0 deletions result.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package mo

import "fmt"

// Ok builds a Result when value is valid.
// Play: https://go.dev/play/p/PDwADdzNoyZ
func Ok[T any](value T) Result[T] {
Expand All @@ -18,6 +20,13 @@ func Err[T any](err error) Result[T] {
}
}

// Errf builds a Result when value is invalid.
// Errf formats according to a format specifier and returns the error as a value that satisfies Result[T].
// Play: https://go.dev/play/p/N43w92SM-Bs
func Errf[T any](format string, a ...any) Result[T] {
return Err[T](fmt.Errorf(format, a...))
}

// TupleToResult convert a pair of T and error into a Result.
// Play: https://go.dev/play/p/KWjfqQDHQwa
func TupleToResult[T any](value T, err error) Result[T] {
Expand Down
9 changes: 9 additions & 0 deletions result_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ func ExampleErr() {
// Output: 1234 error
}

func ExampleErrf() {
ko := Errf[int]("error")
result := ko.OrElse(1234)
_err := ko.Error()

fmt.Println(result, _err)
// Output: 1234 error
}

func ExampleTupleToResult() {
randomFunc := func() (int, error) {
return 42, err
Expand Down
6 changes: 6 additions & 0 deletions result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ func TestResultErr(t *testing.T) {
is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, Err[int](assert.AnError))
}

func TestResultErrf(t *testing.T) {
is := assert.New(t)

is.Equal(Result[int]{value: 0, isErr: true, err: assert.AnError}, Errf[int](assert.AnError.Error()))
}

func TestResultTupleToResult(t *testing.T) {
is := assert.New(t)

Expand Down

0 comments on commit 8ccc38e

Please sign in to comment.