diff --git a/protoc-gen-go/grpc/grpc.go b/protoc-gen-go/grpc/grpc.go index a11e07bcd0..10faf5b1f8 100644 --- a/protoc-gen-go/grpc/grpc.go +++ b/protoc-gen-go/grpc/grpc.go @@ -48,7 +48,7 @@ import ( // 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 @@ func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.Servi // 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 @@ func (g *grpc) generateServerSignature(servName string, method *pb.MethodDescrip 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