Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .custom-gcl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# The golangci-lint version used to build the custom binary.
version: v2.0.0
name: goworkflows
destination: .
plugins:
- module: 'github.com/cschleiden/go-workflows'
path: ./analyzer
60 changes: 50 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,53 @@
---
version: "2"
run:
tests: false

allow-parallel-runners: true
linters:
enable:
- goworkflows

linters-settings:
custom:
goworkflows:
path: ./plugin.so
description: go-workflows
original-url: github.com/cschleiden/go-workflows/analyzer
Comment on lines -6 to -13
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why you would use your own linter in your own project 😅

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repo does contain workflows, like all the different samples and I want to run the linter on those.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, ok. I can bring it back. Or maybe we can write a unit test that runs the analyzer programmatically?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any blockers to running this like it used to with the modules approach? I liked that because it made it easy to dogfood when working on samples and to see how it felt

- "bidichk"
- "bodyclose"
- "errcheck"
- "errname"
- "errorlint"
# - "gocritic"
- "goprintffuncname"
# - "gosec"
- "govet"
- "importas"
- "ineffassign"
- "makezero"
- "prealloc"
- "predeclared"
- "promlinter"
# - "revive"
- "rowserrcheck"
- "spancheck"
- "staticcheck"
- "tagalign"
- "testifylint"
- "tparallel"
- "unconvert"
- "usetesting"
- "wastedassign"
- "whitespace"
- "unused"
settings:
staticcheck:
checks:
- "all"
formatters:
enable:
- "gci"
- "gofmt"
- "gofumpt"
- "goimports"
settings:
gci:
sections:
- "standard"
- "default"
- "prefix(github.com/cschleiden)"
- "localmodule"
goimports:
local-prefixes:
- "github.com/cschleiden/go-workflows"
3 changes: 0 additions & 3 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,3 @@

1. `docker-compose up`

### Use custom linter

1. Build analyzer `go build -tags analyzerplugin -buildmode=plugin analyzer/plugin/plugin.go`
3 changes: 2 additions & 1 deletion activitytester/activitytester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"context"
"testing"

"github.com/cschleiden/go-workflows/activity"
"github.com/stretchr/testify/require"

"github.com/cschleiden/go-workflows/activity"
)

func Activity(ctx context.Context, a int, b int) (int, error) {
Expand Down
16 changes: 15 additions & 1 deletion analyzer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,18 @@

This package implements a basic analyzer for checking various common workflow error conditions.

It can be used with golangci-lint as a custom linter to provide feedback in editors or in CI runs.
In your own .golangci.yaml configuration file, you can enable it like this:

```yaml
version: "2"

linters:
enable:
- goworkflows

settings:
custom:
goworkflows:
type: module
original-url: github.com/cschleiden/go-workflows/analyzer
```
50 changes: 39 additions & 11 deletions analyzer/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,55 @@ import (
"go/ast"
"go/types"

"github.com/golangci/plugin-module-register/register"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

var checkPrivateReturnValues bool
func init() {
register.Plugin("goworkflows", New)
}

func New() *analysis.Analyzer {
a := &analysis.Analyzer{
Name: "goworkflows",
Doc: "Checks for common errors when writing workflows",
Run: run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
func New(settings any) (register.LinterPlugin, error) {
// The configuration type will be map[string]any or []interface, it depends on your configuration.
// You can use https://github.com/go-viper/mapstructure to convert map to struct.
s, err := register.DecodeSettings[Settings](settings)
if err != nil {
return nil, err
}

a.Flags.BoolVar(&checkPrivateReturnValues, "checkprivatereturnvalues", false, "Check return values of workflows which aren't exported")
return &GoWorkflowsPlugin{Settings: s}, nil
}

type GoWorkflowsPlugin struct {
Settings Settings
}

type Settings struct {
CheckPrivateReturnValues bool `json:"checkprivatereturnvalues"`
}

func (w *GoWorkflowsPlugin) BuildAnalyzers() ([]*analysis.Analyzer, error) {
return []*analysis.Analyzer{
{
Name: "goworkflows",
Doc: "Checks for common errors when writing workflows",
Run: w.run,
Requires: []*analysis.Analyzer{inspect.Analyzer},
},
}, nil
}

func (w *GoWorkflowsPlugin) GetLoadMode() string {
// NOTE: the mode can be `register.LoadModeSyntax` or `register.LoadModeTypesInfo`.
// - `register.LoadModeSyntax`: if the linter doesn't use types information.
// - `register.LoadModeTypesInfo`: if the linter uses types information.

return a
return register.LoadModeSyntax
}

func run(pass *analysis.Pass) (interface{}, error) {
func (w *GoWorkflowsPlugin) run(pass *analysis.Pass) (interface{}, error) {
inspector := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

// Expect workflows to be top level functions in a file. Therefore it should be enough to just keep track if the current
Expand Down Expand Up @@ -84,7 +112,7 @@ func run(pass *analysis.Pass) (interface{}, error) {
inWorkflow = true

// Check return types
if n.Name.IsExported() || checkPrivateReturnValues {
if n.Name.IsExported() || w.Settings.CheckPrivateReturnValues {
if n.Type.Results == nil || len(n.Type.Results.List) == 0 {
pass.Reportf(n.Pos(), "workflow `%v` doesn't return anything. needs to return at least `error`", n.Name.Name)
} else {
Expand Down
31 changes: 25 additions & 6 deletions analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,39 @@ package analyzer
import (
"testing"

"github.com/golangci/plugin-module-register/register"
"github.com/stretchr/testify/require"
"golang.org/x/tools/go/analysis/analysistest"
)

func TestAll(t *testing.T) {
a := New()
a.Flags.Set("checkprivatereturnvalues", "true")
analysistest.Run(t, analysistest.TestData(), a, "p", "q")
newPlugin, err := register.GetPlugin("goworkflows")
require.NoError(t, err)

plugin, err := newPlugin(map[string]any{
"checkprivatereturnvalues": true,
})
require.NoError(t, err)

analyzers, err := plugin.BuildAnalyzers()
require.NoError(t, err)

analysistest.Run(t, analysistest.TestData(), analyzers[0], "p", "q")
}

func TestComplex(t *testing.T) {
a := New()
a.Flags.Set("checkprivatereturnvalues", "true")
result := analysistest.Run(t, analysistest.TestData(), a, "q")
newPlugin, err := register.GetPlugin("goworkflows")
require.NoError(t, err)

plugin, err := newPlugin(map[string]any{
"checkprivatereturnvalues": true,
})
require.NoError(t, err)

analyzers, err := plugin.BuildAnalyzers()
require.NoError(t, err)

result := analysistest.Run(t, analysistest.TestData(), analyzers[0], "q")
for _, r := range result {
require.NoError(t, r.Err)
require.Equal(t, 1, len(r.Diagnostics))
Expand Down
17 changes: 17 additions & 0 deletions analyzer/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/cschleiden/go-workflows/analyzer

go 1.24.5

require (
github.com/golangci/plugin-module-register v0.1.1
github.com/stretchr/testify v1.10.0
golang.org/x/tools v0.31.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/mod v0.24.0 // indirect
golang.org/x/sync v0.12.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
20 changes: 20 additions & 0 deletions analyzer/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golangci/plugin-module-register v0.1.1 h1:TCmesur25LnyJkpsVrupv1Cdzo+2f7zX0H6Jkw1Ol6c=
github.com/golangci/plugin-module-register v0.1.1/go.mod h1:TTpqoB6KkwOJMV8u7+NyXMrkwwESJLOkfl9TxR1DGFc=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/mod v0.24.0 h1:ZfthKaKaT4NrhGVZHO1/WDTwGES4De8KtWO0SIbNJMU=
golang.org/x/mod v0.24.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
golang.org/x/sync v0.12.0 h1:MHc5BpPuC30uJk597Ri8TV3CNZcTLu6B6z4lJy+g6Jw=
golang.org/x/sync v0.12.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/tools v0.31.0 h1:0EedkvKDbh+qistFTd0Bcwe/YLh4vHwWEkiI0toFIBU=
golang.org/x/tools v0.31.0/go.mod h1:naFTU+Cev749tSJRXJlna0T3WxKvb1kWEx15xA4SdmQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
16 changes: 0 additions & 16 deletions analyzer/plugin/plugin.go

This file was deleted.

11 changes: 7 additions & 4 deletions backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import (
"errors"
"fmt"

"go.opentelemetry.io/otel/trace"

"github.com/cschleiden/go-workflows/backend/history"
"github.com/cschleiden/go-workflows/backend/metrics"
"github.com/cschleiden/go-workflows/core"
"github.com/cschleiden/go-workflows/workflow"
"go.opentelemetry.io/otel/trace"
)

var ErrInstanceNotFound = errors.New("workflow instance not found")
var ErrInstanceAlreadyExists = errors.New("workflow instance already exists")
var ErrInstanceNotFinished = errors.New("workflow instance is not finished")
var (
ErrInstanceNotFound = errors.New("workflow instance not found")
ErrInstanceAlreadyExists = errors.New("workflow instance already exists")
ErrInstanceNotFinished = errors.New("workflow instance is not finished")
)

type ErrNotSupported struct {
Message string
Expand Down
7 changes: 4 additions & 3 deletions backend/history/grouping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"testing"
"time"

"github.com/cschleiden/go-workflows/core"
"github.com/google/uuid"
"github.com/stretchr/testify/require"

"github.com/cschleiden/go-workflows/core"
)

func TestGrouping_MultipleEventsSameInstance(t *testing.T) {
Expand All @@ -26,6 +27,6 @@ func TestGrouping_MultipleEventsSameInstance(t *testing.T) {

require.Len(t, r, 1)
require.Len(t, r[*instance], 2)
require.Equal(t, r[*instance][0].HistoryEvent.Type, EventType_SubWorkflowScheduled)
require.Equal(t, r[*instance][1].HistoryEvent.Type, EventType_SignalReceived)
require.Equal(t, EventType_SubWorkflowScheduled, r[*instance][0].HistoryEvent.Type)
require.Equal(t, EventType_SignalReceived, r[*instance][1].HistoryEvent.Type)
}
3 changes: 1 addition & 2 deletions backend/history/workflow_canceled.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package history

type ExecutionCanceledAttributes struct {
}
type ExecutionCanceledAttributes struct{}
3 changes: 1 addition & 2 deletions backend/history/workflow_task_started.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
package history

type WorkflowTaskStartedAttributes struct {
}
type WorkflowTaskStartedAttributes struct{}
2 changes: 1 addition & 1 deletion backend/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "time"
type Tags map[string]string

type Client interface {
//Counter records a value at a point in time.
// Counter records a value at a point in time.
Counter(name string, tags Tags, value int64)

// Distribution records a value at a point in time.
Expand Down
3 changes: 2 additions & 1 deletion backend/monoprocess/monoprocess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"errors"
"testing"

"github.com/stretchr/testify/require"

"github.com/cschleiden/go-workflows/backend"
"github.com/cschleiden/go-workflows/backend/history"
"github.com/cschleiden/go-workflows/backend/sqlite"
"github.com/cschleiden/go-workflows/backend/test"
"github.com/stretchr/testify/require"
)

func Test_MonoprocessBackend(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion backend/mysql/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mysql
import (
"context"
"database/sql"
"errors"
"time"

"github.com/cschleiden/go-workflows/core"
Expand Down Expand Up @@ -108,7 +109,7 @@ func (mb *mysqlBackend) GetWorkflowInstance(ctx context.Context, instance *core.

err = res.Scan(&id, &executionID, &parentID, &parentExecutionID, &parentScheduleEventID, &createdAt, &completedAt, &queue)
if err != nil {
if err == sql.ErrNoRows {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}

Expand Down
Loading
Loading