Skip to content

Commit

Permalink
feat: invokeAsyncFn fill out slice if the function panics.
Browse files Browse the repository at this point in the history
  • Loading branch information
ghosind committed Apr 12, 2024
1 parent 6eb73f5 commit 28b43da
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
12 changes: 7 additions & 5 deletions async.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"reflect"

"github.com/ghosind/utils"
"github.com/ghosind/go-try"
)

// AsyncFn is the function to run, the function can be a function without any restriction that accepts any parameters and any return values. For the best practice, please define the function like the following styles:
Expand Down Expand Up @@ -100,13 +100,15 @@ func invokeAsyncFn(fn AsyncFn, ctx context.Context, params []any) ([]any, error)
in := makeFuncIn(ft, ctx, params)

numRet := ft.NumOut()
ret := make([]any, 0, numRet)
ret := make([]any, numRet)

err := utils.Try(func() error {
_, err := try.Try(func() {
out = fv.Call(in)
return nil
})
if err != nil {
for i := 0; i < numRet; i++ {
ret[i] = reflect.Zero(ft.Out(i)).Interface()
}
return ret, err
}

Expand All @@ -118,7 +120,7 @@ func invokeAsyncFn(fn AsyncFn, ctx context.Context, params []any) ([]any, error)
}
}
for i := 0; i < numRet; i++ {
ret = append(ret, out[i].Interface())
ret[i] = out[i].Interface()
}

return ret, err
Expand Down
6 changes: 6 additions & 0 deletions async_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ func TestInvokeAsyncFn(t *testing.T) {
ret, err = invokeAsyncFn(func() (int, string, error) { return 1, "test", nil }, ctx, nil)
a.NilNow(err)
a.EqualNow(ret, []any{1, "test", nil})

ret, err = invokeAsyncFn(func() int {
panic(expectErr)
}, ctx, nil)
a.EqualNow(err, expectErr)
a.EqualNow(ret, []any{0})
}

func TestInvokeAsyncFnWithParams(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@ go 1.18

require (
github.com/ghosind/go-assert v1.0.3
github.com/ghosind/utils v0.2.0
github.com/ghosind/go-try v1.0.0
)

require golang.org/x/exp v0.0.0-20231127185646-65229373498e // indirect
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
github.com/ghosind/go-assert v1.0.3 h1:K77TgGbzCNLPr19MYm2VkYySwOmRixLUZx9Z0BY96O4=
github.com/ghosind/go-assert v1.0.3/go.mod h1:y1ayrGzScwcWG3TiTjJuFstq9oUHUE1Sk+DzH6CKVHY=
github.com/ghosind/utils v0.2.0 h1:U7byV6dLxPY55o89DnrHKLVeOzQMIuATNOjnlyK0ko0=
github.com/ghosind/utils v0.2.0/go.mod h1:TKXXia04sFjVu2qdi//Tqoo5Q87IsOeK94J7Vu+bOAk=
golang.org/x/exp v0.0.0-20231127185646-65229373498e h1:Gvh4YaCaXNs6dKTlfgismwWZKyjVZXwOPfIyUaqU3No=
golang.org/x/exp v0.0.0-20231127185646-65229373498e/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
github.com/ghosind/go-try v1.0.0 h1:ek3DwNsvFWBe7lm7CwgEbEfyCNgxRPmmRk9bjleh3DM=
github.com/ghosind/go-try v1.0.0/go.mod h1:M1lQctWgkFsroI1V4AeeEWzS/JX8CINL1qNfTebUNbs=

0 comments on commit 28b43da

Please sign in to comment.