Generator-Client.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName)")
  180. }
  181. case .serverStreaming:
  182. println("/// Asynchronous. Server-streaming.")
  183. println("/// Send the initial message.")
  184. println("/// Use methods on the returned object to get streamed responses.")
  185. println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName)")
  186. case .clientStreaming:
  187. println("/// Asynchronous. Client-streaming.")
  188. println("/// Use methods on the returned object to stream messages and")
  189. println("/// to close the connection and wait for a final response.")
  190. println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName)")
  191. case .bidirectionalStreaming:
  192. println("/// Asynchronous. Bidirectional-streaming.")
  193. println("/// Use methods on the returned object to stream messages,")
  194. println("/// to wait for replies, and to close the connection.")
  195. println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName)")
  196. }
  197. println()
  198. }
  199. outdent()
  200. println("}")
  201. }
  202. private func printServiceClientProtocolExtension(asynchronousCode: Bool,
  203. synchronousCode: Bool) {
  204. println("\(options.visibility.sourceSnippet) extension \(serviceClassName) {")
  205. indent()
  206. for method in service.methods {
  207. self.method = method
  208. switch streamingType(method) {
  209. case .unary:
  210. if synchronousCode {
  211. println("/// Synchronous. Unary.")
  212. println("func \(methodFunctionName)(_ request: \(methodInputName)) throws -> \(methodOutputName) {")
  213. indent()
  214. println("return try self.\(methodFunctionName)(request, metadata: self.metadata)")
  215. outdent()
  216. println("}")
  217. }
  218. if asynchronousCode {
  219. println("/// Asynchronous. Unary.")
  220. println("func \(methodFunctionName)(_ request: \(methodInputName), completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
  221. indent()
  222. println("return try self.\(methodFunctionName)(request, metadata: self.metadata, completion: completion)")
  223. outdent()
  224. println("}")
  225. }
  226. case .serverStreaming:
  227. println("/// Asynchronous. Server-streaming.")
  228. println("func \(methodFunctionName)(_ request: \(methodInputName), completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  229. indent()
  230. println("return try self.\(methodFunctionName)(request, metadata: self.metadata, completion: completion)")
  231. outdent()
  232. println("}")
  233. case .clientStreaming:
  234. println("/// Asynchronous. Client-streaming.")
  235. println("func \(methodFunctionName)(completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  236. indent()
  237. println("return try self.\(methodFunctionName)(metadata: self.metadata, completion: completion)")
  238. outdent()
  239. println("}")
  240. case .bidirectionalStreaming:
  241. println("/// Asynchronous. Bidirectional-streaming.")
  242. println("func \(methodFunctionName)(completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  243. indent()
  244. println("return try self.\(methodFunctionName)(metadata: self.metadata, completion: completion)")
  245. outdent()
  246. println("}")
  247. }
  248. println()
  249. }
  250. outdent()
  251. println("}")
  252. }
  253. private func printServiceClientImplementation(asynchronousCode: Bool,
  254. synchronousCode: Bool) {
  255. println("\(access) final class \(serviceClassName)Client: ServiceClientBase, \(serviceClassName) {")
  256. indent()
  257. for method in service.methods {
  258. self.method = method
  259. switch streamingType(method) {
  260. case .unary:
  261. if synchronousCode {
  262. println("/// Synchronous. Unary.")
  263. println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName) {")
  264. indent()
  265. println("return try \(callName)Base(channel)")
  266. indent()
  267. println(".run(request: request, metadata: customMetadata)")
  268. outdent()
  269. outdent()
  270. println("}")
  271. }
  272. if asynchronousCode {
  273. println("/// Asynchronous. Unary.")
  274. println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
  275. indent()
  276. println("return try \(callName)Base(channel)")
  277. indent()
  278. println(".start(request: request, metadata: customMetadata, completion: completion)")
  279. outdent()
  280. outdent()
  281. println("}")
  282. }
  283. case .serverStreaming:
  284. println("/// Asynchronous. Server-streaming.")
  285. println("/// Send the initial message.")
  286. println("/// Use methods on the returned object to get streamed responses.")
  287. println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  288. indent()
  289. println("return try \(callName)Base(channel)")
  290. indent()
  291. println(".start(request: request, metadata: customMetadata, completion: completion)")
  292. outdent()
  293. outdent()
  294. println("}")
  295. case .clientStreaming:
  296. println("/// Asynchronous. Client-streaming.")
  297. println("/// Use methods on the returned object to stream messages and")
  298. println("/// to close the connection and wait for a final response.")
  299. println("\(access) func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  300. indent()
  301. println("return try \(callName)Base(channel)")
  302. indent()
  303. println(".start(metadata: customMetadata, completion: completion)")
  304. outdent()
  305. outdent()
  306. println("}")
  307. case .bidirectionalStreaming:
  308. println("/// Asynchronous. Bidirectional-streaming.")
  309. println("/// Use methods on the returned object to stream messages,")
  310. println("/// to wait for replies, and to close the connection.")
  311. println("\(access) func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  312. indent()
  313. println("return try \(callName)Base(channel)")
  314. indent()
  315. println(".start(metadata: customMetadata, completion: completion)")
  316. outdent()
  317. outdent()
  318. println("}")
  319. }
  320. println()
  321. }
  322. outdent()
  323. println("}")
  324. }
  325. private func printServiceClientTestStubs() {
  326. println("class \(serviceClassName)TestStub: ServiceClientTestStubBase, \(serviceClassName) {")
  327. indent()
  328. for method in service.methods {
  329. self.method = method
  330. switch streamingType(method) {
  331. case .unary:
  332. println("var \(methodFunctionName)Requests: [\(methodInputName)] = []")
  333. println("var \(methodFunctionName)Responses: [\(methodOutputName)] = []")
  334. println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata) throws -> \(methodOutputName) {")
  335. indent()
  336. println("\(methodFunctionName)Requests.append(request)")
  337. println("defer { \(methodFunctionName)Responses.removeFirst() }")
  338. println("return \(methodFunctionName)Responses.first!")
  339. outdent()
  340. println("}")
  341. println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: @escaping (\(methodOutputName)?, CallResult) -> Void) throws -> \(callName) {")
  342. indent()
  343. println("fatalError(\"not implemented\")")
  344. outdent()
  345. println("}")
  346. case .serverStreaming:
  347. println("var \(methodFunctionName)Requests: [\(methodInputName)] = []")
  348. println("var \(methodFunctionName)Calls: [\(callName)] = []")
  349. println("func \(methodFunctionName)(_ request: \(methodInputName), metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  350. indent()
  351. println("\(methodFunctionName)Requests.append(request)")
  352. println("defer { \(methodFunctionName)Calls.removeFirst() }")
  353. println("return \(methodFunctionName)Calls.first!")
  354. outdent()
  355. println("}")
  356. case .clientStreaming:
  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. case .bidirectionalStreaming:
  365. println("var \(methodFunctionName)Calls: [\(callName)] = []")
  366. println("func \(methodFunctionName)(metadata customMetadata: Metadata, completion: ((CallResult) -> Void)?) throws -> \(callName) {")
  367. indent()
  368. println("defer { \(methodFunctionName)Calls.removeFirst() }")
  369. println("return \(methodFunctionName)Calls.first!")
  370. outdent()
  371. println("}")
  372. }
  373. println()
  374. }
  375. outdent()
  376. println("}")
  377. }
  378. private func printNIOGRPCClient() {
  379. println()
  380. printNIOServiceClientProtocol()
  381. println()
  382. printNIOServiceClientImplementation()
  383. }
  384. private func printNIOServiceClientProtocol() {
  385. println("/// Usage: instantiate \(serviceClassName)Client, then call methods of this protocol to make API calls.")
  386. println("\(options.visibility.sourceSnippet) protocol \(serviceClassName) {")
  387. indent()
  388. for method in service.methods {
  389. self.method = method
  390. switch streamingType(method) {
  391. case .unary:
  392. println("func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions?) -> UnaryClientCall<\(methodInputName), \(methodOutputName)>")
  393. case .serverStreaming:
  394. println("func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions?, handler: @escaping (\(methodOutputName)) -> Void) -> ServerStreamingClientCall<\(methodInputName), \(methodOutputName)>")
  395. case .clientStreaming:
  396. println("func \(methodFunctionName)(callOptions: CallOptions?) -> ClientStreamingClientCall<\(methodInputName), \(methodOutputName)>")
  397. case .bidirectionalStreaming:
  398. println("func \(methodFunctionName)(callOptions: CallOptions?, handler: @escaping (\(methodOutputName)) -> Void) -> BidirectionalStreamingClientCall<\(methodInputName), \(methodOutputName)>")
  399. }
  400. }
  401. outdent()
  402. println("}")
  403. }
  404. private func printNIOServiceClientImplementation() {
  405. println("\(access) final class \(serviceClassName)Client: GRPCServiceClient, \(serviceClassName) {")
  406. indent()
  407. println("\(access) let client: GRPCClient")
  408. println("\(access) let service = \"\(servicePath)\"")
  409. println("\(access) var defaultCallOptions: CallOptions")
  410. println()
  411. println("/// Creates a client for the \(servicePath) service.")
  412. println("///")
  413. printParameters()
  414. println("/// - client: `GRPCClient` with a connection to the service host.")
  415. println("/// - defaultCallOptions: Options to use for each service call if the user doesn't provide them. Defaults to `client.defaultCallOptions`.")
  416. println("\(access) init(client: GRPCClient, defaultCallOptions: CallOptions? = nil) {")
  417. indent()
  418. println("self.client = client")
  419. println("self.defaultCallOptions = defaultCallOptions ?? client.defaultCallOptions")
  420. outdent()
  421. println("}")
  422. println()
  423. for method in service.methods {
  424. self.method = method
  425. switch streamingType(method) {
  426. case .unary:
  427. println("/// Asynchronous unary call to \(method.name).")
  428. println("///")
  429. printParameters()
  430. printRequestParameter()
  431. printCallOptionsParameter()
  432. println("/// - Returns: A `UnaryClientCall` with futures for the metadata, status and response.")
  433. println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions? = nil) -> UnaryClientCall<\(methodInputName), \(methodOutputName)> {")
  434. indent()
  435. println("return UnaryClientCall(client: client, path: path(forMethod: \"\(method.name)\"), request: request, callOptions: callOptions ?? self.defaultCallOptions)")
  436. outdent()
  437. println("}")
  438. case .serverStreaming:
  439. println("/// Asynchronous server-streaming call to \(method.name).")
  440. println("///")
  441. printParameters()
  442. printRequestParameter()
  443. printCallOptionsParameter()
  444. printHandlerParameter()
  445. println("/// - Returns: A `ServerStreamingClientCall` with futures for the metadata and status.")
  446. println("\(access) func \(methodFunctionName)(_ request: \(methodInputName), callOptions: CallOptions? = nil, handler: @escaping (\(methodOutputName)) -> Void) -> ServerStreamingClientCall<\(methodInputName), \(methodOutputName)> {")
  447. indent()
  448. println("return ServerStreamingClientCall(client: client, path: path(forMethod: \"\(method.name)\"), request: request, callOptions: callOptions ?? self.defaultCallOptions, handler: handler)")
  449. outdent()
  450. println("}")
  451. case .clientStreaming:
  452. println("/// Asynchronous client-streaming call to \(method.name).")
  453. println("///")
  454. printClientStreamingDetails()
  455. println("///")
  456. printParameters()
  457. printCallOptionsParameter()
  458. println("/// - Returns: A `ClientStreamingClientCall` with futures for the metadata, status and response.")
  459. println("\(access) func \(methodFunctionName)(callOptions: CallOptions? = nil) -> ClientStreamingClientCall<\(methodInputName), \(methodOutputName)> {")
  460. indent()
  461. println("return ClientStreamingClientCall(client: client, path: path(forMethod: \"\(method.name)\"), callOptions: callOptions ?? self.defaultCallOptions)")
  462. outdent()
  463. println("}")
  464. case .bidirectionalStreaming:
  465. println("/// Asynchronous bidirectional-streaming call to \(method.name).")
  466. println("///")
  467. printClientStreamingDetails()
  468. println("///")
  469. printParameters()
  470. printCallOptionsParameter()
  471. printHandlerParameter()
  472. println("/// - Returns: A `ClientStreamingClientCall` with futures for the metadata and status.")
  473. println("\(access) func \(methodFunctionName)(callOptions: CallOptions? = nil, handler: @escaping (\(methodOutputName)) -> Void) -> BidirectionalStreamingClientCall<\(methodInputName), \(methodOutputName)> {")
  474. indent()
  475. println("return BidirectionalStreamingClientCall(client: client, path: path(forMethod: \"\(method.name)\"), callOptions: callOptions ?? self.defaultCallOptions, handler: handler)")
  476. outdent()
  477. println("}")
  478. }
  479. println()
  480. }
  481. outdent()
  482. println("}")
  483. }
  484. private func printClientStreamingDetails() {
  485. println("/// Callers should use the `send` method on the returned object to send messages")
  486. println("/// to the server. The caller should send an `.end` after the final message has been sent.")
  487. }
  488. private func printParameters() {
  489. println("/// - Parameters:")
  490. }
  491. private func printRequestParameter() {
  492. println("/// - request: Request to send to \(method.name).")
  493. }
  494. private func printCallOptionsParameter() {
  495. println("/// - callOptions: Call options; `self.defaultCallOptions` is used if `nil`.")
  496. }
  497. private func printHandlerParameter() {
  498. println("/// - handler: A closure called when each response is received from the server.")
  499. }
  500. }