Skip to content

Commit

Permalink
grpc: accept interface in NewClient functions
Browse files Browse the repository at this point in the history
Change-Id: I62123fccd689bdda9612942cc79b0a91527158cd
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/216399
Reviewed-by: Joe Tsai <[email protected]>
  • Loading branch information
dfawley authored and dsnet committed Jan 28, 2020
1 parent adbbc8e commit 5e73c4c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions cmd/protoc-gen-go-grpc/internal_gengogrpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ func GenerateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.
// TODO: Remove this. We don't need to include these references any more.
g.P("// Reference imports to suppress errors if they are not otherwise used.")
g.P("var _ ", contextPackage.Ident("Context"))
g.P("var _ ", grpcPackage.Ident("ClientConn"))
g.P("var _ ", grpcPackage.Ident("ClientConnInterface"))
g.P()

g.P("// This is a compile-time assertion to ensure that this generated file")
g.P("// is compatible with the grpc package it is being compiled against.")
g.P("const _ = ", grpcPackage.Ident("SupportPackageIsVersion4"))
g.P("const _ = ", grpcPackage.Ident("SupportPackageIsVersion6"))
g.P()
for _, service := range file.Services {
genService(gen, file, g, service)
Expand Down Expand Up @@ -85,15 +85,15 @@ func genService(gen *protogen.Plugin, file *protogen.File, g *protogen.Generated

// Client structure.
g.P("type ", unexport(clientName), " struct {")
g.P("cc *", grpcPackage.Ident("ClientConn"))
g.P("cc ", grpcPackage.Ident("ClientConnInterface"))
g.P("}")
g.P()

// NewClient factory.
if service.Desc.Options().(*descriptorpb.ServiceOptions).GetDeprecated() {
g.P(deprecationComment)
}
g.P("func New", clientName, " (cc *", grpcPackage.Ident("ClientConn"), ") ", clientName, " {")
g.P("func New", clientName, " (cc ", grpcPackage.Ident("ClientConnInterface"), ") ", clientName, " {")
g.P("return &", unexport(clientName), "{cc}")
g.P("}")
g.P()
Expand Down
4 changes: 2 additions & 2 deletions cmd/protoc-gen-go-grpc/testdata/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module google.golang.org/protobuf/cmd/protoc-gen-go-grpc/testdata
go 1.9

require (
github.com/golang/protobuf v1.2.1-0.20191106221148-0fd87f0c3acd
google.golang.org/grpc v1.19.0
github.com/golang/protobuf v1.3.2
google.golang.org/grpc v1.27.0-pre.0.20200124224931-7afcfdd66b12
google.golang.org/protobuf v1.0.0
)

Expand Down
8 changes: 4 additions & 4 deletions cmd/protoc-gen-go-grpc/testdata/grpc/deprecation_grpc.pb.go

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

8 changes: 4 additions & 4 deletions cmd/protoc-gen-go-grpc/testdata/grpc/grpc_grpc.pb.go

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

0 comments on commit 5e73c4c

Please sign in to comment.