Go SDK: Safer passing of arguments to workflows and activities

Hello.

I’ve started to work with Temporal recently and maybe there are better ways to achieve my goals, but what makes my life really complex is the lack of type checking when I’m calling workflows and activities.

Especially, when they have a lot of parameters, it is very easy to make a mistake and pass them in the wrong order or just forget some parameters.

So what I’m doing now, for every workflow/activity I have, I create additional function prefixed with Args. for example:

func MyActivityArgs(param1 string, param2 bool) []interface{} {
	return []interface{}{param1, param2}
}

func MyActivity(ctx context.Context, param1 string, param2 bool) error {
	// do something
	return nil
}

This way it is much easier to use this activity - I’m getting type checks for free:

err := workflow.
	ExecuteLocalActivity(ctx, activities.MyActivity,activities.MyActivityArgs("hello", true)...).
	Get(ctx, nil)

Everything becomes much easier with such an approach, but one problem remains - ... part to deconstruct the slice. It is easy to forget about it that will lead to bugs.

So what I was thinking about, maybe it is possible to add special type to Go SDK, e.g. sdk.Args that is just an alias of []interface{}. However, if a value of this type is passed as the first argument to workflow or activity, it should be recognized as a full list of parameters that need to be passed to workflow/activity.

This way it is possible to omit ... part without any bugs.

Activity invocations are essentially remote procedure calls. To be able to add new arguments in a backwards compatible manner we recommend for the majority of applications to use structures as input and output types for their invocation. So I would write your code as:

type MyActivityInput struct {
   Param1 string
   Param2 string
}

workflow.ExecuteActivity(ctx, &MyActivityInput{...})
2 Likes

One possible extension would be to provide code generator to generate activity invocation stubs from an interface. The Java SDK uses such strongly typed stubs, but it can do it without code generation as Java Reflection is more feature complete than the Go one.