Просмотр исходного кода

Only generate imports in GenerateImports,
and move the `var _ pkg.Blah` lines to Generate time.

This helps in the case of multiple plugins.

Fixes #57.

David Symonds 10 лет назад
Родитель
Сommit
73aaaa9eb6
1 измененных файлов с 7 добавлено и 4 удалено
  1. 7 4
      protoc-gen-go/internal/grpc/grpc.go

+ 7 - 4
protoc-gen-go/internal/grpc/grpc.go

@@ -98,6 +98,13 @@ func (g *grpc) P(args ...interface{}) { g.gen.P(args...) }
 
 // Generate generates code for the services in the given file.
 func (g *grpc) Generate(file *generator.FileDescriptor) {
+	if len(file.FileDescriptorProto.Service) == 0 {
+		return
+	}
+	g.P("// Reference imports to suppress errors if they are not otherwise used.")
+	g.P("var _ ", contextPkg, ".Context")
+	g.P("var _ ", grpcPkg, ".ClientConn")
+	g.P()
 	for i, service := range file.FileDescriptorProto.Service {
 		g.generateService(file, service, i)
 	}
@@ -113,10 +120,6 @@ func (g *grpc) GenerateImports(file *generator.FileDescriptor) {
 	g.P(grpcPkg, " ", strconv.Quote(path.Join(g.gen.ImportPrefix, grpcPkgPath)))
 	g.P(")")
 	g.P()
-	g.P("// Reference imports to suppress errors if they are not otherwise used.")
-	g.P("var _ ", contextPkg, ".Context")
-	g.P("var _ ", grpcPkg, ".ClientConn")
-	g.P()
 }
 
 // reservedClientName records whether a client name is reserved on the client side.