grpc: accept interface in NewClient functions

Change-Id: I62123fccd689bdda9612942cc79b0a91527158cd
Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/216399
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
This commit is contained in:
Doug Fawley 2020-01-24 15:42:06 -08:00 committed by Joe Tsai
parent adbbc8ec47
commit 5e73c4cec2
4 changed files with 14 additions and 14 deletions

View File

@ -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)
@ -85,7 +85,7 @@ 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()
@ -93,7 +93,7 @@ func genService(gen *protogen.Plugin, file *protogen.File, g *protogen.Generated
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()

View File

@ -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
)

View File

@ -11,11 +11,11 @@ import (
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
var _ grpc.ClientConnInterface
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion4
const _ = grpc.SupportPackageIsVersion6
// DeprecatedServiceClient is the client API for DeprecatedService service.
//
@ -28,11 +28,11 @@ type DeprecatedServiceClient interface {
}
type deprecatedServiceClient struct {
cc *grpc.ClientConn
cc grpc.ClientConnInterface
}
// Deprecated: Do not use.
func NewDeprecatedServiceClient(cc *grpc.ClientConn) DeprecatedServiceClient {
func NewDeprecatedServiceClient(cc grpc.ClientConnInterface) DeprecatedServiceClient {
return &deprecatedServiceClient{cc}
}

View File

@ -11,11 +11,11 @@ import (
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
var _ grpc.ClientConnInterface
// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion4
const _ = grpc.SupportPackageIsVersion6
// TestServiceClient is the client API for TestService service.
//
@ -31,10 +31,10 @@ type TestServiceClient interface {
}
type testServiceClient struct {
cc *grpc.ClientConn
cc grpc.ClientConnInterface
}
func NewTestServiceClient(cc *grpc.ClientConn) TestServiceClient {
func NewTestServiceClient(cc grpc.ClientConnInterface) TestServiceClient {
return &testServiceClient{cc}
}