Ver Fonte

protoc-gen-go/grpc: use status and code packages only if needed (#820)

In the rare event that a proto file only has a service declaration with
no declared methods, it will not depend on status and code.
Make sure these are not imported in such cases.
Joe Tsai há 6 anos atrás
pai
commit
d3c38a4eb4

+ 4 - 6
protoc-gen-go/grpc/grpc.go

@@ -52,10 +52,10 @@ const generatedCodeVersion = 4
 // Paths for packages used by code generated in this file,
 // relative to the import_prefix of the generator.Generator.
 const (
-	errorPkgPath   = "google.golang.org/grpc/status"
 	contextPkgPath = "context"
 	grpcPkgPath    = "google.golang.org/grpc"
 	codePkgPath    = "google.golang.org/grpc/codes"
+	statusPkgPath  = "google.golang.org/grpc/status"
 )
 
 func init() {
@@ -79,8 +79,6 @@ func (g *grpc) Name() string {
 var (
 	contextPkg string
 	grpcPkg    string
-	errorPkg   string
-	codePkg    string
 )
 
 // Init initializes the plugin.
@@ -109,8 +107,6 @@ func (g *grpc) Generate(file *generator.FileDescriptor) {
 		return
 	}
 
-	errorPkg = string(g.gen.AddImport(errorPkgPath))
-	codePkg = string(g.gen.AddImport(codePkgPath))
 	contextPkg = string(g.gen.AddImport(contextPkgPath))
 	grpcPkg = string(g.gen.AddImport(grpcPkgPath))
 
@@ -304,7 +300,9 @@ func (g *grpc) generateServerMethodConcrete(servName string, method *pb.MethodDe
 		nilArg = "nil, "
 	}
 	methName := generator.CamelCase(method.GetName())
-	g.P("return ", nilArg, errorPkg, `.Errorf(codes.Unimplemented, "method `, methName, ` not implemented")`)
+	statusPkg := string(g.gen.AddImport(statusPkgPath))
+	codePkg := string(g.gen.AddImport(codePkgPath))
+	g.P("return ", nilArg, statusPkg, `.Errorf(`, codePkg, `.Unimplemented, "method `, methName, ` not implemented")`)
 	g.P("}")
 }
 

+ 79 - 0
protoc-gen-go/testdata/grpc/grpc_empty.pb.go

@@ -0,0 +1,79 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// source: grpc/grpc_empty.proto
+
+package testing
+
+import (
+	context "context"
+	fmt "fmt"
+	proto "github.com/golang/protobuf/proto"
+	grpc "google.golang.org/grpc"
+	math "math"
+)
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ = proto.Marshal
+var _ = fmt.Errorf
+var _ = math.Inf
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the proto package it is being compiled against.
+// A compilation error at this line likely means your copy of the
+// proto package needs to be updated.
+const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
+
+func init() { proto.RegisterFile("grpc/grpc_empty.proto", fileDescriptor_c580a37f1c90e9b1) }
+
+var fileDescriptor_c580a37f1c90e9b1 = []byte{
+	// 125 bytes of a gzipped FileDescriptorProto
+	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x4d, 0x2f, 0x2a, 0x48,
+	0xd6, 0x07, 0x11, 0xf1, 0xa9, 0xb9, 0x05, 0x25, 0x95, 0x7a, 0x05, 0x45, 0xf9, 0x25, 0xf9, 0x42,
+	0x3c, 0x20, 0x11, 0xbd, 0x92, 0xd4, 0xe2, 0x92, 0xcc, 0xbc, 0x74, 0x23, 0x3e, 0x2e, 0x1e, 0x57,
+	0x90, 0x64, 0x70, 0x6a, 0x51, 0x59, 0x66, 0x72, 0xaa, 0x93, 0x43, 0x94, 0x5d, 0x7a, 0x66, 0x49,
+	0x46, 0x69, 0x92, 0x5e, 0x72, 0x7e, 0xae, 0x7e, 0x7a, 0x7e, 0x4e, 0x62, 0x5e, 0xba, 0x3e, 0x58,
+	0x63, 0x52, 0x69, 0x1a, 0x84, 0x91, 0xac, 0x9b, 0x9e, 0x9a, 0xa7, 0x9b, 0x9e, 0xaf, 0x0f, 0x32,
+	0x23, 0x25, 0xb1, 0x24, 0x11, 0x6c, 0x87, 0x35, 0xd4, 0xc4, 0x24, 0x36, 0xb0, 0x22, 0x63, 0x40,
+	0x00, 0x00, 0x00, 0xff, 0xff, 0x93, 0x1d, 0xf2, 0x47, 0x7f, 0x00, 0x00, 0x00,
+}
+
+// Reference imports to suppress errors if they are not otherwise used.
+var _ context.Context
+var _ grpc.ClientConn
+
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the grpc package it is being compiled against.
+const _ = grpc.SupportPackageIsVersion4
+
+// EmptyServiceClient is the client API for EmptyService service.
+//
+// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
+type EmptyServiceClient interface {
+}
+
+type emptyServiceClient struct {
+	cc *grpc.ClientConn
+}
+
+func NewEmptyServiceClient(cc *grpc.ClientConn) EmptyServiceClient {
+	return &emptyServiceClient{cc}
+}
+
+// EmptyServiceServer is the server API for EmptyService service.
+type EmptyServiceServer interface {
+}
+
+// UnimplementedEmptyServiceServer can be embedded to have forward compatible implementations.
+type UnimplementedEmptyServiceServer struct {
+}
+
+func RegisterEmptyServiceServer(s *grpc.Server, srv EmptyServiceServer) {
+	s.RegisterService(&_EmptyService_serviceDesc, srv)
+}
+
+var _EmptyService_serviceDesc = grpc.ServiceDesc{
+	ServiceName: "grpc.testing.EmptyService",
+	HandlerType: (*EmptyServiceServer)(nil),
+	Methods:     []grpc.MethodDesc{},
+	Streams:     []grpc.StreamDesc{},
+	Metadata:    "grpc/grpc_empty.proto",
+}

+ 38 - 0
protoc-gen-go/testdata/grpc/grpc_empty.proto

@@ -0,0 +1,38 @@
+// Go support for Protocol Buffers - Google's data interchange format
+//
+// Copyright 2019 The Go Authors.  All rights reserved.
+// https://github.com/golang/protobuf
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+//     * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+//     * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+//     * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+syntax = "proto3";
+
+package grpc.testing;
+
+option go_package = "github.com/golang/protobuf/protoc-gen-go/testdata/grpc;testing";
+
+service EmptyService {}