Skip to content

Commit

Permalink
Merge pull request #4 from pepabo/with_close
Browse files Browse the repository at this point in the history
Add `with_close` option to Add `Close()` method
  • Loading branch information
k1LoW authored Mar 29, 2023
2 parents e61609d + eb98bce commit bbcae33
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ Generate mock client ([client_mock.go](example/gen/go/myapp/client_mock.go)) tha
| --- | --- | --- |
| `package` | string | Specify package name (ex `--go-client_opt=package=xxxx` ) |
| `same_package` | bool | Make the package the same as the package generated by proto-gen-go (ex `--go-client_opt=same_package` ) |
| `with_close` | bool | Add `Close()` method for closing *grpc.ClientConn from client |
4 changes: 2 additions & 2 deletions example/buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ plugins:
opt: paths=source_relative
- plugin: go-client
out: gen/go
opt: paths=source_relative,same_package
opt: paths=source_relative,same_package,with_close
- plugin: go-client-mock
out: gen/go
opt: paths=source_relative,same_package
opt: paths=source_relative,same_package,with_close
9 changes: 8 additions & 1 deletion example/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ func TestClientUser(t *testing.T) {
func TestClientProject(t *testing.T) {
ctx := context.Background()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
t.Cleanup(func() {
ctrl.Finish()
})
client := myapp.NewMockClient(ctrl)
t.Cleanup(func() {
client.Close()
})
req := &myapp.ListProjectsRequest{
Page: 1,
}
Expand All @@ -57,6 +62,8 @@ func TestClientProject(t *testing.T) {
lc.EXPECT().Recv().Return(res[1], nil)
lc.EXPECT().Recv().Return(nil, io.EOF)
client.EXPECT().ProjectService().ListProjects(ctx, req).Return(lc, nil)
client.EXPECT().Close().Return(nil)

rc, err := client.ProjectService().ListProjects(ctx, req)
if err != nil {
t.Fatal(err)
Expand Down
19 changes: 17 additions & 2 deletions example/gen/go/myapp/client.go

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

16 changes: 15 additions & 1 deletion example/gen/go/myapp/client_mock.go

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

24 changes: 23 additions & 1 deletion gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import (
const (
optPackage = "package"
optSamePackage = "same_package"
optWithClose = "with_close"
)

type Generator struct {
genp *protogen.Plugin
samePackage bool
withClose bool
packageName string
}

Expand Down Expand Up @@ -56,10 +58,13 @@ func (gen *Generator) Generate() error {
}
g.P("")
g.P(`import (`)
if gen.withClose {
g.P(`"reflect"`)
g.P("")
}
if !gen.samePackage {
g.P(fmt.Sprintf("%s %s", tmppf.GoPackageName, tmppf.GoImportPath.String()))
}
g.P("")
g.P(`"github.com/golang/mock/gomock"`)
g.P(`)`)
g.P("")
Expand Down Expand Up @@ -155,6 +160,21 @@ func (gen *Generator) Generate() error {
}
}
}
if gen.withClose {
g.P("func (m *MockClient) Close() error {")
g.P("m.ctrl.T.Helper()")
g.P(`ret := m.ctrl.Call(m, "Close")`)
g.P(`ret0, _ := ret[0].(error)`)
g.P(`return ret0`)
g.P("}")
g.P("")

g.P("func (mr *MockClientMockRecorder) Close() *gomock.Call {")
g.P("mr.mock.ctrl.T.Helper()")
g.P(`return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockClient)(nil).Close))`)
g.P("}")
g.P("")
}

return nil
}
Expand All @@ -166,6 +186,8 @@ func (gen *Generator) parseOpts() error {
switch {
case o == optSamePackage:
gen.samePackage = true
case o == optWithClose:
gen.withClose = true
case strings.HasPrefix(o, fmt.Sprintf("%s=", optPackage)):
gen.packageName = strings.TrimPrefix(o, fmt.Sprintf("%s=", optPackage))
}
Expand Down

0 comments on commit bbcae33

Please sign in to comment.