Update the grpc plugin to support the grpc-go interceptor implementation.
See https://github.com/grpc/grpc-go/pull/642 for the corresponding grpc-go change.
Signed-off-by: David Symonds <dsymonds@golang.org>
diff --git a/protoc-gen-go/grpc/grpc.go b/protoc-gen-go/grpc/grpc.go
index a11e07b..10faf5b 100644
--- a/protoc-gen-go/grpc/grpc.go
+++ b/protoc-gen-go/grpc/grpc.go
@@ -48,7 +48,7 @@
// It is incremented whenever an incompatibility between the generated code and
// the grpc package is introduced; the generated code references
// a constant, grpc.SupportPackageIsVersionN (where N is generatedCodeVersion).
-const generatedCodeVersion = 1
+const generatedCodeVersion = 2
// Paths for packages used by code generated in this file,
// relative to the import_prefix of the generator.Generator.
@@ -218,7 +218,7 @@
// Server handler implementations.
var handlerNames []string
for _, method := range service.Method {
- hname := g.generateServerMethod(servName, method)
+ hname := g.generateServerMethod(servName, fullServName, method)
handlerNames = append(handlerNames, hname)
}
@@ -378,19 +378,25 @@
return methName + "(" + strings.Join(reqArgs, ", ") + ") " + ret
}
-func (g *grpc) generateServerMethod(servName string, method *pb.MethodDescriptorProto) string {
+func (g *grpc) generateServerMethod(servName, fullServName string, method *pb.MethodDescriptorProto) string {
methName := generator.CamelCase(method.GetName())
hname := fmt.Sprintf("_%s_%s_Handler", servName, methName)
inType := g.typeName(method.GetInputType())
outType := g.typeName(method.GetOutputType())
if !method.GetServerStreaming() && !method.GetClientStreaming() {
- g.P("func ", hname, "(srv interface{}, ctx ", contextPkg, ".Context, dec func(interface{}) error) (interface{}, error) {")
+ g.P("func ", hname, "(srv interface{}, ctx ", contextPkg, ".Context, dec func(interface{}) error, interceptor ", grpcPkg, ".UnaryServerInterceptor) (interface{}, error) {")
g.P("in := new(", inType, ")")
g.P("if err := dec(in); err != nil { return nil, err }")
- g.P("out, err := srv.(", servName, "Server).", methName, "(ctx, in)")
- g.P("if err != nil { return nil, err }")
- g.P("return out, nil")
+ g.P("if interceptor == nil { return srv.(", servName, "Server).", methName, "(ctx, in) }")
+ g.P("info := &grpc.UnaryServerInfo{")
+ g.P("Server: srv,")
+ g.P("FullMethod: ", strconv.Quote(fmt.Sprintf("/%s/%s", fullServName, methName)), ",")
+ g.P("}")
+ g.P("handler := func(ctx ", contextPkg, ".Context, req interface{}) (interface{}, error) {")
+ g.P("return srv.(", servName, "Server).", methName, "(ctx, req.(*", inType, "))")
+ g.P("}")
+ g.P("return interceptor(ctx, in, info, handler)")
g.P("}")
g.P()
return hname