Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protoc: Change protoc to include static method call option #6960

Merged
merged 5 commits into from
Mar 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions cmd/protoc-gen-go-grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.

g.P("// This is a compile-time assertion to ensure that this generated file")
g.P("// is compatible with the grpc package it is being compiled against.")
g.P("// Requires gRPC-Go v1.32.0 or later.")
g.P("// Requires gRPC-Go v1.32.0 or later.") // TODO: Update to latest released gRPC version
g.P("const _ = ", grpcPackage.Ident("SupportPackageIsVersion7")) // When changing, update version number above.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be updated to Version8 now. The comment just lets users discover more easily when Version8 became available.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

g.P()
for _, service := range file.Services {
Expand Down Expand Up @@ -309,9 +309,15 @@ func genClientMethod(gen *protogen.Plugin, file *protogen.File, g *protogen.Gene
g.P(deprecationComment)
}
g.P("func (c *", unexport(service.GoName), "Client) ", clientSignature(g, method), "{")
g.P("cOpts := make([]", grpcPackage.Ident("CallOption"), ", len(opts))")
g.P("for i, opt := range opts {")
g.P(" cOpts[i] = opt")
g.P("}")
g.P("cOpts = append(cOpts, ", grpcPackage.Ident("RegisteredMethod()"), "\"")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be simplified to:

opts := append([]CallOption{grpc.RegisteredMethod()}, opts...) // shadows `opts` so you don't need the subsequent diffs even

Or use copy instead of a loop (and also pre-allocate len(opts)+1).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to former. Chose that since less lines of code and get rid of subsequent diffs.


if !method.Desc.IsStreamingServer() && !method.Desc.IsStreamingClient() {
g.P("out := new(", method.Output.GoIdent, ")")
g.P(`err := c.cc.Invoke(ctx, `, fmSymbol, `, in, out, opts...)`)
g.P(`err := c.cc.Invoke(ctx, `, fmSymbol, `, in, out, cOpts...)`)
g.P("if err != nil { return nil, err }")
g.P("return out, nil")
g.P("}")
Expand All @@ -320,7 +326,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, `], `, fmSymbol, `, opts...)`)
g.P("stream, err := c.cc.NewStream(ctx, &", serviceDescVar, ".Streams[", index, `], `, fmSymbol, `, cOpts...)`)
g.P("if err != nil { return nil, err }")
g.P("x := &", streamType, "{stream}")
if !method.Desc.IsStreamingClient() {
Expand Down