Skip to content

Commit

Permalink
Update the grpc plugin to support the grpc-go interceptor implementat…
Browse files Browse the repository at this point in the history
…ion.

See grpc/grpc-go#642 for the corresponding grpc-go change.

Signed-off-by: David Symonds <dsymonds@golang.org>
  • Loading branch information
iamqizhao authored and dsymonds committed Apr 18, 2016
1 parent f0a097d commit 331aba2
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions protoc-gen-go/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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{")

This comment has been minimized.

Copy link
@awalterschulze

awalterschulze Apr 25, 2016

should be
g.P("info := &", grpcPkg, ".UnaryServerInfo{")

This comment has been minimized.

Copy link
@dsymonds

dsymonds Apr 25, 2016

Contributor

Thanks. I'll fix that.

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
Expand Down

0 comments on commit 331aba2

Please sign in to comment.