Skip to content

Commit 611e527

Browse files
Transmission type parameters for sync/async code generation
1 parent 23a0ebd commit 611e527

File tree

4 files changed

+60
-28
lines changed

4 files changed

+60
-28
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ separated from the output directory by a colon.
9797
| `Visibility` | `Internal`/`Public` | `Internal` | ACL of generated code |
9898
| `Server` | `true`/`false` | `true` | Whether to generate server code |
9999
| `Client` | `true`/`false` | `true` | Whether to generate client code |
100+
| `Async` | `true`/`false` | `true` | Whether to generate asynchronous code |
101+
| `Sync` | `true`/`false` | `true` | Whether to generate synchronous code |
100102
| `TestStubs` | `true`/`false` | `false` | Whether to generate test stub code |
101103

102104
Example:

Sources/protoc-gen-swiftgrpc/Generator-Client.swift

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import SwiftProtobuf
1818
import SwiftProtobufPluginLibrary
1919

2020
extension Generator {
21-
internal func printClient() {
21+
internal func printClient(asynchronousCode: Bool,
22+
synchronousCode: Bool) {
2223
for method in service.methods {
2324
self.method = method
2425
switch streamingType(method) {
@@ -33,9 +34,11 @@ extension Generator {
3334
}
3435
}
3536
println()
36-
printServiceClientProtocol()
37+
printServiceClientProtocol(asynchronousCode: asynchronousCode,
38+
synchronousCode: synchronousCode)
3739
println()
38-
printServiceClientImplementation()
40+
printServiceClientImplementation(asynchronousCode: asynchronousCode,
41+
synchronousCode: synchronousCode)
3942
if options.generateTestStubs {
4043
println()
4144
printServiceClientTestStubs()
@@ -144,18 +147,23 @@ extension Generator {
144147
println()
145148
}
146149

147-
private func printServiceClientProtocol() {
150+
private func printServiceClientProtocol(asynchronousCode: Bool,
151+
synchronousCode: Bool) {
148152
println("/// Instantiate \(serviceClassName)Client, then call methods of this protocol to make API calls.")
149153
println("\(options.visibility.sourceSnippet) protocol \(serviceClassName): ServiceClient {")
150154
indent()
151155
for method in service.methods {
152156
self.method = method
153157
switch streamingType(method) {
154158
case .unary:
155-
println("/// Synchronous. Unary.")
156-
println("func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName)")
157-
println("/// Asynchronous. Unary.")
158-
println("func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName)")
159+
if synchronousCode {
160+
println("/// Synchronous. Unary.")
161+
println("func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName)")
162+
}
163+
if asynchronousCode {
164+
println("/// Asynchronous. Unary.")
165+
println("func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName)")
166+
}
159167
case .serverStreaming:
160168
println("/// Asynchronous. Server-streaming.")
161169
println("/// Send the initial message.")
@@ -178,31 +186,36 @@ extension Generator {
178186
println("}")
179187
}
180188

181-
private func printServiceClientImplementation() {
189+
private func printServiceClientImplementation(asynchronousCode: Bool,
190+
synchronousCode: Bool) {
182191
println("\(access) final class \(serviceClassName)Client: ServiceClientBase, \(serviceClassName) {")
183192
indent()
184193
for method in service.methods {
185194
self.method = method
186195
switch streamingType(method) {
187196
case .unary:
188-
println("/// Synchronous. Unary.")
189-
println("\(access) func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName) {")
190-
indent()
191-
println("return try \(callName)Base(channel)")
192-
indent()
193-
println(".run(request: request, metadata: metadata)")
194-
outdent()
195-
outdent()
196-
println("}")
197-
println("/// Asynchronous. Unary.")
198-
println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
199-
indent()
200-
println("return try \(callName)Base(channel)")
201-
indent()
202-
println(".start(request: request, metadata: metadata, completion: completion)")
203-
outdent()
204-
outdent()
205-
println("}")
197+
if synchronousCode {
198+
println("/// Synchronous. Unary.")
199+
println("\(access) func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName) {")
200+
indent()
201+
println("return try \(callName)Base(channel)")
202+
indent()
203+
println(".run(request: request, metadata: metadata)")
204+
outdent()
205+
outdent()
206+
println("}")
207+
}
208+
if asynchronousCode {
209+
println("/// Asynchronous. Unary.")
210+
println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
211+
indent()
212+
println("return try \(callName)Base(channel)")
213+
indent()
214+
println(".start(request: request, metadata: metadata, completion: completion)")
215+
outdent()
216+
outdent()
217+
println("}")
218+
}
206219
case .serverStreaming:
207220
println("/// Asynchronous. Server-streaming.")
208221
println("/// Send the initial message.")

Sources/protoc-gen-swiftgrpc/Generator.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ class Generator {
8181
if options.generateClient {
8282
for service in file.services {
8383
self.service = service
84-
printClient()
84+
printClient(asynchronousCode: options.generateAsynchronous,
85+
synchronousCode: options.generateSynchronous)
8586
}
8687
}
8788
println()

Sources/protoc-gen-swiftgrpc/options.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ final class GeneratorOptions {
4949
private(set) var visibility = Visibility.internal
5050
private(set) var generateServer = true
5151
private(set) var generateClient = true
52+
private(set) var generateAsynchronous = true
53+
private(set) var generateSynchronous = true
5254
private(set) var generateTestStubs = false
5355

5456
init(parameter: String?) throws {
@@ -74,6 +76,20 @@ final class GeneratorOptions {
7476
} else {
7577
throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
7678
}
79+
80+
case "Async":
81+
if let value = Bool(pair.value) {
82+
generateAsynchronous = value
83+
} else {
84+
throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
85+
}
86+
87+
case "Sync":
88+
if let value = Bool(pair.value) {
89+
generateSynchronous = value
90+
} else {
91+
throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
92+
}
7793

7894
case "TestStubs":
7995
if let value = Bool(pair.value) {

0 commit comments

Comments
 (0)