Browse Source

Generate conformances files containing a message are passed to the plugin (#837)

Motivation:

There was some code in the codegen which checked whether the codegen
request contained any services. If there were no services in the request
then no code would be generated. This was fine when we generated
conformance based on when request/response types of a service. Less see
now.

Modifications:

- Remove the conditional check
- Remove some related dead code
- Add a couple of tests which validate that we do the right thing when
  we generate a file with just a service and the same for just a
  message.

Result:

Less broken codegen
George Barnett 5 years ago
parent
commit
363bb788f4

+ 3 - 0
Sources/protoc-gen-grpc-swift/Generator-Conformance.swift

@@ -18,6 +18,9 @@ import SwiftProtobufPluginLibrary
 
 extension Generator {
   internal func printProtobufExtensions() {
+    if self.file.messages.isEmpty {
+      return
+    }
     println("// Provides conformance to `GRPCPayload`")
     for message in self.file.messages {
       let name = self.protobufNamer.fullName(message: message)

+ 1 - 3
Sources/protoc-gen-grpc-swift/Generator.swift

@@ -23,13 +23,11 @@ class Generator {
   internal var service: ServiceDescriptor! // context during generation
   internal var method: MethodDescriptor!   // context during generation
 
-  internal var observedMessages: Set<String>
   internal let protobufNamer: SwiftProtobufNamer
 
-  init(_ file: FileDescriptor, options: GeneratorOptions, observedMessages: Set<String>) {
+  init(_ file: FileDescriptor, options: GeneratorOptions) {
     self.file = file
     self.options = options
-    self.observedMessages = observedMessages
     self.printer = CodePrinter()
     self.protobufNamer = SwiftProtobufNamer(
       currentFile: file,

+ 6 - 13
Sources/protoc-gen-grpc-swift/main.swift

@@ -102,23 +102,16 @@ func main() throws {
   // Build the SwiftProtobufPluginLibrary model of the plugin input
   let descriptorSet = DescriptorSet(protos: request.protoFile)
 
-  // We need to generate conformance to `GRPCPayload` for request/response types. Track which
-  // types we've seen to avoid generating the conformance multiple times.
-  var observedMessages = Set<String>()
-
   // process each .proto file in filename order in an attempt to stabilise the output (i.e. where
   // conformance to `GRPCPayload` is generated)
   for name in request.fileToGenerate.sorted() {
     let fileDescriptor = descriptorSet.lookupFileDescriptor(protoName: name)
-    if fileDescriptor.services.count > 0 {
-      let grpcFileName = uniqueOutputFileName(component: "grpc", fileDescriptor: fileDescriptor, fileNamingOption: options.fileNaming)
-      let grpcGenerator = Generator(fileDescriptor, options: options, observedMessages: observedMessages)
-      var grpcFile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
-      grpcFile.name = grpcFileName
-      grpcFile.content = grpcGenerator.code
-      response.file.append(grpcFile)
-      observedMessages.formUnion(grpcGenerator.observedMessages)
-    }
+    let grpcFileName = uniqueOutputFileName(component: "grpc", fileDescriptor: fileDescriptor, fileNamingOption: options.fileNaming)
+    let grpcGenerator = Generator(fileDescriptor, options: options)
+    var grpcFile = Google_Protobuf_Compiler_CodeGeneratorResponse.File()
+    grpcFile.name = grpcFileName
+    grpcFile.content = grpcGenerator.code
+    response.file.append(grpcFile)
   }
 
   // return everything to the caller