| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- /*
- * Copyright 2018, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- import Foundation
- import SwiftProtobuf
- import SwiftProtobufPluginLibrary
- extension Generator {
- internal func printClient(asynchronousCode: Bool,
- synchronousCode: Bool) {
- for method in service.methods {
- self.method = method
- switch streamingType(method) {
- case .unary:
- printServiceClientMethodCallUnary()
- case .serverStreaming:
- printServiceClientMethodCallServerStreaming()
- case .clientStreaming:
- printServiceClientMethodCallClientStreaming()
- case .bidirectionalStreaming:
- printServiceClientMethodCallBidiStreaming()
- }
- }
- println()
- printServiceClientProtocol(asynchronousCode: asynchronousCode,
- synchronousCode: synchronousCode)
- println()
- printServiceClientProtocolExtension(asynchronousCode: asynchronousCode,
- synchronousCode: synchronousCode)
- println()
- printServiceClientImplementation(asynchronousCode: asynchronousCode,
- synchronousCode: synchronousCode)
- if options.generateTestStubs {
- println()
- printServiceClientTestStubs()
- }
- }
- private func printServiceClientMethodCallUnary() {
- println("\(access) protocol \(callName): ClientCallUnary {}")
- println()
- println("fileprivate final class \(callName)Base: ClientCallUnaryBase<\(methodInputName), \(methodOutputName)>, \(callName) {")
- indent()
- println("override class var method: String { return \(methodPath) }")
- outdent()
- println("}")
- println()
- }
- private func printServiceClientMethodCallServerStreaming() {
- println("\(access) protocol \(callName): ClientCallServerStreaming {")
- indent()
- printStreamReceiveMethods(receivedType: methodOutputName)
- outdent()
- println("}")
- println()
- printStreamReceiveExtension(extendedType: callName, receivedType: methodOutputName)
- println()
- println("fileprivate final class \(callName)Base: ClientCallServerStreamingBase<\(methodInputName), \(methodOutputName)>, \(callName) {")
- indent()
- println("override class var method: String { return \(methodPath) }")
- outdent()
- println("}")
- if options.generateTestStubs {
- println()
- println("class \(callName)TestStub: ClientCallServerStreamingTestStub<\(methodOutputName)>, \(callName) {")
- indent()
- println("override class var method: String { return \(methodPath) }")
- outdent()
- println("}")
- }
- println()
- }
- private func printServiceClientMethodCallClientStreaming() {
- println("\(options.visibility.sourceSnippet) protocol \(callName): ClientCallClientStreaming {")
- indent()
- printStreamSendMethods(sentType: methodInputName)
- println()
- println("/// Call this to close the connection and wait for a response. Blocking.")
- println("func closeAndReceive() throws -> \(methodOutputName)")
- println("/// Call this to close the connection and wait for a response. Nonblocking.")
- println("func closeAndReceive(completion: @escaping (ResultOrRPCError<\(methodOutputName)>) -> Void) throws")
- outdent()
- println("}")
- println()
- printStreamSendExtension(extendedType: callName, sentType: methodInputName)
- println()
- println("fileprivate final class \(callName)Base: ClientCallClientStreamingBase<\(methodInputName), \(methodOutputName)>, \(callName) {")
- indent()
- println("override class var method: String { return \(methodPath) }")
- outdent()
- println("}")
- if options.generateTestStubs {
- println()
- println("/// Simple fake implementation of \(callName)")
- println("/// stores sent values for later verification and finall returns a previously-defined result.")
- println("class \(callName)TestStub: ClientCallClientStreamingTestStub<\(methodInputName), \(methodOutputName)>, \(callName) {")
- indent()
- println("override class var method: String { return \(methodPath) }")
- outdent()
- println("}")
- }
- println()
- }
- private func printServiceClientMethodCallBidiStreaming() {
- println("\(access) protocol \(callName): ClientCallBidirectionalStreaming {")
- indent()
- printStreamReceiveMethods(receivedType: methodOutputName)
- println()
- printStreamSendMethods(sentType: methodInputName)
- println()
- println("/// Call this to close the sending connection. Blocking.")
- println("func closeSend() throws")
- println("/// Call this to close the sending connection. Nonblocking.")
- println("func closeSend(completion: (() -> Void)?) throws")
- outdent()
- println("}")
- println()
- printStreamReceiveExtension(extendedType: callName, receivedType: methodOutputName)
- println()
- printStreamSendExtension(extendedType: callName, sentType: methodInputName)
- println()
- println("fileprivate final class \(callName)Base: ClientCallBidirectionalStreamingBase<\(methodInputName), \(methodOutputName)>, \(callName) {")
- indent()
- println("override class var method: String { return \(methodPath) }")
- outdent()
- println("}")
- if options.generateTestStubs {
- println()
- println("class \(callName)TestStub: ClientCallBidirectionalStreamingTestStub<\(methodInputName), \(methodOutputName)>, \(callName) {")
- indent()
- println("override class var method: String { return \(methodPath) }")
- outdent()
- println("}")
- }
- println()
- }
- 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:
- if synchronousCode {
- println("/// Synchronous. Unary.")
- println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName)")
- }
- if asynchronousCode {
- println("/// Asynchronous. Unary.")
- println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName)")
- }
- case .serverStreaming:
- println("/// Asynchronous. Server-streaming.")
- println("/// Send the initial message.")
- println("/// Use methods on the returned object to get streamed responses.")
- println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName)")
- case .clientStreaming:
- println("/// Asynchronous. Client-streaming.")
- println("/// Use methods on the returned object to stream messages and")
- println("/// to close the connection and wait for a final response.")
- println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName)")
- case .bidirectionalStreaming:
- println("/// Asynchronous. Bidirectional-streaming.")
- println("/// Use methods on the returned object to stream messages,")
- println("/// to wait for replies, and to close the connection.")
- println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName)")
- }
- println()
- }
- outdent()
- println("}")
- }
- private func printServiceClientProtocolExtension(asynchronousCode: Bool,
- synchronousCode: Bool) {
- println("\(options.visibility.sourceSnippet) extension \(serviceClassName) {")
- indent()
- for method in service.methods {
- self.method = method
- switch streamingType(method) {
- case .unary:
- if synchronousCode {
- println("/// Synchronous. Unary.")
- println("func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName) {")
- indent()
- println("return try self.\(methodFunctionName)(request, metadata: self.metadata)")
- outdent()
- println("}")
- }
- if asynchronousCode {
- println("/// Asynchronous. Unary.")
- println("func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
- indent()
- println("return try self.\(methodFunctionName)(request, metadata: self.metadata, completion: completion)")
- outdent()
- println("}")
- }
- case .serverStreaming:
- println("/// Asynchronous. Server-streaming.")
- println("func \(methodFunctionName)(_ request: \(methodInputName), completion: ((CallResult) -> Void)?) throws -> \(callName) {")
- indent()
- println("return try self.\(methodFunctionName)(request, metadata: self.metadata, completion: completion)")
- outdent()
- println("}")
- case .clientStreaming:
- println("/// Asynchronous. Client-streaming.")
- println("func \(methodFunctionName)(completion: ((CallResult) -> Void)?) throws -> \(callName) {")
- indent()
- println("return try self.\(methodFunctionName)(metadata: self.metadata, completion: completion)")
- outdent()
- println("}")
- case .bidirectionalStreaming:
- println("/// Asynchronous. Bidirectional-streaming.")
- println("func \(methodFunctionName)(completion: ((CallResult) -> Void)?) throws -> \(callName) {")
- indent()
- println("return try self.\(methodFunctionName)(metadata: self.metadata, completion: completion)")
- outdent()
- println("}")
- }
- println()
- }
- outdent()
- println("}")
- }
- 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:
- if synchronousCode {
- println("/// Synchronous. Unary.")
- println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName) {")
- indent()
- println("return try \(callName)Base(channel)")
- indent()
- println(".run(request: request, metadata: customMetadata)")
- outdent()
- outdent()
- println("}")
- }
- if asynchronousCode {
- println("/// Asynchronous. Unary.")
- println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
- indent()
- println("return try \(callName)Base(channel)")
- indent()
- println(".start(request: request, metadata: customMetadata, completion: completion)")
- outdent()
- outdent()
- println("}")
- }
- case .serverStreaming:
- println("/// Asynchronous. Server-streaming.")
- println("/// Send the initial message.")
- println("/// Use methods on the returned object to get streamed responses.")
- println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
- indent()
- println("return try \(callName)Base(channel)")
- indent()
- println(".start(request: request, metadata: customMetadata, completion: completion)")
- outdent()
- outdent()
- println("}")
- case .clientStreaming:
- println("/// Asynchronous. Client-streaming.")
- println("/// Use methods on the returned object to stream messages and")
- println("/// to close the connection and wait for a final response.")
- println("\(access) func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
- indent()
- println("return try \(callName)Base(channel)")
- indent()
- println(".start(metadata: customMetadata, completion: completion)")
- outdent()
- outdent()
- println("}")
- case .bidirectionalStreaming:
- println("/// Asynchronous. Bidirectional-streaming.")
- println("/// Use methods on the returned object to stream messages,")
- println("/// to wait for replies, and to close the connection.")
- println("\(access) func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
- indent()
- println("return try \(callName)Base(channel)")
- indent()
- println(".start(metadata: customMetadata, completion: completion)")
- outdent()
- outdent()
- println("}")
- }
- println()
- }
- outdent()
- println("}")
- }
- private func printServiceClientTestStubs() {
- println("class \(serviceClassName)TestStub: ServiceClientTestStubBase, \(serviceClassName) {")
- indent()
- for method in service.methods {
- self.method = method
- switch streamingType(method) {
- case .unary:
- println("var \(methodFunctionName)Requests: [\(methodInputName)] = []")
- println("var \(methodFunctionName)Responses: [\(methodOutputName)] = []")
- println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName) {")
- indent()
- println("\(methodFunctionName)Requests.append(request)")
- println("defer { \(methodFunctionName)Responses.removeFirst() }")
- println("return \(methodFunctionName)Responses.first!")
- outdent()
- println("}")
- println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
- indent()
- println("fatalError(\"not implemented\")")
- outdent()
- println("}")
- case .serverStreaming:
- println("var \(methodFunctionName)Requests: [\(methodInputName)] = []")
- println("var \(methodFunctionName)Calls: [\(callName)] = []")
- println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
- indent()
- println("\(methodFunctionName)Requests.append(request)")
- println("defer { \(methodFunctionName)Calls.removeFirst() }")
- println("return \(methodFunctionName)Calls.first!")
- outdent()
- println("}")
- case .clientStreaming:
- println("var \(methodFunctionName)Calls: [\(callName)] = []")
- println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
- indent()
- println("defer { \(methodFunctionName)Calls.removeFirst() }")
- println("return \(methodFunctionName)Calls.first!")
- outdent()
- println("}")
- case .bidirectionalStreaming:
- println("var \(methodFunctionName)Calls: [\(callName)] = []")
- println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
- indent()
- println("defer { \(methodFunctionName)Calls.removeFirst() }")
- println("return \(methodFunctionName)Calls.first!")
- outdent()
- println("}")
- }
- println()
- }
- outdent()
- println("}")
- }
- }
|