Jelajahi Sumber

Allow separate generation of test clients (#881)

Motivation:

Users should be able to generate their test clients separately to their
real clients so that their real clients can be public and their test
clients internal.

Modifications:

- Allow test clients to be generated separately from real clients
- Add an option to control whether conformance is generated; this allows
  test clients to be generated separately from real clients when the
  service and messages are defined in a single file without duplicating
  the conformance.
- Expand on the plugin documentation.

Result:

Resolves #878
George Barnett 5 tahun lalu
induk
melakukan
0c3c8c016d

+ 8 - 6
Sources/protoc-gen-grpc-swift/Generator-Client.swift

@@ -19,12 +19,14 @@ import SwiftProtobufPluginLibrary
 
 extension Generator {
   internal func printClient() {
-    println()
-    printServiceClientProtocol()
-    println()
-    printClientProtocolExtension()
-    println()
-    printServiceClientImplementation()
+    if self.options.generateClient {
+      self.println()
+      self.printServiceClientProtocol()
+      self.println()
+      self.printClientProtocolExtension()
+      self.println()
+      self.printServiceClientImplementation()
+    }
 
     if self.options.generateTestClient {
       self.println()

+ 10 - 7
Sources/protoc-gen-grpc-swift/Generator.swift

@@ -104,11 +104,11 @@ class Generator {
     }
     println()
 
-    if options.generateClient {
-      for service in file.services {
-        self.service = service
-        printClient()
-      }
+    // We defer the check for printing clients to `printClient()` since this could be the 'real'
+    // client or the test client.
+    for service in file.services {
+      self.service = service
+      self.printClient()
     }
     println()
 
@@ -118,7 +118,10 @@ class Generator {
         printServer()
       }
     }
-    self.println()
-    self.printProtobufExtensions()
+
+    if options.generatePayloadConformance {
+      self.println()
+      self.printProtobufExtensions()
+    }
   }
 }

+ 8 - 0
Sources/protoc-gen-grpc-swift/options.swift

@@ -55,6 +55,7 @@ final class GeneratorOptions {
   private(set) var generateServer = true
   private(set) var generateClient = true
   private(set) var generateTestClient = false
+  private(set) var generatePayloadConformance = true
   private(set) var protoToModuleMappings = ProtoFileToModuleMappings()
   private(set) var fileNaming = FileNaming.FullPath
   private(set) var extraModuleImports: [String] = []
@@ -90,6 +91,13 @@ final class GeneratorOptions {
           throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
         }
 
+      case "PayloadConformance":
+        if let value = Bool(pair.value) {
+          self.generatePayloadConformance = value
+        } else {
+          throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
+        }
+
       case "ProtoPathModuleMappings":
         if !pair.value.isEmpty {
           do {