Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added UnimplementedServer for server interface #785

Merged
merged 14 commits into from
Mar 15, 2019
54 changes: 54 additions & 0 deletions protoc-gen-go/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const generatedCodeVersion = 4
// Paths for packages used by code generated in this file,
// relative to the import_prefix of the generator.Generator.
const (
errorPkgPath = "errors"
contextPkgPath = "context"
grpcPkgPath = "google.golang.org/grpc"
)
Expand All @@ -77,6 +78,7 @@ func (g *grpc) Name() string {
var (
contextPkg string
grpcPkg string
errorPkg string
)

// Init initializes the plugin.
Expand Down Expand Up @@ -105,12 +107,14 @@ func (g *grpc) Generate(file *generator.FileDescriptor) {
return
}

errorPkg = string(g.gen.AddImport(errorPkgPath))
contextPkg = string(g.gen.AddImport(contextPkgPath))
grpcPkg = string(g.gen.AddImport(grpcPkgPath))

g.P("// Reference imports to suppress errors if they are not otherwise used.")
g.P("var _ ", contextPkg, ".Context")
g.P("var _ ", grpcPkg, ".ClientConn")
g.P("var v = ", errorPkg, ".New(\"Unimplemented Function\")")
prannayk marked this conversation as resolved.
Show resolved Hide resolved
g.P()

// Assert version compatibility.
Expand Down Expand Up @@ -169,6 +173,20 @@ func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.Servi
g.P("}")
g.P()

// Client Unimplemented struct for forward compatabilit.
prannayk marked this conversation as resolved.
Show resolved Hide resolved
if deprecated {
g.P("//")
g.P(deprecationComment)
}
g.P("// ", servName, "ClientSafeImplementation should be extended to have forward comptaible implementations")
prannayk marked this conversation as resolved.
Show resolved Hide resolved
g.P("type ", servName, "ClientSafeImplementation struct {")
g.P("}")
g.P()
for i, method := range service.Method {
g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service.
g.P(g.generateClientMethodConcrete(servName, method))
}

// Client structure.
g.P("type ", unexport(servName), "Client struct {")
g.P("cc *", grpcPkg, ".ClientConn")
Expand Down Expand Up @@ -216,6 +234,22 @@ func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.Servi
g.P("}")
g.P()

// Server Unimplemented struct for forward compatabilit.
prannayk marked this conversation as resolved.
Show resolved Hide resolved
if deprecated {
prannayk marked this conversation as resolved.
Show resolved Hide resolved
g.P("//")
prannayk marked this conversation as resolved.
Show resolved Hide resolved
g.P(deprecationComment)
}
g.P("// ", servName, "ServerSafeImplementation should be extended to have forward comptaible implementations")
prannayk marked this conversation as resolved.
Show resolved Hide resolved
prannayk marked this conversation as resolved.
Show resolved Hide resolved
g.P("type ", serverType, "SafeImplementation struct {")
g.P("}")
g.P()
// SafeImplementation's concrete methods
for i, method := range service.Method {
g.gen.PrintComments(fmt.Sprintf("%s,2,%d", path, i)) // 2 means method in a service.
prannayk marked this conversation as resolved.
Show resolved Hide resolved
g.P(g.generateServerMethodConcrete(servName, method))
}
g.P()

// Server registration.
if deprecated {
g.P(deprecationComment)
Expand Down Expand Up @@ -269,6 +303,18 @@ func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.Servi
g.P()
}

// generateServerMethodConcrete returns unimplemented methods which ensure forward compatibility
func (g *grpc) generateServerMethodConcrete(servName string, method *pb.MethodDescriptorProto) string {
header := g.generateServerSignature(servName, method)
implementation := fmt.Sprintf("func (*%sServerSafeImplementation) %s {\n\tfmt.Println(\"Unimplemented function\")\n", servName, header)
implementation += fmt.Sprintf("\treturn ")
if !method.GetServerStreaming() && !method.GetClientStreaming() {
implementation += fmt.Sprintf("nil, ")
prannayk marked this conversation as resolved.
Show resolved Hide resolved
}
implementation += fmt.Sprintf("errors.New(\"Unimplemented Function\")\n}")
return implementation
}

// generateClientSignature returns the client-side signature for a method.
func (g *grpc) generateClientSignature(servName string, method *pb.MethodDescriptorProto) string {
origMethName := method.GetName()
Expand Down Expand Up @@ -368,6 +414,14 @@ func (g *grpc) generateClientMethod(servName, fullServName, serviceDescVar strin
}
}

// generateClientMethodConcrete returns unimplemented methods which ensure forward compatibility
func (g *grpc) generateClientMethodConcrete(servName string, method *pb.MethodDescriptorProto) string {
header := g.generateClientSignature(servName, method)
implementation := fmt.Sprintf("func (*%sClientSafeImplementation) %s {\n\tfmt.Println(\"Unimplemented function\")\n", servName, header)
implementation += fmt.Sprintf("\treturn nil, errors.New(\"Unimplemented Function\")\n}")
return implementation
}

// generateServerSignature returns the server-side signature for a method.
func (g *grpc) generateServerSignature(servName string, method *pb.MethodDescriptorProto) string {
origMethName := method.GetName()
Expand Down
26 changes: 26 additions & 0 deletions protoc-gen-go/testdata/deprecated/deprecated.pb.go

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

56 changes: 56 additions & 0 deletions protoc-gen-go/testdata/grpc/grpc.pb.go

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