Generator-Client.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * Copyright 2018, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. import SwiftProtobuf
  18. import SwiftProtobufPluginLibrary
  19. extension Generator {
  20. internal func printClient(asynchronousCode: Bool,
  21. synchronousCode: Bool) {
  22. if options.generateNIOImplementation {
  23. printNIOGRPCClient()
  24. } else {
  25. printCGRPCClient(asynchronousCode: asynchronousCode,
  26. synchronousCode: synchronousCode)
  27. }
  28. }
  29. private func printCGRPCClient(asynchronousCode: Bool,
  30. synchronousCode: Bool) {
  31. for method in service.methods {
  32. self.method = method
  33. switch streamingType(method) {
  34. case .unary:
  35. printServiceClientMethodCallUnary()
  36. case .serverStreaming:
  37. printServiceClientMethodCallServerStreaming()
  38. case .clientStreaming:
  39. printServiceClientMethodCallClientStreaming()
  40. case .bidirectionalStreaming:
  41. printServiceClientMethodCallBidiStreaming()
  42. }
  43. }
  44. println()
  45. printServiceClientProtocol(asynchronousCode: asynchronousCode,
  46. synchronousCode: synchronousCode)
  47. println()
  48. printServiceClientProtocolExtension(asynchronousCode: asynchronousCode,
  49. synchronousCode: synchronousCode)
  50. println()
  51. printServiceClientImplementation(asynchronousCode: asynchronousCode,
  52. synchronousCode: synchronousCode)
  53. if options.generateTestStubs {
  54. println()
  55. printServiceClientTestStubs()
  56. }
  57. }
  58. private func printServiceClientMethodCallUnary() {
  59. println("\(access) protocol \(callName): ClientCallUnary {}")
  60. println()
  61. println("fileprivate final class \(callName)Base: ClientCallUnaryBase<\(methodInputName), \(methodOutputName)>, \(callName) {")
  62. indent()
  63. println("override class var method: String { return \(methodPath) }")
  64. outdent()
  65. println("}")
  66. println()
  67. }
  68. private func printServiceClientMethodCallServerStreaming() {
  69. println("\(access) protocol \(callName): ClientCallServerStreaming {")
  70. indent()
  71. printStreamReceiveMethods(receivedType: methodOutputName)
  72. outdent()
  73. println("}")
  74. println()
  75. printStreamReceiveExtension(extendedType: callName, receivedType: methodOutputName)
  76. println()
  77. println("fileprivate final class \(callName)Base: ClientCallServerStreamingBase<\(methodInputName), \(methodOutputName)>, \(callName) {")
  78. indent()
  79. println("override class var method: String { return \(methodPath) }")
  80. outdent()
  81. println("}")
  82. if options.generateTestStubs {
  83. println()
  84. println("class \(callName)TestStub: ClientCallServerStreamingTestStub<\(methodOutputName)>, \(callName) {")
  85. indent()
  86. println("override class var method: String { return \(methodPath) }")
  87. outdent()
  88. println("}")
  89. }
  90. println()
  91. }
  92. private func printServiceClientMethodCallClientStreaming() {
  93. println("\(options.visibility.sourceSnippet) protocol \(callName): ClientCallClientStreaming {")
  94. indent()
  95. printStreamSendMethods(sentType: methodInputName)
  96. println()
  97. println("/// Call this to close the connection and wait for a response. Blocking.")
  98. println("func closeAndReceive() throws -> \(methodOutputName)")
  99. println("/// Call this to close the connection and wait for a response. Nonblocking.")
  100. println("func closeAndReceive(completion: @escaping (ResultOrRPCError<\(methodOutputName)>) -> Void) throws")
  101. outdent()
  102. println("}")
  103. println()
  104. printStreamSendExtension(extendedType: callName, sentType: methodInputName)
  105. println()
  106. println("fileprivate final class \(callName)Base: ClientCallClientStreamingBase<\(methodInputName), \(methodOutputName)>, \(callName) {")
  107. indent()
  108. println("override class var method: String { return \(methodPath) }")
  109. outdent()
  110. println("}")
  111. if options.generateTestStubs {
  112. println()
  113. println("/// Simple fake implementation of \(callName)")
  114. println("/// stores sent values for later verification and finall returns a previously-defined result.")
  115. println("class \(callName)TestStub: ClientCallClientStreamingTestStub<\(methodInputName), \(methodOutputName)>, \(callName) {")
  116. indent()
  117. println("override class var method: String { return \(methodPath) }")
  118. outdent()
  119. println("}")
  120. }
  121. println()
  122. }
  123. private func printServiceClientMethodCallBidiStreaming() {
  124. println("\(access) protocol \(callName): ClientCallBidirectionalStreaming {")
  125. indent()
  126. printStreamReceiveMethods(receivedType: methodOutputName)
  127. println()
  128. printStreamSendMethods(sentType: methodInputName)
  129. println()
  130. println("/// Call this to close the sending connection. Blocking.")
  131. println("func closeSend() throws")
  132. println("/// Call this to close the sending connection. Nonblocking.")
  133. println("func closeSend(completion: (() -> Void)?) throws")
  134. outdent()
  135. println("}")
  136. println()
  137. printStreamReceiveExtension(extendedType: callName, receivedType: methodOutputName)
  138. println()
  139. printStreamSendExtension(extendedType: callName, sentType: methodInputName)
  140. println()
  141. println("fileprivate final class \(callName)Base: ClientCallBidirectionalStreamingBase<\(methodInputName), \(methodOutputName)>, \(callName) {")
  142. indent()
  143. println("override class var method: String { return \(methodPath) }")
  144. outdent()
  145. println("}")
  146. if options.generateTestStubs {
  147. println()
  148. println("class \(callName)TestStub: ClientCallBidirectionalStreamingTestStub<\(methodInputName), \(methodOutputName)>, \(callName) {")
  149. indent()
  150. println("override class var method: String { return \(methodPath) }")
  151. outdent()
  152. println("}")
  153. }
  154. println()
  155. }
  156. private func printServiceClientProtocol(asynchronousCode: Bool,
  157. synchronousCode: Bool) {
  158. println("/// Instantiate \(serviceClassName)Client, then call methods of this protocol to make API calls.")
  159. println("\(options.visibility.sourceSnippet) protocol \(serviceClassName): ServiceClient {")
  160. indent()
  161. for method in service.methods {
  162. self.method = method
  163. switch streamingType(method) {
  164. case .unary:
  165. if synchronousCode {
  166. println("/// Synchronous. Unary.")
  167. println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName)")
  168. }
  169. if asynchronousCode {
  170. println("/// Asynchronous. Unary.")
  171. println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName)")
  172. }
  173. case .serverStreaming:
  174. println("/// Asynchronous. Server-streaming.")
  175. println("/// Send the initial message.")
  176. println("/// Use methods on the returned object to get streamed responses.")
  177. println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName)")
  178. case .clientStreaming:
  179. println("/// Asynchronous. Client-streaming.")
  180. println("/// Use methods on the returned object to stream messages and")
  181. println("/// to close the connection and wait for a final response.")
  182. println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName)")
  183. case .bidirectionalStreaming:
  184. println("/// Asynchronous. Bidirectional-streaming.")
  185. println("/// Use methods on the returned object to stream messages,")
  186. println("/// to wait for replies, and to close the connection.")
  187. println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName)")
  188. }
  189. println()
  190. }
  191. outdent()
  192. println("}")
  193. }
  194. private func printServiceClientProtocolExtension(asynchronousCode: Bool,
  195. synchronousCode: Bool) {
  196. println("\(options.visibility.sourceSnippet) extension \(serviceClassName) {")
  197. indent()
  198. for method in service.methods {
  199. self.method = method
  200. switch streamingType(method) {
  201. case .unary:
  202. if synchronousCode {
  203. println("/// Synchronous. Unary.")
  204. println("func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName) {")
  205. indent()
  206. println("return try self.\(methodFunctionName)(request, metadata: self.metadata)")
  207. outdent()
  208. println("}")
  209. }
  210. if asynchronousCode {
  211. println("/// Asynchronous. Unary.")
  212. println("func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
  213. indent()
  214. println("return try self.\(methodFunctionName)(request, metadata: self.metadata, completion: completion)")
  215. outdent()
  216. println("}")
  217. }
  218. case .serverStreaming:
  219. println("/// Asynchronous. Server-streaming.")
  220. println("func \(methodFunctionName)(_ request: \(methodInputName), completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  221. indent()
  222. println("return try self.\(methodFunctionName)(request, metadata: self.metadata, completion: completion)")
  223. outdent()
  224. println("}")
  225. case .clientStreaming:
  226. println("/// Asynchronous. Client-streaming.")
  227. println("func \(methodFunctionName)(completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  228. indent()
  229. println("return try self.\(methodFunctionName)(metadata: self.metadata, completion: completion)")
  230. outdent()
  231. println("}")
  232. case .bidirectionalStreaming:
  233. println("/// Asynchronous. Bidirectional-streaming.")
  234. println("func \(methodFunctionName)(completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  235. indent()
  236. println("return try self.\(methodFunctionName)(metadata: self.metadata, completion: completion)")
  237. outdent()
  238. println("}")
  239. }
  240. println()
  241. }
  242. outdent()
  243. println("}")
  244. }
  245. private func printServiceClientImplementation(asynchronousCode: Bool,
  246. synchronousCode: Bool) {
  247. println("\(access) final class \(serviceClassName)Client: ServiceClientBase, \(serviceClassName) {")
  248. indent()
  249. for method in service.methods {
  250. self.method = method
  251. switch streamingType(method) {
  252. case .unary:
  253. if synchronousCode {
  254. println("/// Synchronous. Unary.")
  255. println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName) {")
  256. indent()
  257. println("return try \(callName)Base(channel)")
  258. indent()
  259. println(".run(request: request, metadata: customMetadata)")
  260. outdent()
  261. outdent()
  262. println("}")
  263. }
  264. if asynchronousCode {
  265. println("/// Asynchronous. Unary.")
  266. println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
  267. indent()
  268. println("return try \(callName)Base(channel)")
  269. indent()
  270. println(".start(request: request, metadata: customMetadata, completion: completion)")
  271. outdent()
  272. outdent()
  273. println("}")
  274. }
  275. case .serverStreaming:
  276. println("/// Asynchronous. Server-streaming.")
  277. println("/// Send the initial message.")
  278. println("/// Use methods on the returned object to get streamed responses.")
  279. println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  280. indent()
  281. println("return try \(callName)Base(channel)")
  282. indent()
  283. println(".start(request: request, metadata: customMetadata, completion: completion)")
  284. outdent()
  285. outdent()
  286. println("}")
  287. case .clientStreaming:
  288. println("/// Asynchronous. Client-streaming.")
  289. println("/// Use methods on the returned object to stream messages and")
  290. println("/// to close the connection and wait for a final response.")
  291. println("\(access) func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  292. indent()
  293. println("return try \(callName)Base(channel)")
  294. indent()
  295. println(".start(metadata: customMetadata, completion: completion)")
  296. outdent()
  297. outdent()
  298. println("}")
  299. case .bidirectionalStreaming:
  300. println("/// Asynchronous. Bidirectional-streaming.")
  301. println("/// Use methods on the returned object to stream messages,")
  302. println("/// to wait for replies, and to close the connection.")
  303. println("\(access) func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  304. indent()
  305. println("return try \(callName)Base(channel)")
  306. indent()
  307. println(".start(metadata: customMetadata, completion: completion)")
  308. outdent()
  309. outdent()
  310. println("}")
  311. }
  312. println()
  313. }
  314. outdent()
  315. println("}")
  316. }
  317. private func printServiceClientTestStubs() {
  318. println("class \(serviceClassName)TestStub: ServiceClientTestStubBase, \(serviceClassName) {")
  319. indent()
  320. for method in service.methods {
  321. self.method = method
  322. switch streamingType(method) {
  323. case .unary:
  324. println("var \(methodFunctionName)Requests: [\(methodInputName)] = []")
  325. println("var \(methodFunctionName)Responses: [\(methodOutputName)] = []")
  326. println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName) {")
  327. indent()
  328. println("\(methodFunctionName)Requests.append(request)")
  329. println("defer { \(methodFunctionName)Responses.removeFirst() }")
  330. println("return \(methodFunctionName)Responses.first!")
  331. outdent()
  332. println("}")
  333. println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
  334. indent()
  335. println("fatalError(\"not implemented\")")
  336. outdent()
  337. println("}")
  338. case .serverStreaming:
  339. println("var \(methodFunctionName)Requests: [\(methodInputName)] = []")
  340. println("var \(methodFunctionName)Calls: [\(callName)] = []")
  341. println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  342. indent()
  343. println("\(methodFunctionName)Requests.append(request)")
  344. println("defer { \(methodFunctionName)Calls.removeFirst() }")
  345. println("return \(methodFunctionName)Calls.first!")
  346. outdent()
  347. println("}")
  348. case .clientStreaming:
  349. println("var \(methodFunctionName)Calls: [\(callName)] = []")
  350. println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  351. indent()
  352. println("defer { \(methodFunctionName)Calls.removeFirst() }")
  353. println("return \(methodFunctionName)Calls.first!")
  354. outdent()
  355. println("}")
  356. case .bidirectionalStreaming:
  357. println("var \(methodFunctionName)Calls: [\(callName)] = []")
  358. println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  359. indent()
  360. println("defer { \(methodFunctionName)Calls.removeFirst() }")
  361. println("return \(methodFunctionName)Calls.first!")
  362. outdent()
  363. println("}")
  364. }
  365. println()
  366. }
  367. outdent()
  368. println("}")
  369. }
  370. private func printNIOGRPCClient() {
  371. println()
  372. printNIOServiceClientProtocol()
  373. println()
  374. printNIOServiceClientImplementation()
  375. }
  376. private func printNIOServiceClientProtocol() {
  377. println("/// Usage: instantiate \(serviceClassName)Client, then call methods of this protocol to make API calls.")
  378. println("\(options.visibility.sourceSnippet) protocol \(serviceClassName) {")
  379. indent()
  380. for method in service.methods {
  381. self.method = method
  382. switch streamingType(method) {
  383. case .unary:
  384. println("func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions?) -> UnaryClientCall<\(methodInputName), \(methodOutputName)>")
  385. case .serverStreaming:
  386. println("func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions?, handler: @escaping (\(methodOutputName)) -> Void) -> ServerStreamingClientCall<\(methodInputName), \(methodOutputName)>")
  387. case .clientStreaming:
  388. println("func \(methodFunctionName)(callOptions: CallOptions?) -> ClientStreamingClientCall<\(methodInputName), \(methodOutputName)>")
  389. case .bidirectionalStreaming:
  390. println("func \(methodFunctionName)(callOptions: CallOptions?, handler: @escaping (\(methodOutputName)) -> Void) -> BidirectionalStreamingClientCall<\(methodInputName), \(methodOutputName)>")
  391. }
  392. }
  393. outdent()
  394. println("}")
  395. }
  396. private func printNIOServiceClientImplementation() {
  397. println("\(access) final class \(serviceClassName)Client: GRPCServiceClient, \(serviceClassName) {")
  398. indent()
  399. println("\(access) let client: GRPCClient")
  400. println("\(access) let service = \"\(servicePath)\"")
  401. println("\(access) var defaultCallOptions: CallOptions")
  402. println()
  403. println("/// Creates a client for the \(servicePath) service.")
  404. println("///")
  405. printParameters()
  406. println("/// - client: `GRPCClient` with a connection to the service host.")
  407. println("/// - defaultCallOptions: Options to use for each service call if the user doesn't provide them. Defaults to `client.defaultCallOptions`.")
  408. println("\(access) init(client: GRPCClient, defaultCallOptions: CallOptions? = nil) {")
  409. indent()
  410. println("self.client = client")
  411. println("self.defaultCallOptions = defaultCallOptions ?? client.defaultCallOptions")
  412. outdent()
  413. println("}")
  414. println()
  415. for method in service.methods {
  416. self.method = method
  417. switch streamingType(method) {
  418. case .unary:
  419. println("/// Asynchronous unary call to \(method.name).")
  420. println("///")
  421. printParameters()
  422. printRequestParameter()
  423. printCallOptionsParameter()
  424. println("/// - Returns: A `UnaryClientCall` with futures for the metadata, status and response.")
  425. println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions? = nil) -> UnaryClientCall<\(methodInputName), \(methodOutputName)> {")
  426. indent()
  427. println("return UnaryClientCall(client: client, path: path(forMethod: \"\(method.name)\"), request: request, callOptions: callOptions ?? self.defaultCallOptions)")
  428. outdent()
  429. println("}")
  430. case .serverStreaming:
  431. println("/// Asynchronous server-streaming call to \(method.name).")
  432. println("///")
  433. printParameters()
  434. printRequestParameter()
  435. printCallOptionsParameter()
  436. printHandlerParameter()
  437. println("/// - Returns: A `ServerStreamingClientCall` with futures for the metadata and status.")
  438. println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions? = nil, handler: @escaping (\(methodOutputName)) -> Void) -> ServerStreamingClientCall<\(methodInputName), \(methodOutputName)> {")
  439. indent()
  440. println("return ServerStreamingClientCall(client: client, path: path(forMethod: \"\(method.name)\"), request: request, callOptions: callOptions ?? self.defaultCallOptions, handler: handler)")
  441. outdent()
  442. println("}")
  443. case .clientStreaming:
  444. println("/// Asynchronous client-streaming call to \(method.name).")
  445. println("///")
  446. printClientStreamingDetails()
  447. println("///")
  448. printParameters()
  449. printCallOptionsParameter()
  450. println("/// - Returns: A `ClientStreamingClientCall` with futures for the metadata, status and response.")
  451. println("\(access) func \(methodFunctionName)(callOptions: CallOptions? = nil) -> ClientStreamingClientCall<\(methodInputName), \(methodOutputName)> {")
  452. indent()
  453. println("return ClientStreamingClientCall(client: client, path: path(forMethod: \"\(method.name)\"), callOptions: callOptions ?? self.defaultCallOptions)")
  454. outdent()
  455. println("}")
  456. case .bidirectionalStreaming:
  457. println("/// Asynchronous bidirectional-streaming call to \(method.name).")
  458. println("///")
  459. printClientStreamingDetails()
  460. println("///")
  461. printParameters()
  462. printCallOptionsParameter()
  463. printHandlerParameter()
  464. println("/// - Returns: A `ClientStreamingClientCall` with futures for the metadata and status.")
  465. println("\(access) func \(methodFunctionName)(callOptions: CallOptions? = nil, handler: @escaping (\(methodOutputName)) -> Void) -> BidirectionalStreamingClientCall<\(methodInputName), \(methodOutputName)> {")
  466. indent()
  467. println("return BidirectionalStreamingClientCall(client: client, path: path(forMethod: \"\(method.name)\"), callOptions: callOptions ?? self.defaultCallOptions, handler: handler)")
  468. outdent()
  469. println("}")
  470. }
  471. println()
  472. }
  473. outdent()
  474. println("}")
  475. }
  476. private func printClientStreamingDetails() {
  477. println("/// Callers should use the `send` method on the returned object to send messages")
  478. println("/// to the server. The caller should send an `.end` after the final message has been sent.")
  479. }
  480. private func printParameters() {
  481. println("/// - Parameters:")
  482. }
  483. private func printRequestParameter() {
  484. println("/// - request: Request to send to \(method.name).")
  485. }
  486. private func printCallOptionsParameter() {
  487. println("/// - callOptions: Call options; `self.defaultCallOptions` is used if `nil`.")
  488. }
  489. private func printHandlerParameter() {
  490. println("/// - handler: A closure called when each response is received from the server.")
  491. }
  492. }