temporalgen

command module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 19, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

Stub Generation for Temporal Activities

Beware, that this code is currently PoC quality, so expect some issues when using it - please report them!

But this shouldn't stop you from using it, as the generated code is perfectly fine and easy to inspect.

When calling Temporal activities in Go you lose type safety, and it's a bit of boilerplate code.

But imagine instead of this

func BoilerplateWorkflow(ctx workflow.Context) (r *WfResult, err error) {
	var a Activities
	ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
		ScheduleToCloseTimeout: 10 * time.Second,
	})
	f := workflow.ExecuteActivity(ctx, a.DoSomething,  workflow.GetInfo(ctx).WorkflowExecution.ID, "maybe a string?")
	var result string
	err = f.Get(ctx, &result)
	if err != nil {
		return nil, err
	}
	return &WfResult{
		Result: result,
	}, nil
}

you could write this

func NiceWorkflow(ctx workflow.Context) (r *WfResult, err error) {
	var a ActivitiesStub
	ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
		ScheduleToCloseTimeout: 10 * time.Second,
	})
	// a.DoSomething(ctx, workflow.GetInfo(ctx).WorkflowExecution.ID, "maybe a string?") 🪲 wrong argument!
	result, err := a.DoSomethingExec(ctx, workflow.GetInfo(ctx).WorkflowExecution.ID, &echo.Group{})
	if err != nil {
		return nil, err
	}
	return &WfResult{
		Result: result,
	}, nil
}

This tool accomplishes this by generating some simple stubs for you. You can have a look at the test stub to see what is generated.

Usage

Install with go install github.com/Hades32/temporalgen@latest

Now, just add a Go generate comment with the type you want to create stubs for

//go:generate temporalgen -type Activities
type Activities struct {
	...
}

Now you can run go generate in your package (or go generate ./... for your whole module) and a activities.gen.go file will be generated.

I suggest running generate locally and checking the result into source control like any other source-code.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL