Skip to content

Commit

Permalink
Merge pull request #13 from GettEngineering/issue-12
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
Ilya Buzlov authored Sep 24, 2020
2 parents e47545c + cd0f5aa commit e689898
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cmd/effe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

const (
version = "0.1.4"
version = "0.1.5"
)

func main() {
Expand Down
28 changes: 13 additions & 15 deletions generator/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func (a *analyzer) sortFlowDeclsByDependecies() ([]flowDecl, []error) {
errs := []error{}
sequence := make([]string, len(a.flowDecls))
for _, flowDecl := range a.flowDecls {
a.inProcess[flowDecl.flowFunc.Name.Name] = struct{}{}
a.inProcess[flowDecl.FlowName()] = struct{}{}
callPath, err := a.findFlowCallPath(flowDecl.buildFlowFuncCall)
if err != nil {
errs = append(errs, err)
}
callPath = append(callPath, flowDecl.flowFunc.Name.Name)
delete(a.inProcess, flowDecl.flowFunc.Name.Name)
callPath = append(callPath, flowDecl.FlowName())
delete(a.inProcess, flowDecl.FlowName())
for _, call := range callPath {
sequence = addUniqueString(sequence, call)
}
Expand All @@ -42,7 +42,7 @@ func (a *analyzer) sortFlowDeclsByDependecies() ([]flowDecl, []error) {
sortedFlowDecls := []flowDecl{}
for _, p := range sequence {
for _, flowDecl := range a.flowDecls {
if p == flowDecl.flowFunc.Name.Name {
if p == flowDecl.FlowName() {
sortedFlowDecls = append(sortedFlowDecls, flowDecl)
break
}
Expand All @@ -64,30 +64,28 @@ func (a *analyzer) findFlowCallPath(funcCall *ast.CallExpr) ([]string, error) {
case *ast.Ident:
flowDeclIndex := -1
for i, flowDecl := range a.flowDecls {
if flowDecl.flowFunc.Name.Name == arg.Name {
if flowDecl.FlowName() == arg.Name {
flowDeclIndex = i
break
}
}
if flowDeclIndex == -1 {
continue
}
callPath = append(callPath, arg.Name)
_, ok := a.visited[a.flowDecls[flowDeclIndex].flowFunc.Name.Name]
if ok {
continue
}
if _, ok := a.inProcess[a.flowDecls[flowDeclIndex].flowFunc.Name.Name]; ok {
return []string{}, errors.Errorf("circular dependency found for %s", a.flowDecls[flowDeclIndex].flowFunc.Name.Name)
flowName := a.flowDecls[flowDeclIndex].FlowName()
callPath = append(callPath, flowName)
if _, ok := a.inProcess[flowName]; ok {
return []string{}, errors.Errorf("circular dependency found for %s", flowName)
}

a.inProcess[a.flowDecls[flowDeclIndex].flowFunc.Name.Name] = struct{}{}
defer delete(a.inProcess, a.flowDecls[flowDeclIndex].flowFunc.Name.Name)
a.inProcess[flowName] = struct{}{}
defer delete(a.inProcess, flowName)
dependeciesFordependecies, err := a.findFlowCallPath(a.flowDecls[flowDeclIndex].buildFlowFuncCall)
if err != nil {
return []string{}, err
}
a.visited[a.flowDecls[flowDeclIndex].flowFunc.Name.Name] = struct{}{}
a.visited[flowName] = struct{}{}

callPath = append(dependeciesFordependecies, callPath...)
}
}
Expand Down
10 changes: 7 additions & 3 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ type flowDecl struct {
buildFlowFuncCall *ast.CallExpr
}

func (f flowDecl) FlowName() string {
return f.flowFunc.Name.Name
}

type pkgGen struct {
flowFuncDecls []*ast.FuncDecl
depInitializerFuncDecls []*ast.FuncDecl
Expand Down Expand Up @@ -269,13 +273,13 @@ func (g *Generator) generateForPackage(pkg *packages.Package) (*pkgGen, []error)

importSet := make(map[string]struct{})
p := &pkgGen{}
for _, funcDecl := range sortedFlowDecls {
for _, flowDecl := range sortedFlowDecls {
f := &flowGen{
pkgFuncDecls: pkgFuncDecls,
implFields: make(map[string]implFieldInfo),
}

res, err := g.genFlow(funcDecl.flowFunc, funcDecl.buildFlowFuncCall, f)
res, err := g.genFlow(flowDecl.flowFunc, flowDecl.buildFlowFuncCall, f)
if err != nil {
loadErr, ok := err.(*types.LoadError)
if ok {
Expand Down Expand Up @@ -305,7 +309,7 @@ func (g *Generator) generateForPackage(pkg *packages.Package) (*pkgGen, []error)
p.flowFuncDecls = append(p.flowFuncDecls, res.flowFuncDecl)
p.implFuncDecls = append(p.implFuncDecls, res.implFuncDecls...)
p.typeSpecs = append(p.typeSpecs, res.typeSpecs...)
pkgFuncDecls[funcDecl.flowFunc.Name.Name] = res.flowFuncDecl
pkgFuncDecls[flowDecl.FlowName()] = res.flowFuncDecl
continue
}

Expand Down
18 changes: 5 additions & 13 deletions strategies/blockcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@ func (b *BlockContext) CalculateInput(calls []ComponentCall) {
for _, inputField := range c.Input().List {
var foundSourceOfArg bool
for _, previous := range calls[:index] {
if previous.Output() == nil {
continue
}
foundSourceOfArg = fields.FindFieldWithType(previous.Output().List, inputField.Type) != nil
if foundSourceOfArg {
break
}
foundSourceOfArg = previous.Output() != nil && fields.FindFieldWithType(previous.Output().List, inputField.Type) != nil
}

if !foundSourceOfArg {
Expand All @@ -63,18 +57,16 @@ func (b *BlockContext) CalculateOutput(calls []ComponentCall) {
continue
}
for _, outputField := range c.Output().List {
var using bool
var foundUsageOfOutput bool

for _, next := range calls[index+1:] {
if next.Input() == nil {
continue
}
using = fields.FindFieldWithType(next.Input().List, outputField.Type) != nil
if using {
break
}
foundUsageOfOutput = next.Input() != nil && fields.FindFieldWithType(next.Input().List, outputField.Type) != nil
}

if !using && fields.FindFieldWithType(b.Output.List, outputField.Type) == nil {
if !foundUsageOfOutput && fields.FindFieldWithType(b.Output.List, outputField.Type) == nil {
b.addOutput(outputField.Type)
}
}
Expand Down
35 changes: 35 additions & 0 deletions testdata/BuildWithRepeatRequirementComponent/foo/effe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// +build effeinject

package main

import (
"github.com/GettEngineering/effe"
)

func C() error {
effe.BuildFlow(
effe.Step(step2),
effe.Wrap(effe.Before(step1),
effe.Step(B),
),
effe.Step(step1),
effe.Wrap(effe.Before(step1),
effe.Step(B),
),
)
return nil
}

func B() error {
effe.BuildFlow(
effe.Step(A),
)
return nil
}

func A() error {
effe.BuildFlow(
effe.Step(step1),
)
return nil
}
15 changes: 15 additions & 0 deletions testdata/BuildWithRepeatRequirementComponent/foo/steps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

type stepFunc func() error

func step1() stepFunc {
return func() error {
return nil
}
}

func step2() stepFunc {
return func() error {
return nil
}
}
1 change: 1 addition & 0 deletions testdata/BuildWithRepeatRequirementComponent/pkg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example.com/foo
110 changes: 110 additions & 0 deletions testdata/BuildWithRepeatRequirementComponent/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 e689898

Please sign in to comment.