Skip to content

Commit 331aba2

Browse files
iamqizhaodsymonds
authored andcommitted
Update the grpc plugin to support the grpc-go interceptor implementation.
See grpc/grpc-go#642 for the corresponding grpc-go change. Signed-off-by: David Symonds <[email protected]>
1 parent f0a097d commit 331aba2

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

protoc-gen-go/grpc/grpc.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import (
4848
// It is incremented whenever an incompatibility between the generated code and
4949
// the grpc package is introduced; the generated code references
5050
// a constant, grpc.SupportPackageIsVersionN (where N is generatedCodeVersion).
51-
const generatedCodeVersion = 1
51+
const generatedCodeVersion = 2
5252

5353
// Paths for packages used by code generated in this file,
5454
// relative to the import_prefix of the generator.Generator.
@@ -218,7 +218,7 @@ func (g *grpc) generateService(file *generator.FileDescriptor, service *pb.Servi
218218
// Server handler implementations.
219219
var handlerNames []string
220220
for _, method := range service.Method {
221-
hname := g.generateServerMethod(servName, method)
221+
hname := g.generateServerMethod(servName, fullServName, method)
222222
handlerNames = append(handlerNames, hname)
223223
}
224224

@@ -378,19 +378,25 @@ func (g *grpc) generateServerSignature(servName string, method *pb.MethodDescrip
378378
return methName + "(" + strings.Join(reqArgs, ", ") + ") " + ret
379379
}
380380

381-
func (g *grpc) generateServerMethod(servName string, method *pb.MethodDescriptorProto) string {
381+
func (g *grpc) generateServerMethod(servName, fullServName string, method *pb.MethodDescriptorProto) string {
382382
methName := generator.CamelCase(method.GetName())
383383
hname := fmt.Sprintf("_%s_%s_Handler", servName, methName)
384384
inType := g.typeName(method.GetInputType())
385385
outType := g.typeName(method.GetOutputType())
386386

387387
if !method.GetServerStreaming() && !method.GetClientStreaming() {
388-
g.P("func ", hname, "(srv interface{}, ctx ", contextPkg, ".Context, dec func(interface{}) error) (interface{}, error) {")
388+
g.P("func ", hname, "(srv interface{}, ctx ", contextPkg, ".Context, dec func(interface{}) error, interceptor ", grpcPkg, ".UnaryServerInterceptor) (interface{}, error) {")
389389
g.P("in := new(", inType, ")")
390390
g.P("if err := dec(in); err != nil { return nil, err }")
391-
g.P("out, err := srv.(", servName, "Server).", methName, "(ctx, in)")
392-
g.P("if err != nil { return nil, err }")
393-
g.P("return out, nil")
391+
g.P("if interceptor == nil { return srv.(", servName, "Server).", methName, "(ctx, in) }")
392+
g.P("info := &grpc.UnaryServerInfo{")
393+
g.P("Server: srv,")
394+
g.P("FullMethod: ", strconv.Quote(fmt.Sprintf("/%s/%s", fullServName, methName)), ",")
395+
g.P("}")
396+
g.P("handler := func(ctx ", contextPkg, ".Context, req interface{}) (interface{}, error) {")
397+
g.P("return srv.(", servName, "Server).", methName, "(ctx, req.(*", inType, "))")
398+
g.P("}")
399+
g.P("return interceptor(ctx, in, info, handler)")
394400
g.P("}")
395401
g.P()
396402
return hname

0 commit comments

Comments
 (0)