Skip to content

Commit

Permalink
refine result package
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuan.li committed Jan 2, 2025
1 parent a81b61e commit 52d9e49
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions result/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package result_test
import (
"fmt"
"strconv"
"testing"

"github.com/ayonli/goext/result"
"github.com/stretchr/testify/assert"
)

func ExampleWrap() {
Expand Down Expand Up @@ -44,3 +46,17 @@ func ExampleUnwrap() {
// Output:
// 30
}

func TestWrap(t *testing.T) {
mathAdd := func(input1 string, input2 string) (int, error) {
return result.Wrap(func() (int, error) {
num1 := result.Unwrap(strconv.Atoi(input1))
num2 := result.Unwrap(strconv.Atoi(input2))
return num1 + num2, nil
})
}

res, err := mathAdd("10", "b")
assert.Equal(t, 0, res)
assert.Equal(t, "strconv.Atoi: parsing \"b\": invalid syntax", err.Error())
}
2 changes: 1 addition & 1 deletion result/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
)

func Wrap[R any](fn func() (value R, err error)) (value R, err error) {
func Wrap[T any](fn func() (value T, err error)) (value T, err error) {
defer func() {
if re := recover(); re != nil {
if _err, ok := re.(error); ok {
Expand Down

0 comments on commit 52d9e49

Please sign in to comment.