Skip to content

Commit 4a50d28

Browse files
author
Chris Roche
authored
Enable BiDi graph building (#70)
* Enable BiDi graph building
1 parent c5b4c16 commit 4a50d28

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

Diff for: init_option.go

+6
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,9 @@ func MutateParams(pm ...ParamMutator) InitOption {
4141
// disk. By default, the OS's file system is used. This option currently only
4242
// impacts CustomFile and CustomTemplateFile artifacts generated by modules.
4343
func FileSystem(fs afero.Fs) InitOption { return func(g *Generator) { g.persister.SetFS(fs) } }
44+
45+
// BiDirectional instructs the Generator to build the AST graph in both
46+
// directions (ie, accessing dependents of an entity, not just dependencies).
47+
func BiDirectional() InitOption {
48+
return func(g *Generator) { g.workflow = &onceWorkflow{workflow: &standardWorkflow{BiDi: true}} }
49+
}

Diff for: init_option_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/spf13/afero"
1111
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
1213
)
1314

1415
func TestDebugMode(t *testing.T) {
@@ -70,3 +71,21 @@ func TestProtocOutput(t *testing.T) {
7071
ProtocOutput(b)(g)
7172
assert.Equal(t, b, g.out)
7273
}
74+
75+
func TestBiDirectional(t *testing.T) {
76+
t.Parallel()
77+
78+
g := &Generator{}
79+
assert.Nil(t, g.workflow)
80+
81+
BiDirectional()(g)
82+
wf := g.workflow
83+
84+
require.IsType(t, &onceWorkflow{}, wf)
85+
once := wf.(*onceWorkflow)
86+
87+
require.IsType(t, &standardWorkflow{}, once.workflow)
88+
std := once.workflow.(*standardWorkflow)
89+
90+
assert.True(t, std.BiDi)
91+
}

Diff for: workflow.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ type workflow interface {
1515
}
1616

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

2225
func (wf *standardWorkflow) Init(g *Generator) AST {
2326
wf.Generator = g
@@ -38,6 +41,10 @@ func (wf *standardWorkflow) Init(g *Generator) AST {
3841
pm(wf.params)
3942
}
4043

44+
if wf.BiDi {
45+
return ProcessCodeGeneratorRequestBidirectional(g, req)
46+
}
47+
4148
return ProcessCodeGeneratorRequest(g, req)
4249
}
4350

Diff for: workflow_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ func TestStandardWorkflow_Init(t *testing.T) {
2323
g.workflow.Init(g)
2424

2525
assert.True(t, mutated)
26+
27+
t.Run("bidi", func(t *testing.T) {
28+
mutated = false
29+
g = Init(ProtocInput(bytes.NewReader(b)), BiDirectional(), MutateParams(func(p Parameters) { mutated = true }))
30+
g.workflow.Init(g)
31+
32+
assert.True(t, mutated)
33+
})
2634
}
2735

2836
func TestStandardWorkflow_Run(t *testing.T) {

0 commit comments

Comments
 (0)