Skip to content

Commit

Permalink
Transmission type parameters for sync/async code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
pontus-andersson committed Oct 16, 2018
1 parent 23a0ebd commit 611e527
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 28 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ separated from the output directory by a colon.
| `Visibility` | `Internal`/`Public` | `Internal` | ACL of generated code |
| `Server` | `true`/`false` | `true` | Whether to generate server 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 |

Example:
Expand Down
67 changes: 40 additions & 27 deletions Sources/protoc-gen-swiftgrpc/Generator-Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import SwiftProtobuf
import SwiftProtobufPluginLibrary

extension Generator {
internal func printClient() {
internal func printClient(asynchronousCode: Bool,
synchronousCode: Bool) {
for method in service.methods {
self.method = method
switch streamingType(method) {
Expand All @@ -33,9 +34,11 @@ extension Generator {
}
}
println()
printServiceClientProtocol()
printServiceClientProtocol(asynchronousCode: asynchronousCode,
synchronousCode: synchronousCode)
println()
printServiceClientImplementation()
printServiceClientImplementation(asynchronousCode: asynchronousCode,
synchronousCode: synchronousCode)
if options.generateTestStubs {
println()
printServiceClientTestStubs()
Expand Down Expand Up @@ -144,18 +147,23 @@ extension Generator {
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("\(options.visibility.sourceSnippet) protocol \(serviceClassName): ServiceClient {")
indent()
for method in service.methods {
self.method = method
switch streamingType(method) {
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:
println("/// Asynchronous. Server-streaming.")
println("/// Send the initial message.")
Expand All @@ -178,31 +186,36 @@ extension Generator {
println("}")
}

private func printServiceClientImplementation() {
private func printServiceClientImplementation(asynchronousCode: Bool,
synchronousCode: Bool) {
println("\(access) final class \(serviceClassName)Client: ServiceClientBase, \(serviceClassName) {")
indent()
for method in service.methods {
self.method = method
switch streamingType(method) {
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:
println("/// Asynchronous. Server-streaming.")
println("/// Send the initial message.")
Expand Down
3 changes: 2 additions & 1 deletion Sources/protoc-gen-swiftgrpc/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class Generator {
if options.generateClient {
for service in file.services {
self.service = service
printClient()
printClient(asynchronousCode: options.generateAsynchronous,
synchronousCode: options.generateSynchronous)
}
}
println()
Expand Down
16 changes: 16 additions & 0 deletions Sources/protoc-gen-swiftgrpc/options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ final class GeneratorOptions {
private(set) var visibility = Visibility.internal
private(set) var generateServer = true
private(set) var generateClient = true
private(set) var generateAsynchronous = true
private(set) var generateSynchronous = true
private(set) var generateTestStubs = false

init(parameter: String?) throws {
Expand All @@ -74,6 +76,20 @@ final class GeneratorOptions {
} else {
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":
if let value = Bool(pair.value) {
Expand Down

0 comments on commit 611e527

Please sign in to comment.