Skip to content

Commit

Permalink
protoc-gen-go/grpc: use status and code packages only if needed (#820)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dsnet authored and neild committed Mar 18, 2019
1 parent b85cd75 commit d3c38a4
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 6 deletions.
10 changes: 4 additions & 6 deletions protoc-gen-go/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -79,8 +79,6 @@ func (g *grpc) Name() string {
var (
contextPkg string
grpcPkg string
errorPkg string
codePkg string
)

// Init initializes the plugin.
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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("}")
}

Expand Down
79 changes: 79 additions & 0 deletions protoc-gen-go/testdata/grpc/grpc_empty.pb.go

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

38 changes: 38 additions & 0 deletions protoc-gen-go/testdata/grpc/grpc_empty.proto
Original file line number Diff line number Diff line change
@@ -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 {}

0 comments on commit d3c38a4

Please sign in to comment.