Skip to content

Commit

Permalink
perf: use errors.New when possible (#1490)
Browse files Browse the repository at this point in the history
  • Loading branch information
codyoss committed Apr 11, 2024
1 parent 7fdb7aa commit c1fd44c
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 16 deletions.
2 changes: 1 addition & 1 deletion internal/gengapic/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func (g *generator) nestedName(nested pbinfo.ProtoType) string {
// * The field is not annotated with google.api.field_behavior = REQUIRED.
// * The field name is listed in google.api.publishing.method_settings.auto_populated_fields.
// * The field is annotated with google.api.field_info.format = UUID4.
func (g *generator) autoPopulatedFields(servName string, m *descriptor.MethodDescriptorProto) []*descriptor.FieldDescriptorProto {
func (g *generator) autoPopulatedFields(_ string, m *descriptor.MethodDescriptorProto) []*descriptor.FieldDescriptorProto {
var apfs []string
// Find the service config's AutoPopulatedFields entry by method name.
mfqn := g.fqn(m)
Expand Down
15 changes: 6 additions & 9 deletions internal/gengapic/genrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ func (g *generator) serverStreamRESTCall(servName string, s *descriptor.ServiceD
p("")
p("func (c *%s) CloseSend() error {", streamClient)
p(" // This is a no-op to fulfill the interface.")
p(` return fmt.Errorf("this method is not implemented for a server-stream")`)
p(` return errors.New("this method is not implemented for a server-stream")`)
p("}")
p("")
p("func (c *%s) Context() context.Context {", streamClient)
Expand All @@ -715,17 +715,17 @@ func (g *generator) serverStreamRESTCall(servName string, s *descriptor.ServiceD
p("")
p("func (c *%s) SendMsg(m interface{}) error {", streamClient)
p(" // This is a no-op to fulfill the interface.")
p(` return fmt.Errorf("this method is not implemented for a server-stream")`)
p(` return errors.New("this method is not implemented for a server-stream")`)
p("}")
p("")
p("func (c *%s) RecvMsg(m interface{}) error {", streamClient)
p(" // This is a no-op to fulfill the interface.")
p(` return fmt.Errorf("this method is not implemented, use Recv")`)
p(` return errors.New("this method is not implemented, use Recv")`)
p("}")
p("")

g.imports[pbinfo.ImportSpec{Path: "context"}] = true
g.imports[pbinfo.ImportSpec{Path: "fmt"}] = true
g.imports[pbinfo.ImportSpec{Path: "errors"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/grpc/metadata"}] = true

return nil
Expand All @@ -748,12 +748,12 @@ func (g *generator) noRequestStreamRESTCall(servName string, s *descriptor.Servi

p("func (c *%s) %s(ctx context.Context, opts ...gax.CallOption) (%s.%s_%sClient, error) {",
lowcaseServName, m.GetName(), servSpec.Name, s.GetName(), m.GetName())
p(` return nil, fmt.Errorf("%s not yet supported for REST clients")`, m.GetName())
p(` return nil, errors.New("%s not yet supported for REST clients")`, m.GetName())
p("}")
p("")

g.imports[pbinfo.ImportSpec{Path: "context"}] = true
g.imports[pbinfo.ImportSpec{Path: "fmt"}] = true
g.imports[pbinfo.ImportSpec{Path: "errors"}] = true

return nil
}
Expand All @@ -775,9 +775,6 @@ func (g *generator) pagingRESTCall(servName string, m *descriptor.MethodDescript
return err
}
info := getHTTPInfo(m)
if err != nil {
return err
}
if info == nil {
return fmt.Errorf("method has no http info: %q", m.GetName())
}
Expand Down
3 changes: 2 additions & 1 deletion internal/gengapic/genrest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ func TestGenRestMethod(t *testing.T) {
imports: map[pbinfo.ImportSpec]bool{
{Path: "bytes"}: true,
{Path: "context"}: true,
{Path: "errors"}: true,
{Path: "fmt"}: true,
{Path: "google.golang.org/api/googleapi"}: true,
{Path: "net/url"}: true,
Expand All @@ -835,7 +836,7 @@ func TestGenRestMethod(t *testing.T) {
options: &options{},
imports: map[pbinfo.ImportSpec]bool{
{Path: "context"}: true,
{Path: "fmt"}: true,
{Path: "errors"}: true,
{Name: "foopb", Path: "google.golang.org/genproto/cloud/foo/v1"}: true,
},
},
Expand Down
2 changes: 1 addition & 1 deletion internal/gengapic/mixins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func TestGetOperationPathOverride(t *testing.T) {
want: "/v2/%s",
http: &annotations.Http{
Rules: []*annotations.HttpRule{
&annotations.HttpRule{
{
Selector: "google.longrunning.Operations.GetOperation",
Pattern: &annotations.HttpRule_Get{
Get: "/v2/{operation=projects/*/locations/*/operations/*}",
Expand Down
2 changes: 1 addition & 1 deletion internal/gengapic/testdata/rest_ClientStreamRPC.want
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
func (c *fooRESTClient) ClientStreamRPC(ctx context.Context, opts ...gax.CallOption) (foopb.FooService_ClientStreamRPCClient, error) {
return nil, fmt.Errorf("ClientStreamRPC not yet supported for REST clients")
return nil, errors.New("ClientStreamRPC not yet supported for REST clients")
}

6 changes: 3 additions & 3 deletions internal/gengapic/testdata/rest_ServerStreamRPC.want
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (c *serverStreamRPCRESTClient) Trailer() metadata.MD {

func (c *serverStreamRPCRESTClient) CloseSend() error {
// This is a no-op to fulfill the interface.
return fmt.Errorf("this method is not implemented for a server-stream")
return errors.New("this method is not implemented for a server-stream")
}

func (c *serverStreamRPCRESTClient) Context() context.Context {
Expand All @@ -99,11 +99,11 @@ func (c *serverStreamRPCRESTClient) Context() context.Context {

func (c *serverStreamRPCRESTClient) SendMsg(m interface{}) error {
// This is a no-op to fulfill the interface.
return fmt.Errorf("this method is not implemented for a server-stream")
return errors.New("this method is not implemented for a server-stream")
}

func (c *serverStreamRPCRESTClient) RecvMsg(m interface{}) error {
// This is a no-op to fulfill the interface.
return fmt.Errorf("this method is not implemented, use Recv")
return errors.New("this method is not implemented, use Recv")
}

0 comments on commit c1fd44c

Please sign in to comment.