From 0eb5aacaf8025453da6a12550d6f804aab7a4bcd Mon Sep 17 00:00:00 2001 From: Tiago Rodrigues Date: Mon, 25 Nov 2024 11:50:41 +0000 Subject: [PATCH] add utils method to wrap the activity func --- utils.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 utils.go diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..b483a7e --- /dev/null +++ b/utils.go @@ -0,0 +1,23 @@ +package tempts + +import "context" + +func WrapReturn[T any](fn func(ctx context.Context, t T) error) func(context.Context, T) (struct{}, error) { + return func(ctx context.Context, t T) (struct{}, error) { + err := fn(ctx, t) + return struct{}{}, err + } +} + +func WrapParam[T any](fn func(ctx context.Context) (T, error)) func(context.Context, struct{}) (T, error) { + return func(ctx context.Context, _ struct{}) (T, error) { + res, err := fn(ctx) + return res, err + } +} + +func Wrap(fn func(context.Context) error) func(context.Context, struct{}) (struct{}, error) { + return func(ctx context.Context, _ struct{}) (struct{}, error) { + return struct{}{}, fn(ctx) + } +}