From 28b43da1b1655918607f588ca84857a478ca9240 Mon Sep 17 00:00:00 2001 From: ghosind Date: Fri, 12 Apr 2024 22:00:00 +0800 Subject: [PATCH] feat: invokeAsyncFn fill out slice if the function panics. --- async.go | 12 +++++++----- async_test.go | 6 ++++++ go.mod | 4 +--- go.sum | 6 ++---- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/async.go b/async.go index b5d8c8e..d9ece38 100644 --- a/async.go +++ b/async.go @@ -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: @@ -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 } @@ -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 diff --git a/async_test.go b/async_test.go index 8fb7af7..f92103b 100644 --- a/async_test.go +++ b/async_test.go @@ -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) { diff --git a/go.mod b/go.mod index 1a751ef..d13c112 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index ccc3f2f..161a038 100644 --- a/go.sum +++ b/go.sum @@ -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=