Browse Source

Merge pull request #316 from pontus-andersson/transmission-sync-option

Transmission type parameters for sync/async code generation
Tim Burks 7 years ago
parent
commit
d00d98715b

+ 2 - 0
README.md

@@ -97,6 +97,8 @@ separated from the output directory by a colon.
 | `Visibility` | `Internal`/`Public` | `Internal` | ACL of generated code |
 | `Visibility` | `Internal`/`Public` | `Internal` | ACL of generated code |
 | `Server` |  `true`/`false` | `true` | Whether to generate server code |
 | `Server` |  `true`/`false` | `true` | Whether to generate server code |
 | `Client` |  `true`/`false` | `true` | Whether to generate client code |
 | `Client` |  `true`/`false` | `true` | Whether to generate client code |
+| `Async` |  `true`/`false` | `true` | Whether to generate asynchronous code |
+| `Sync` |  `true`/`false` | `true` | Whether to generate synchronous code |
 | `TestStubs` |  `true`/`false` | `false` | Whether to generate test stub code |
 | `TestStubs` |  `true`/`false` | `false` | Whether to generate test stub code |
 
 
 Example:
 Example:

+ 40 - 27
Sources/protoc-gen-swiftgrpc/Generator-Client.swift

@@ -18,7 +18,8 @@ import SwiftProtobuf
 import SwiftProtobufPluginLibrary
 import SwiftProtobufPluginLibrary
 
 
 extension Generator {
 extension Generator {
-  internal func printClient() {
+  internal func printClient(asynchronousCode: Bool,
+                            synchronousCode: Bool) {
     for method in service.methods {
     for method in service.methods {
       self.method = method
       self.method = method
       switch streamingType(method) {
       switch streamingType(method) {
@@ -33,9 +34,11 @@ extension Generator {
       }
       }
     }
     }
     println()
     println()
-    printServiceClientProtocol()
+    printServiceClientProtocol(asynchronousCode: asynchronousCode,
+                               synchronousCode: synchronousCode)
     println()
     println()
-    printServiceClientImplementation()
+    printServiceClientImplementation(asynchronousCode: asynchronousCode,
+                                     synchronousCode: synchronousCode)
     if options.generateTestStubs {
     if options.generateTestStubs {
       println()
       println()
       printServiceClientTestStubs()
       printServiceClientTestStubs()
@@ -144,7 +147,8 @@ extension Generator {
     println()
     println()
   }
   }
 
 
-  private func printServiceClientProtocol() {
+  private func printServiceClientProtocol(asynchronousCode: Bool,
+                                          synchronousCode: Bool) {
     println("/// Instantiate \(serviceClassName)Client, then call methods of this protocol to make API calls.")
     println("/// Instantiate \(serviceClassName)Client, then call methods of this protocol to make API calls.")
     println("\(options.visibility.sourceSnippet) protocol \(serviceClassName): ServiceClient {")
     println("\(options.visibility.sourceSnippet) protocol \(serviceClassName): ServiceClient {")
     indent()
     indent()
@@ -152,10 +156,14 @@ extension Generator {
       self.method = method
       self.method = method
       switch streamingType(method) {
       switch streamingType(method) {
       case .unary:
       case .unary:
-        println("/// Synchronous. Unary.")
-        println("func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName)")
-        println("/// Asynchronous. Unary.")
-        println("func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName)")
+        if synchronousCode {
+          println("/// Synchronous. Unary.")
+          println("func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName)")
+        }
+        if asynchronousCode {
+          println("/// Asynchronous. Unary.")
+          println("func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName)")
+        }
       case .serverStreaming:
       case .serverStreaming:
         println("/// Asynchronous. Server-streaming.")
         println("/// Asynchronous. Server-streaming.")
         println("/// Send the initial message.")
         println("/// Send the initial message.")
@@ -178,31 +186,36 @@ extension Generator {
     println("}")
     println("}")
   }
   }
 
 
-  private func printServiceClientImplementation() {
+  private func printServiceClientImplementation(asynchronousCode: Bool,
+                                                synchronousCode: Bool) {
     println("\(access) final class \(serviceClassName)Client: ServiceClientBase, \(serviceClassName) {")
     println("\(access) final class \(serviceClassName)Client: ServiceClientBase, \(serviceClassName) {")
     indent()
     indent()
     for method in service.methods {
     for method in service.methods {
       self.method = method
       self.method = method
       switch streamingType(method) {
       switch streamingType(method) {
       case .unary:
       case .unary:
-        println("/// Synchronous. Unary.")
-        println("\(access) func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName) {")
-        indent()
-        println("return try \(callName)Base(channel)")
-        indent()
-        println(".run(request: request, metadata: metadata)")
-        outdent()
-        outdent()
-        println("}")
-        println("/// Asynchronous. Unary.")
-        println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
-        indent()
-        println("return try \(callName)Base(channel)")
-        indent()
-        println(".start(request: request, metadata: metadata, completion: completion)")
-        outdent()
-        outdent()
-        println("}")
+        if synchronousCode {
+          println("/// Synchronous. Unary.")
+          println("\(access) func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName) {")
+          indent()
+          println("return try \(callName)Base(channel)")
+          indent()
+          println(".run(request: request, metadata: metadata)")
+          outdent()
+          outdent()
+          println("}")
+        }
+        if asynchronousCode {
+          println("/// Asynchronous. Unary.")
+          println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
+          indent()
+          println("return try \(callName)Base(channel)")
+          indent()
+          println(".start(request: request, metadata: metadata, completion: completion)")
+          outdent()
+          outdent()
+          println("}")
+        }
       case .serverStreaming:
       case .serverStreaming:
         println("/// Asynchronous. Server-streaming.")
         println("/// Asynchronous. Server-streaming.")
         println("/// Send the initial message.")
         println("/// Send the initial message.")

+ 2 - 1
Sources/protoc-gen-swiftgrpc/Generator.swift

@@ -81,7 +81,8 @@ class Generator {
     if options.generateClient {
     if options.generateClient {
       for service in file.services {
       for service in file.services {
         self.service = service
         self.service = service
-        printClient()
+        printClient(asynchronousCode: options.generateAsynchronous,
+                    synchronousCode: options.generateSynchronous)
       }
       }
     }
     }
     println()
     println()

+ 16 - 0
Sources/protoc-gen-swiftgrpc/options.swift

@@ -49,6 +49,8 @@ final class GeneratorOptions {
   private(set) var visibility = Visibility.internal
   private(set) var visibility = Visibility.internal
   private(set) var generateServer = true
   private(set) var generateServer = true
   private(set) var generateClient = true
   private(set) var generateClient = true
+  private(set) var generateAsynchronous = true
+  private(set) var generateSynchronous = true
   private(set) var generateTestStubs = false
   private(set) var generateTestStubs = false
 
 
   init(parameter: String?) throws {
   init(parameter: String?) throws {
@@ -74,6 +76,20 @@ final class GeneratorOptions {
         } else {
         } else {
           throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
           throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
         }
         }
+        
+      case "Async":
+        if let value = Bool(pair.value) {
+          generateAsynchronous = value
+        } else {
+          throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
+        }
+        
+      case "Sync":
+        if let value = Bool(pair.value) {
+          generateSynchronous = value
+        } else {
+          throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
+        }
 
 
       case "TestStubs":
       case "TestStubs":
         if let value = Bool(pair.value) {
         if let value = Bool(pair.value) {