Skip to content

Commit

Permalink
Enable BiDi graph building (#70)
Browse files Browse the repository at this point in the history
* Enable BiDi graph building
  • Loading branch information
Chris Roche authored Dec 11, 2019
1 parent c5b4c16 commit 4a50d28
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
6 changes: 6 additions & 0 deletions init_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ func MutateParams(pm ...ParamMutator) InitOption {
// disk. By default, the OS's file system is used. This option currently only
// impacts CustomFile and CustomTemplateFile artifacts generated by modules.
func FileSystem(fs afero.Fs) InitOption { return func(g *Generator) { g.persister.SetFS(fs) } }

// BiDirectional instructs the Generator to build the AST graph in both
// directions (ie, accessing dependents of an entity, not just dependencies).
func BiDirectional() InitOption {
return func(g *Generator) { g.workflow = &onceWorkflow{workflow: &standardWorkflow{BiDi: true}} }
}
19 changes: 19 additions & 0 deletions init_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestDebugMode(t *testing.T) {
Expand Down Expand Up @@ -70,3 +71,21 @@ func TestProtocOutput(t *testing.T) {
ProtocOutput(b)(g)
assert.Equal(t, b, g.out)
}

func TestBiDirectional(t *testing.T) {
t.Parallel()

g := &Generator{}
assert.Nil(t, g.workflow)

BiDirectional()(g)
wf := g.workflow

require.IsType(t, &onceWorkflow{}, wf)
once := wf.(*onceWorkflow)

require.IsType(t, &standardWorkflow{}, once.workflow)
std := once.workflow.(*standardWorkflow)

assert.True(t, std.BiDi)
}
11 changes: 9 additions & 2 deletions workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ type workflow interface {
}

// standardWorkflow describes a typical protoc-plugin flow, with the only
// exception being the behavior of the persistor directly writing custom file
// exception being the behavior of the persister directly writing custom file
// artifacts to disk (instead of via the plugin's output to protoc).
type standardWorkflow struct{ *Generator }
type standardWorkflow struct {
*Generator
BiDi bool
}

func (wf *standardWorkflow) Init(g *Generator) AST {
wf.Generator = g
Expand All @@ -38,6 +41,10 @@ func (wf *standardWorkflow) Init(g *Generator) AST {
pm(wf.params)
}

if wf.BiDi {
return ProcessCodeGeneratorRequestBidirectional(g, req)
}

return ProcessCodeGeneratorRequest(g, req)
}

Expand Down
8 changes: 8 additions & 0 deletions workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ func TestStandardWorkflow_Init(t *testing.T) {
g.workflow.Init(g)

assert.True(t, mutated)

t.Run("bidi", func(t *testing.T) {
mutated = false
g = Init(ProtocInput(bytes.NewReader(b)), BiDirectional(), MutateParams(func(p Parameters) { mutated = true }))
g.workflow.Init(g)

assert.True(t, mutated)
})
}

func TestStandardWorkflow_Run(t *testing.T) {
Expand Down

0 comments on commit 4a50d28

Please sign in to comment.