Skip to content

Commit

Permalink
cmd/protoc-gen-go-grpc: export consts for full method names (#5886)
Browse files Browse the repository at this point in the history
  • Loading branch information
KenxinKun authored Feb 1, 2023
1 parent d6dabba commit 3151e83
Show file tree
Hide file tree
Showing 19 changed files with 249 additions and 104 deletions.
6 changes: 5 additions & 1 deletion balancer/grpclb/grpc_lb_v1/load_balancer_grpc.pb.go

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

38 changes: 24 additions & 14 deletions channelz/grpc_channelz_v1/channelz_grpc.pb.go

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

33 changes: 24 additions & 9 deletions cmd/protoc-gen-go-grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const (
)

type serviceGenerateHelperInterface interface {
formatFullMethodName(service *protogen.Service, method *protogen.Method) string
formatFullMethodSymbol(service *protogen.Service, method *protogen.Method) string
genFullMethods(g *protogen.GeneratedFile, service *protogen.Service)
generateClientStruct(g *protogen.GeneratedFile, clientName string)
generateNewClientDefinitions(g *protogen.GeneratedFile, service *protogen.Service, clientName string)
generateUnimplementedServerType(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, service *protogen.Service)
Expand All @@ -46,8 +47,19 @@ type serviceGenerateHelperInterface interface {

type serviceGenerateHelper struct{}

func (serviceGenerateHelper) formatFullMethodName(service *protogen.Service, method *protogen.Method) string {
return fmt.Sprintf("/%s/%s", service.Desc.FullName(), method.Desc.Name())
func (serviceGenerateHelper) formatFullMethodSymbol(service *protogen.Service, method *protogen.Method) string {
return fmt.Sprintf("%s_%s_FullMethodName", service.GoName, method.GoName)
}

func (serviceGenerateHelper) genFullMethods(g *protogen.GeneratedFile, service *protogen.Service) {
g.P("const (")
for _, method := range service.Methods {
fmSymbol := helper.formatFullMethodSymbol(service, method)
fmName := fmt.Sprintf("/%s/%s", service.Desc.FullName(), method.Desc.Name())
g.P(fmSymbol, ` = "`, fmName, `"`)
}
g.P(")")
g.P()
}

func (serviceGenerateHelper) generateClientStruct(g *protogen.GeneratedFile, clientName string) {
Expand Down Expand Up @@ -167,13 +179,16 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.
}

func genService(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, service *protogen.Service) {
// Full methods constants.
helper.genFullMethods(g, service)

// Client interface.
clientName := service.GoName + "Client"

g.P("// ", clientName, " is the client API for ", service.GoName, " service.")
g.P("//")
g.P("// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.")

// Client interface.
if service.Desc.Options().(*descriptorpb.ServiceOptions).GetDeprecated() {
g.P("//")
g.P(deprecationComment)
Expand Down Expand Up @@ -288,15 +303,15 @@ func clientSignature(g *protogen.GeneratedFile, method *protogen.Method) string

func genClientMethod(gen *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFile, method *protogen.Method, index int) {
service := method.Parent
sname := helper.formatFullMethodName(service, method)
fmSymbol := helper.formatFullMethodSymbol(service, method)

if method.Desc.Options().(*descriptorpb.MethodOptions).GetDeprecated() {
g.P(deprecationComment)
}
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, ")")
g.P(`err := c.cc.Invoke(ctx, "`, sname, `", in, out, opts...)`)
g.P(`err := c.cc.Invoke(ctx, `, fmSymbol, `, in, out, opts...)`)
g.P("if err != nil { return nil, err }")
g.P("return out, nil")
g.P("}")
Expand All @@ -305,7 +320,7 @@ func genClientMethod(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene
}
streamType := unexport(service.GoName) + method.GoName + "Client"
serviceDescVar := service.GoName + "_ServiceDesc"
g.P("stream, err := c.cc.NewStream(ctx, &", serviceDescVar, ".Streams[", index, `], "`, sname, `", opts...)`)
g.P("stream, err := c.cc.NewStream(ctx, &", serviceDescVar, ".Streams[", index, `], `, fmSymbol, `, opts...)`)
g.P("if err != nil { return nil, err }")
g.P("x := &", streamType, "{stream}")
if !method.Desc.IsStreamingClient() {
Expand Down Expand Up @@ -433,8 +448,8 @@ func genServerMethod(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene
g.P("if interceptor == nil { return srv.(", service.GoName, "Server).", method.GoName, "(ctx, in) }")
g.P("info := &", grpcPackage.Ident("UnaryServerInfo"), "{")
g.P("Server: srv,")
fullMethodName := helper.formatFullMethodName(service, method)
g.P("FullMethod: \"", fullMethodName, "\",")
fmSymbol := helper.formatFullMethodSymbol(service, method)
g.P("FullMethod: ", fmSymbol, ",")
g.P("}")
g.P("handler := func(ctx ", contextPackage.Ident("Context"), ", req interface{}) (interface{}, error) {")
g.P("return srv.(", service.GoName, "Server).", method.GoName, "(ctx, req.(*", method.Input.GoIdent, "))")
Expand Down

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

17 changes: 12 additions & 5 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.

8 changes: 6 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.

Loading

0 comments on commit 3151e83

Please sign in to comment.