Browse Source

Provide conformance for messages provided by SwiftProtobuf (#811)

Motivation:

We currently generate conformance to `GRPCProtobufPayload` based on the
input and output types of `rpc`s defined in .proto files passed to the
plugin. This works in some situations, including when the .proto file
for the message in question is not present (such as those provided by
the SwiftProtobuf library; `Google_Protobuf_*`). However this is
problematic if the plugin is invoked on a file-by-file basis where each
file contains the message since the conformance would be erroneously
generated mulutple times.

Modifications:

- Provide conformance to all `Google_Protobuf_*` messages currently
  vendored by `SwiftProtobuf`
- Generaate conformance only based on the `message`s defined in each
  .proto file
- Add a few generation tests

Result:

Multiple conformance generation is avoided in the scenario set out
above.
George Barnett 5 years ago
parent
commit
0daec69153
2 changed files with 5 additions and 17 deletions
  1. 1 0
      .gitignore
  2. 4 17
      Sources/protoc-gen-grpc-swift/Generator-Conformance.swift

+ 1 - 0
.gitignore

@@ -17,3 +17,4 @@ third_party/**
 Examples/EchoWeb/dist
 Examples/EchoWeb/node_modules
 Examples/EchoWeb/package-lock.json
+dev/codegen-tests/**/generated/*

+ 4 - 17
Sources/protoc-gen-grpc-swift/Generator-Conformance.swift

@@ -18,23 +18,10 @@ import SwiftProtobufPluginLibrary
 
 extension Generator {
   internal func printProtobufExtensions() {
-    println("// Provides conformance to `GRPCPayload` for request and response messages")
-    for service in self.file.services {
-      self.service = service
-      for method in self.service.methods {
-        self.method = method
-        self.printExtension(for: self.methodInputName)
-        self.printExtension(for: self.methodOutputName)
-      }
-      println()
+    println("// Provides conformance to `GRPCPayload`")
+    for message in self.file.messages {
+      let name = self.protobufNamer.fullName(message: message)
+      self.println("extension \(name): GRPCProtobufPayload {}")
     }
   }
-
-  private func printExtension(for messageType: String) {
-    guard !self.observedMessages.contains(messageType) else {
-      return
-    }
-    self.println("extension \(messageType): GRPCProtobufPayload {}")
-    self.observedMessages.insert(messageType)
-  }
 }