2
0

Generator-Client.swift 23 KB

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