Skip to content

Commit

Permalink
Merge pull request #19 from GettEngineering/issue-18
Browse files Browse the repository at this point in the history
Generate default error for a desicion block
  • Loading branch information
Ilya Buzlov authored Sep 30, 2020
2 parents 2712dbf + b791989 commit d8f24ab
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 8 deletions.
14 changes: 14 additions & 0 deletions strategies/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package strategies
import (
"fmt"
"go/ast"
goTypes "go/types"

"github.com/GettEngineering/effe/fields"
"github.com/GettEngineering/effe/types"
Expand Down Expand Up @@ -124,6 +125,19 @@ func GenDecisionComponentCall(f FlowGen, dComponent types.Component) (ComponentC
ctx.CalculateOutput([]ComponentCall{call})
}

var switchReturnErr bool

for _, output := range ctx.OutputList() {
if goTypes.ExprString(output.Type) == errorExpr {
switchReturnErr = true
break
}
}

if !switchReturnErr {
ctx.addOutput(ast.NewIdent("error"))
}

ctx.Output.List = sortComponentOutput(ctx.Output.List)

sharedVars := make(map[string]*ast.Ident)
Expand Down
16 changes: 16 additions & 0 deletions testdata/DecisionGenerateDefaultError/foo/effe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// +build effeinject

package main

import "github.com/GettEngineering/effe"

func A() error {
effe.BuildFlow(
effe.Step(step1),
effe.Decision(new(a), effe.Failure(failure),
effe.Case("a", effe.Step(step2)),
effe.Case("", effe.Step(step3)),
),
)
return nil
}
30 changes: 30 additions & 0 deletions testdata/DecisionGenerateDefaultError/foo/steps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import "fmt"

type a string

func failure() func(error) error {
return func(err error) error {
return err
}
}

func step2() func(a) int {
return func(v a) int {
fmt.Println(v)
return 0
}
}

func step3() func() int {
return func() int {
return 0
}
}

func step1() func() (a, error) {
return func() (a, error) {
return "a", nil
}
}
1 change: 1 addition & 0 deletions testdata/DecisionGenerateDefaultError/pkg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example.com/foo
66 changes: 66 additions & 0 deletions testdata/DecisionGenerateDefaultError/want/effe_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 12 additions & 8 deletions testdata/DecisionReturnDifferentTypes/want/effe_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d8f24ab

Please sign in to comment.