Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dfawley committed Aug 19, 2020
1 parent 0670a4a commit d53b63f
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 92 deletions.
29 changes: 15 additions & 14 deletions cmd/protoc-gen-go-grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
)

const (
errorsPackage = protogen.GoImportPath("errors")
contextPackage = protogen.GoImportPath("context")
grpcPackage = protogen.GoImportPath("google.golang.org/grpc")
codesPackage = protogen.GoImportPath("google.golang.org/grpc/codes")
Expand Down Expand Up @@ -136,6 +135,18 @@ func genClientMethod(gen *protogen.Plugin, g *protogen.GeneratedFile, method *pr
if method.Desc.Options().(*descriptorpb.MethodOptions).GetDeprecated() {
g.P(deprecationComment)
}

streamDescName := unexport(service.GoName) + method.GoName + "StreamDesc"
g.P("var ", streamDescName, " = &", grpcPackage.Ident("StreamDesc"), "{")
g.P("StreamName: ", strconv.Quote(string(method.Desc.Name())), ",")
if method.Desc.IsStreamingServer() {
g.P("ServerStreams: true,")
}
if method.Desc.IsStreamingClient() {
g.P("ClientStreams: true,")
}
g.P("}")

g.P("func (c *", unexport(service.GoName), "Client) ", clientSignature(g, method), "{")
if !method.Desc.IsStreamingServer() && !method.Desc.IsStreamingClient() {
g.P("out := new(", method.Output.GoIdent, ")")
Expand All @@ -148,17 +159,7 @@ func genClientMethod(gen *protogen.Plugin, g *protogen.GeneratedFile, method *pr
}
streamType := unexport(service.GoName) + method.GoName + "Client"

g.P("streamDesc := &", grpcPackage.Ident("StreamDesc"), "{")
g.P("StreamName: ", strconv.Quote(string(method.Desc.Name())), ",")
if method.Desc.IsStreamingServer() {
g.P("ServerStreams: true,")
}
if method.Desc.IsStreamingClient() {
g.P("ClientStreams: true,")
}
g.P("}")

g.P(`stream, err := c.cc.NewStream(ctx, streamDesc, "`, sname, `", opts...)`)
g.P(`stream, err := c.cc.NewStream(ctx, `, streamDescName, `, "`, sname, `", opts...)`)
g.P("if err != nil { return nil, err }")
g.P("x := &", streamType, "{stream}")
if !method.Desc.IsStreamingClient() {
Expand Down Expand Up @@ -384,12 +385,12 @@ func handlerSignature(g *protogen.GeneratedFile, method *protogen.Method) string
}

func unaryHandlerSignature(g *protogen.GeneratedFile) string {
return "(srv interface{}, ctx " + g.QualifiedGoIdent(contextPackage.Ident("Context")) +
return "(_ interface{}, ctx " + g.QualifiedGoIdent(contextPackage.Ident("Context")) +
", dec func(interface{}) error, interceptor " + g.QualifiedGoIdent(grpcPackage.Ident("UnaryServerInterceptor")) + ") (interface{}, error)"
}

func streamHandlerSignature(g *protogen.GeneratedFile) string {
return "(srv interface{}, stream " + g.QualifiedGoIdent(grpcPackage.Ident("ServerStream")) + ") error"
return "(_ interface{}, stream " + g.QualifiedGoIdent(grpcPackage.Ident("ServerStream")) + ") error"
}

func genMethodHandler(gen *protogen.Plugin, g *protogen.GeneratedFile, method *protogen.Method) {
Expand Down
50 changes: 29 additions & 21 deletions examples/features/proto/echo/echo_grpc.pb.go

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

2 changes: 1 addition & 1 deletion examples/features/reflection/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func sayHello(ctx context.Context, in *hwpb.HelloRequest) (*hwpb.HelloReply, err
return &hwpb.HelloReply{Message: "Hello " + in.Name}, nil
}

// unaryEcho implementes echo.Echo.UnaryEcho
// unaryEcho implements echo.Echo.UnaryEcho
func unaryEcho(ctx context.Context, req *ecpb.EchoRequest) (*ecpb.EchoResponse, error) {
return &ecpb.EchoResponse{Message: req.Message}, nil
}
Expand Down
9 changes: 7 additions & 2 deletions examples/helloworld/helloworld/helloworld_grpc.pb.go

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

50 changes: 29 additions & 21 deletions examples/route_guide/routeguide/route_guide_grpc.pb.go

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

5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ require (
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f
github.com/envoyproxy/go-control-plane v0.9.4
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/golang/protobuf v1.4.2
github.com/golang/protobuf v1.3.3
github.com/google/go-cmp v0.4.0
golang.org/x/net v0.0.0-20190311183353-d8887717615a
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a
google.golang.org/genproto v0.0.0-20200624020401-64a14ca9d1ad
google.golang.org/grpc/examples v0.0.0-20200811135751-6aaac03d175a // indirect
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55
)
Loading

0 comments on commit d53b63f

Please sign in to comment.