ProtobufCodeGeneratorTests.swift 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. /*
  2. * Copyright 2024, 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 GRPCCodeGen
  17. import GRPCProtobufCodeGen
  18. import SwiftProtobufPluginLibrary
  19. import Testing
  20. struct ProtobufCodeGeneratorTests: UsesDescriptorSet {
  21. static let descriptorSetName = "test-service"
  22. static let fileDescriptorName = "test-service"
  23. @Test("Generate", arguments: [SourceGenerator.Config.AccessLevel.internal, .public, .package])
  24. func generate(accessLevel: SourceGenerator.Config.AccessLevel) throws {
  25. let generator = ProtobufCodeGenerator(
  26. config: SourceGenerator.Config(
  27. accessLevel: accessLevel,
  28. accessLevelOnImports: false,
  29. client: true,
  30. server: true,
  31. indentation: 2
  32. )
  33. )
  34. let access: String
  35. switch accessLevel {
  36. case .public:
  37. access = "public"
  38. case .internal:
  39. access = "internal"
  40. case .package:
  41. access = "package"
  42. default:
  43. fatalError()
  44. }
  45. let generated = try generator.generateCode(
  46. fileDescriptor: Self.fileDescriptor,
  47. protoFileModuleMappings: ProtoFileToModuleMappings(),
  48. extraModuleImports: []
  49. )
  50. let expected = """
  51. /// Leading trivia.
  52. // DO NOT EDIT.
  53. // swift-format-ignore-file
  54. //
  55. // Generated by the gRPC Swift generator plugin for the protocol buffer compiler.
  56. // Source: test-service.proto
  57. //
  58. // For information on using the generated types, please see the documentation:
  59. // https://github.com/grpc/grpc-swift
  60. import GRPCCore
  61. import GRPCProtobuf
  62. import SwiftProtobuf
  63. // MARK: - test.TestService
  64. /// Namespace containing generated types for the "test.TestService" service.
  65. \(access) enum Test_TestService {
  66. /// Service descriptor for the "test.TestService" service.
  67. \(access) static let descriptor = GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService")
  68. /// Namespace for method metadata.
  69. \(access) enum Method {
  70. /// Namespace for "Unary" metadata.
  71. \(access) enum Unary {
  72. /// Request type for "Unary".
  73. \(access) typealias Input = Test_TestInput
  74. /// Response type for "Unary".
  75. \(access) typealias Output = Test_TestOutput
  76. /// Descriptor for "Unary".
  77. \(access) static let descriptor = GRPCCore.MethodDescriptor(
  78. service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService"),
  79. method: "Unary"
  80. )
  81. }
  82. /// Namespace for "ClientStreaming" metadata.
  83. \(access) enum ClientStreaming {
  84. /// Request type for "ClientStreaming".
  85. \(access) typealias Input = Test_TestInput
  86. /// Response type for "ClientStreaming".
  87. \(access) typealias Output = Test_TestOutput
  88. /// Descriptor for "ClientStreaming".
  89. \(access) static let descriptor = GRPCCore.MethodDescriptor(
  90. service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService"),
  91. method: "ClientStreaming"
  92. )
  93. }
  94. /// Namespace for "ServerStreaming" metadata.
  95. \(access) enum ServerStreaming {
  96. /// Request type for "ServerStreaming".
  97. \(access) typealias Input = Test_TestInput
  98. /// Response type for "ServerStreaming".
  99. \(access) typealias Output = Test_TestOutput
  100. /// Descriptor for "ServerStreaming".
  101. \(access) static let descriptor = GRPCCore.MethodDescriptor(
  102. service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService"),
  103. method: "ServerStreaming"
  104. )
  105. }
  106. /// Namespace for "BidirectionalStreaming" metadata.
  107. \(access) enum BidirectionalStreaming {
  108. /// Request type for "BidirectionalStreaming".
  109. \(access) typealias Input = Test_TestInput
  110. /// Response type for "BidirectionalStreaming".
  111. \(access) typealias Output = Test_TestOutput
  112. /// Descriptor for "BidirectionalStreaming".
  113. \(access) static let descriptor = GRPCCore.MethodDescriptor(
  114. service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService"),
  115. method: "BidirectionalStreaming"
  116. )
  117. }
  118. /// Descriptors for all methods in the "test.TestService" service.
  119. \(access) static let descriptors: [GRPCCore.MethodDescriptor] = [
  120. Unary.descriptor,
  121. ClientStreaming.descriptor,
  122. ServerStreaming.descriptor,
  123. BidirectionalStreaming.descriptor
  124. ]
  125. }
  126. }
  127. extension GRPCCore.ServiceDescriptor {
  128. /// Service descriptor for the "test.TestService" service.
  129. \(access) static let test_TestService = GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService")
  130. }
  131. // MARK: test.TestService (server)
  132. extension Test_TestService {
  133. /// Streaming variant of the service protocol for the "test.TestService" service.
  134. ///
  135. /// This protocol is the lowest-level of the service protocols generated for this service
  136. /// giving you the most flexibility over the implementation of your service. This comes at
  137. /// the cost of more verbose and less strict APIs. Each RPC requires you to implement it in
  138. /// terms of a request stream and response stream. Where only a single request or response
  139. /// message is expected, you are responsible for enforcing this invariant is maintained.
  140. ///
  141. /// Where possible, prefer using the stricter, less-verbose ``ServiceProtocol``
  142. /// or ``SimpleServiceProtocol`` instead.
  143. ///
  144. /// > Source IDL Documentation:
  145. /// >
  146. /// > Service docs.
  147. \(access) protocol StreamingServiceProtocol: GRPCCore.RegistrableRPCService {
  148. /// Handle the "Unary" method.
  149. ///
  150. /// > Source IDL Documentation:
  151. /// >
  152. /// > Unary docs.
  153. ///
  154. /// - Parameters:
  155. /// - request: A streaming request of `Test_TestInput` messages.
  156. /// - context: Context providing information about the RPC.
  157. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  158. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  159. /// to an internal error.
  160. /// - Returns: A streaming response of `Test_TestOutput` messages.
  161. func unary(
  162. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  163. context: GRPCCore.ServerContext
  164. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
  165. /// Handle the "ClientStreaming" method.
  166. ///
  167. /// > Source IDL Documentation:
  168. /// >
  169. /// > Client streaming docs.
  170. ///
  171. /// - Parameters:
  172. /// - request: A streaming request of `Test_TestInput` messages.
  173. /// - context: Context providing information about the RPC.
  174. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  175. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  176. /// to an internal error.
  177. /// - Returns: A streaming response of `Test_TestOutput` messages.
  178. func clientStreaming(
  179. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  180. context: GRPCCore.ServerContext
  181. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
  182. /// Handle the "ServerStreaming" method.
  183. ///
  184. /// > Source IDL Documentation:
  185. /// >
  186. /// > Server streaming docs.
  187. ///
  188. /// - Parameters:
  189. /// - request: A streaming request of `Test_TestInput` messages.
  190. /// - context: Context providing information about the RPC.
  191. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  192. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  193. /// to an internal error.
  194. /// - Returns: A streaming response of `Test_TestOutput` messages.
  195. func serverStreaming(
  196. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  197. context: GRPCCore.ServerContext
  198. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
  199. /// Handle the "BidirectionalStreaming" method.
  200. ///
  201. /// > Source IDL Documentation:
  202. /// >
  203. /// > Bidirectional streaming docs.
  204. ///
  205. /// - Parameters:
  206. /// - request: A streaming request of `Test_TestInput` messages.
  207. /// - context: Context providing information about the RPC.
  208. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  209. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  210. /// to an internal error.
  211. /// - Returns: A streaming response of `Test_TestOutput` messages.
  212. func bidirectionalStreaming(
  213. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  214. context: GRPCCore.ServerContext
  215. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
  216. }
  217. /// Service protocol for the "test.TestService" service.
  218. ///
  219. /// This protocol is higher level than ``StreamingServiceProtocol`` but lower level than
  220. /// the ``SimpleServiceProtocol``, it provides access to request and response metadata and
  221. /// trailing response metadata. If you don't need these then consider using
  222. /// the ``SimpleServiceProtocol``. If you need fine grained control over your RPCs then
  223. /// use ``StreamingServiceProtocol``.
  224. ///
  225. /// > Source IDL Documentation:
  226. /// >
  227. /// > Service docs.
  228. \(access) protocol ServiceProtocol: Test_TestService.StreamingServiceProtocol {
  229. /// Handle the "Unary" method.
  230. ///
  231. /// > Source IDL Documentation:
  232. /// >
  233. /// > Unary docs.
  234. ///
  235. /// - Parameters:
  236. /// - request: A request containing a single `Test_TestInput` message.
  237. /// - context: Context providing information about the RPC.
  238. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  239. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  240. /// to an internal error.
  241. /// - Returns: A response containing a single `Test_TestOutput` message.
  242. func unary(
  243. request: GRPCCore.ServerRequest<Test_TestInput>,
  244. context: GRPCCore.ServerContext
  245. ) async throws -> GRPCCore.ServerResponse<Test_TestOutput>
  246. /// Handle the "ClientStreaming" method.
  247. ///
  248. /// > Source IDL Documentation:
  249. /// >
  250. /// > Client streaming docs.
  251. ///
  252. /// - Parameters:
  253. /// - request: A streaming request of `Test_TestInput` messages.
  254. /// - context: Context providing information about the RPC.
  255. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  256. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  257. /// to an internal error.
  258. /// - Returns: A response containing a single `Test_TestOutput` message.
  259. func clientStreaming(
  260. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  261. context: GRPCCore.ServerContext
  262. ) async throws -> GRPCCore.ServerResponse<Test_TestOutput>
  263. /// Handle the "ServerStreaming" method.
  264. ///
  265. /// > Source IDL Documentation:
  266. /// >
  267. /// > Server streaming docs.
  268. ///
  269. /// - Parameters:
  270. /// - request: A request containing a single `Test_TestInput` message.
  271. /// - context: Context providing information about the RPC.
  272. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  273. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  274. /// to an internal error.
  275. /// - Returns: A streaming response of `Test_TestOutput` messages.
  276. func serverStreaming(
  277. request: GRPCCore.ServerRequest<Test_TestInput>,
  278. context: GRPCCore.ServerContext
  279. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
  280. /// Handle the "BidirectionalStreaming" method.
  281. ///
  282. /// > Source IDL Documentation:
  283. /// >
  284. /// > Bidirectional streaming docs.
  285. ///
  286. /// - Parameters:
  287. /// - request: A streaming request of `Test_TestInput` messages.
  288. /// - context: Context providing information about the RPC.
  289. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  290. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  291. /// to an internal error.
  292. /// - Returns: A streaming response of `Test_TestOutput` messages.
  293. func bidirectionalStreaming(
  294. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  295. context: GRPCCore.ServerContext
  296. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
  297. }
  298. /// Simple service protocol for the "test.TestService" service.
  299. ///
  300. /// This is the highest level protocol for the service. The API is the easiest to use but
  301. /// doesn't provide access to request or response metadata. If you need access to these
  302. /// then use ``ServiceProtocol`` instead.
  303. ///
  304. /// > Source IDL Documentation:
  305. /// >
  306. /// > Service docs.
  307. \(access) protocol SimpleServiceProtocol: Test_TestService.ServiceProtocol {
  308. /// Handle the "Unary" method.
  309. ///
  310. /// > Source IDL Documentation:
  311. /// >
  312. /// > Unary docs.
  313. ///
  314. /// - Parameters:
  315. /// - request: A `Test_TestInput` message.
  316. /// - context: Context providing information about the RPC.
  317. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  318. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  319. /// to an internal error.
  320. /// - Returns: A `Test_TestOutput` to respond with.
  321. func unary(
  322. request: Test_TestInput,
  323. context: GRPCCore.ServerContext
  324. ) async throws -> Test_TestOutput
  325. /// Handle the "ClientStreaming" method.
  326. ///
  327. /// > Source IDL Documentation:
  328. /// >
  329. /// > Client streaming docs.
  330. ///
  331. /// - Parameters:
  332. /// - request: A stream of `Test_TestInput` messages.
  333. /// - context: Context providing information about the RPC.
  334. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  335. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  336. /// to an internal error.
  337. /// - Returns: A `Test_TestOutput` to respond with.
  338. func clientStreaming(
  339. request: GRPCCore.RPCAsyncSequence<Test_TestInput, any Swift.Error>,
  340. context: GRPCCore.ServerContext
  341. ) async throws -> Test_TestOutput
  342. /// Handle the "ServerStreaming" method.
  343. ///
  344. /// > Source IDL Documentation:
  345. /// >
  346. /// > Server streaming docs.
  347. ///
  348. /// - Parameters:
  349. /// - request: A `Test_TestInput` message.
  350. /// - response: A response stream of `Test_TestOutput` messages.
  351. /// - context: Context providing information about the RPC.
  352. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  353. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  354. /// to an internal error.
  355. func serverStreaming(
  356. request: Test_TestInput,
  357. response: GRPCCore.RPCWriter<Test_TestOutput>,
  358. context: GRPCCore.ServerContext
  359. ) async throws
  360. /// Handle the "BidirectionalStreaming" method.
  361. ///
  362. /// > Source IDL Documentation:
  363. /// >
  364. /// > Bidirectional streaming docs.
  365. ///
  366. /// - Parameters:
  367. /// - request: A stream of `Test_TestInput` messages.
  368. /// - response: A response stream of `Test_TestOutput` messages.
  369. /// - context: Context providing information about the RPC.
  370. /// - Throws: Any error which occurred during the processing of the request. Thrown errors
  371. /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
  372. /// to an internal error.
  373. func bidirectionalStreaming(
  374. request: GRPCCore.RPCAsyncSequence<Test_TestInput, any Swift.Error>,
  375. response: GRPCCore.RPCWriter<Test_TestOutput>,
  376. context: GRPCCore.ServerContext
  377. ) async throws
  378. }
  379. }
  380. // Default implementation of 'registerMethods(with:)'.
  381. extension Test_TestService.StreamingServiceProtocol {
  382. \(access) func registerMethods(with router: inout GRPCCore.RPCRouter) {
  383. router.registerHandler(
  384. forMethod: Test_TestService.Method.Unary.descriptor,
  385. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestInput>(),
  386. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestOutput>(),
  387. handler: { request, context in
  388. try await self.unary(
  389. request: request,
  390. context: context
  391. )
  392. }
  393. )
  394. router.registerHandler(
  395. forMethod: Test_TestService.Method.ClientStreaming.descriptor,
  396. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestInput>(),
  397. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestOutput>(),
  398. handler: { request, context in
  399. try await self.clientStreaming(
  400. request: request,
  401. context: context
  402. )
  403. }
  404. )
  405. router.registerHandler(
  406. forMethod: Test_TestService.Method.ServerStreaming.descriptor,
  407. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestInput>(),
  408. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestOutput>(),
  409. handler: { request, context in
  410. try await self.serverStreaming(
  411. request: request,
  412. context: context
  413. )
  414. }
  415. )
  416. router.registerHandler(
  417. forMethod: Test_TestService.Method.BidirectionalStreaming.descriptor,
  418. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestInput>(),
  419. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestOutput>(),
  420. handler: { request, context in
  421. try await self.bidirectionalStreaming(
  422. request: request,
  423. context: context
  424. )
  425. }
  426. )
  427. }
  428. }
  429. // Default implementation of streaming methods from 'StreamingServiceProtocol'.
  430. extension Test_TestService.ServiceProtocol {
  431. \(access) func unary(
  432. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  433. context: GRPCCore.ServerContext
  434. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
  435. let response = try await self.unary(
  436. request: GRPCCore.ServerRequest(stream: request),
  437. context: context
  438. )
  439. return GRPCCore.StreamingServerResponse(single: response)
  440. }
  441. \(access) func clientStreaming(
  442. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  443. context: GRPCCore.ServerContext
  444. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
  445. let response = try await self.clientStreaming(
  446. request: request,
  447. context: context
  448. )
  449. return GRPCCore.StreamingServerResponse(single: response)
  450. }
  451. \(access) func serverStreaming(
  452. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  453. context: GRPCCore.ServerContext
  454. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
  455. let response = try await self.serverStreaming(
  456. request: GRPCCore.ServerRequest(stream: request),
  457. context: context
  458. )
  459. return response
  460. }
  461. }
  462. // Default implementation of methods from 'ServiceProtocol'.
  463. extension Test_TestService.SimpleServiceProtocol {
  464. \(access) func unary(
  465. request: GRPCCore.ServerRequest<Test_TestInput>,
  466. context: GRPCCore.ServerContext
  467. ) async throws -> GRPCCore.ServerResponse<Test_TestOutput> {
  468. return GRPCCore.ServerResponse<Test_TestOutput>(
  469. message: try await self.unary(
  470. request: request.message,
  471. context: context
  472. ),
  473. metadata: [:]
  474. )
  475. }
  476. \(access) func clientStreaming(
  477. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  478. context: GRPCCore.ServerContext
  479. ) async throws -> GRPCCore.ServerResponse<Test_TestOutput> {
  480. return GRPCCore.ServerResponse<Test_TestOutput>(
  481. message: try await self.clientStreaming(
  482. request: request.messages,
  483. context: context
  484. ),
  485. metadata: [:]
  486. )
  487. }
  488. \(access) func serverStreaming(
  489. request: GRPCCore.ServerRequest<Test_TestInput>,
  490. context: GRPCCore.ServerContext
  491. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
  492. return GRPCCore.StreamingServerResponse<Test_TestOutput>(
  493. metadata: [:],
  494. producer: { writer in
  495. try await self.serverStreaming(
  496. request: request.message,
  497. response: writer,
  498. context: context
  499. )
  500. return [:]
  501. }
  502. )
  503. }
  504. \(access) func bidirectionalStreaming(
  505. request: GRPCCore.StreamingServerRequest<Test_TestInput>,
  506. context: GRPCCore.ServerContext
  507. ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
  508. return GRPCCore.StreamingServerResponse<Test_TestOutput>(
  509. metadata: [:],
  510. producer: { writer in
  511. try await self.bidirectionalStreaming(
  512. request: request.messages,
  513. response: writer,
  514. context: context
  515. )
  516. return [:]
  517. }
  518. )
  519. }
  520. }
  521. // MARK: test.TestService (client)
  522. extension Test_TestService {
  523. /// Generated client protocol for the "test.TestService" service.
  524. ///
  525. /// You don't need to implement this protocol directly, use the generated
  526. /// implementation, ``Client``.
  527. ///
  528. /// > Source IDL Documentation:
  529. /// >
  530. /// > Service docs.
  531. \(access) protocol ClientProtocol: Sendable {
  532. /// Call the "Unary" method.
  533. ///
  534. /// > Source IDL Documentation:
  535. /// >
  536. /// > Unary docs.
  537. ///
  538. /// - Parameters:
  539. /// - request: A request containing a single `Test_TestInput` message.
  540. /// - serializer: A serializer for `Test_TestInput` messages.
  541. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  542. /// - options: Options to apply to this RPC.
  543. /// - handleResponse: A closure which handles the response, the result of which is
  544. /// returned to the caller. Returning from the closure will cancel the RPC if it
  545. /// hasn't already finished.
  546. /// - Returns: The result of `handleResponse`.
  547. func unary<Result>(
  548. request: GRPCCore.ClientRequest<Test_TestInput>,
  549. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  550. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  551. options: GRPCCore.CallOptions,
  552. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result
  553. ) async throws -> Result where Result: Sendable
  554. /// Call the "ClientStreaming" method.
  555. ///
  556. /// > Source IDL Documentation:
  557. /// >
  558. /// > Client streaming docs.
  559. ///
  560. /// - Parameters:
  561. /// - request: A streaming request producing `Test_TestInput` messages.
  562. /// - serializer: A serializer for `Test_TestInput` messages.
  563. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  564. /// - options: Options to apply to this RPC.
  565. /// - handleResponse: A closure which handles the response, the result of which is
  566. /// returned to the caller. Returning from the closure will cancel the RPC if it
  567. /// hasn't already finished.
  568. /// - Returns: The result of `handleResponse`.
  569. func clientStreaming<Result>(
  570. request: GRPCCore.StreamingClientRequest<Test_TestInput>,
  571. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  572. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  573. options: GRPCCore.CallOptions,
  574. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result
  575. ) async throws -> Result where Result: Sendable
  576. /// Call the "ServerStreaming" method.
  577. ///
  578. /// > Source IDL Documentation:
  579. /// >
  580. /// > Server streaming docs.
  581. ///
  582. /// - Parameters:
  583. /// - request: A request containing a single `Test_TestInput` message.
  584. /// - serializer: A serializer for `Test_TestInput` messages.
  585. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  586. /// - options: Options to apply to this RPC.
  587. /// - handleResponse: A closure which handles the response, the result of which is
  588. /// returned to the caller. Returning from the closure will cancel the RPC if it
  589. /// hasn't already finished.
  590. /// - Returns: The result of `handleResponse`.
  591. func serverStreaming<Result>(
  592. request: GRPCCore.ClientRequest<Test_TestInput>,
  593. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  594. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  595. options: GRPCCore.CallOptions,
  596. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  597. ) async throws -> Result where Result: Sendable
  598. /// Call the "BidirectionalStreaming" method.
  599. ///
  600. /// > Source IDL Documentation:
  601. /// >
  602. /// > Bidirectional streaming docs.
  603. ///
  604. /// - Parameters:
  605. /// - request: A streaming request producing `Test_TestInput` messages.
  606. /// - serializer: A serializer for `Test_TestInput` messages.
  607. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  608. /// - options: Options to apply to this RPC.
  609. /// - handleResponse: A closure which handles the response, the result of which is
  610. /// returned to the caller. Returning from the closure will cancel the RPC if it
  611. /// hasn't already finished.
  612. /// - Returns: The result of `handleResponse`.
  613. func bidirectionalStreaming<Result>(
  614. request: GRPCCore.StreamingClientRequest<Test_TestInput>,
  615. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  616. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  617. options: GRPCCore.CallOptions,
  618. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  619. ) async throws -> Result where Result: Sendable
  620. }
  621. /// Generated client for the "test.TestService" service.
  622. ///
  623. /// The ``Client`` provides an implementation of ``ClientProtocol`` which wraps
  624. /// a `GRPCCore.GRPCCClient`. The underlying `GRPCClient` provides the long-lived
  625. /// means of communication with the remote peer.
  626. ///
  627. /// > Source IDL Documentation:
  628. /// >
  629. /// > Service docs.
  630. \(access) struct Client: ClientProtocol {
  631. private let client: GRPCCore.GRPCClient
  632. /// Creates a new client wrapping the provided `GRPCCore.GRPCClient`.
  633. ///
  634. /// - Parameters:
  635. /// - client: A `GRPCCore.GRPCClient` providing a communication channel to the service.
  636. \(access) init(wrapping client: GRPCCore.GRPCClient) {
  637. self.client = client
  638. }
  639. /// Call the "Unary" method.
  640. ///
  641. /// > Source IDL Documentation:
  642. /// >
  643. /// > Unary docs.
  644. ///
  645. /// - Parameters:
  646. /// - request: A request containing a single `Test_TestInput` message.
  647. /// - serializer: A serializer for `Test_TestInput` messages.
  648. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  649. /// - options: Options to apply to this RPC.
  650. /// - handleResponse: A closure which handles the response, the result of which is
  651. /// returned to the caller. Returning from the closure will cancel the RPC if it
  652. /// hasn't already finished.
  653. /// - Returns: The result of `handleResponse`.
  654. \(access) func unary<Result>(
  655. request: GRPCCore.ClientRequest<Test_TestInput>,
  656. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  657. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  658. options: GRPCCore.CallOptions = .defaults,
  659. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
  660. try response.message
  661. }
  662. ) async throws -> Result where Result: Sendable {
  663. try await self.client.unary(
  664. request: request,
  665. descriptor: Test_TestService.Method.Unary.descriptor,
  666. serializer: serializer,
  667. deserializer: deserializer,
  668. options: options,
  669. onResponse: handleResponse
  670. )
  671. }
  672. /// Call the "ClientStreaming" method.
  673. ///
  674. /// > Source IDL Documentation:
  675. /// >
  676. /// > Client streaming docs.
  677. ///
  678. /// - Parameters:
  679. /// - request: A streaming request producing `Test_TestInput` messages.
  680. /// - serializer: A serializer for `Test_TestInput` messages.
  681. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  682. /// - options: Options to apply to this RPC.
  683. /// - handleResponse: A closure which handles the response, the result of which is
  684. /// returned to the caller. Returning from the closure will cancel the RPC if it
  685. /// hasn't already finished.
  686. /// - Returns: The result of `handleResponse`.
  687. \(access) func clientStreaming<Result>(
  688. request: GRPCCore.StreamingClientRequest<Test_TestInput>,
  689. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  690. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  691. options: GRPCCore.CallOptions = .defaults,
  692. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
  693. try response.message
  694. }
  695. ) async throws -> Result where Result: Sendable {
  696. try await self.client.clientStreaming(
  697. request: request,
  698. descriptor: Test_TestService.Method.ClientStreaming.descriptor,
  699. serializer: serializer,
  700. deserializer: deserializer,
  701. options: options,
  702. onResponse: handleResponse
  703. )
  704. }
  705. /// Call the "ServerStreaming" method.
  706. ///
  707. /// > Source IDL Documentation:
  708. /// >
  709. /// > Server streaming docs.
  710. ///
  711. /// - Parameters:
  712. /// - request: A request containing a single `Test_TestInput` message.
  713. /// - serializer: A serializer for `Test_TestInput` messages.
  714. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  715. /// - options: Options to apply to this RPC.
  716. /// - handleResponse: A closure which handles the response, the result of which is
  717. /// returned to the caller. Returning from the closure will cancel the RPC if it
  718. /// hasn't already finished.
  719. /// - Returns: The result of `handleResponse`.
  720. \(access) func serverStreaming<Result>(
  721. request: GRPCCore.ClientRequest<Test_TestInput>,
  722. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  723. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  724. options: GRPCCore.CallOptions = .defaults,
  725. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  726. ) async throws -> Result where Result: Sendable {
  727. try await self.client.serverStreaming(
  728. request: request,
  729. descriptor: Test_TestService.Method.ServerStreaming.descriptor,
  730. serializer: serializer,
  731. deserializer: deserializer,
  732. options: options,
  733. onResponse: handleResponse
  734. )
  735. }
  736. /// Call the "BidirectionalStreaming" method.
  737. ///
  738. /// > Source IDL Documentation:
  739. /// >
  740. /// > Bidirectional streaming docs.
  741. ///
  742. /// - Parameters:
  743. /// - request: A streaming request producing `Test_TestInput` messages.
  744. /// - serializer: A serializer for `Test_TestInput` messages.
  745. /// - deserializer: A deserializer for `Test_TestOutput` messages.
  746. /// - options: Options to apply to this RPC.
  747. /// - handleResponse: A closure which handles the response, the result of which is
  748. /// returned to the caller. Returning from the closure will cancel the RPC if it
  749. /// hasn't already finished.
  750. /// - Returns: The result of `handleResponse`.
  751. \(access) func bidirectionalStreaming<Result>(
  752. request: GRPCCore.StreamingClientRequest<Test_TestInput>,
  753. serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
  754. deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
  755. options: GRPCCore.CallOptions = .defaults,
  756. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  757. ) async throws -> Result where Result: Sendable {
  758. try await self.client.bidirectionalStreaming(
  759. request: request,
  760. descriptor: Test_TestService.Method.BidirectionalStreaming.descriptor,
  761. serializer: serializer,
  762. deserializer: deserializer,
  763. options: options,
  764. onResponse: handleResponse
  765. )
  766. }
  767. }
  768. }
  769. // Helpers providing default arguments to 'ClientProtocol' methods.
  770. extension Test_TestService.ClientProtocol {
  771. /// Call the "Unary" method.
  772. ///
  773. /// > Source IDL Documentation:
  774. /// >
  775. /// > Unary docs.
  776. ///
  777. /// - Parameters:
  778. /// - request: A request containing a single `Test_TestInput` message.
  779. /// - options: Options to apply to this RPC.
  780. /// - handleResponse: A closure which handles the response, the result of which is
  781. /// returned to the caller. Returning from the closure will cancel the RPC if it
  782. /// hasn't already finished.
  783. /// - Returns: The result of `handleResponse`.
  784. \(access) func unary<Result>(
  785. request: GRPCCore.ClientRequest<Test_TestInput>,
  786. options: GRPCCore.CallOptions = .defaults,
  787. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
  788. try response.message
  789. }
  790. ) async throws -> Result where Result: Sendable {
  791. try await self.unary(
  792. request: request,
  793. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestInput>(),
  794. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestOutput>(),
  795. options: options,
  796. onResponse: handleResponse
  797. )
  798. }
  799. /// Call the "ClientStreaming" method.
  800. ///
  801. /// > Source IDL Documentation:
  802. /// >
  803. /// > Client streaming docs.
  804. ///
  805. /// - Parameters:
  806. /// - request: A streaming request producing `Test_TestInput` messages.
  807. /// - options: Options to apply to this RPC.
  808. /// - handleResponse: A closure which handles the response, the result of which is
  809. /// returned to the caller. Returning from the closure will cancel the RPC if it
  810. /// hasn't already finished.
  811. /// - Returns: The result of `handleResponse`.
  812. \(access) func clientStreaming<Result>(
  813. request: GRPCCore.StreamingClientRequest<Test_TestInput>,
  814. options: GRPCCore.CallOptions = .defaults,
  815. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
  816. try response.message
  817. }
  818. ) async throws -> Result where Result: Sendable {
  819. try await self.clientStreaming(
  820. request: request,
  821. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestInput>(),
  822. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestOutput>(),
  823. options: options,
  824. onResponse: handleResponse
  825. )
  826. }
  827. /// Call the "ServerStreaming" method.
  828. ///
  829. /// > Source IDL Documentation:
  830. /// >
  831. /// > Server streaming docs.
  832. ///
  833. /// - Parameters:
  834. /// - request: A request containing a single `Test_TestInput` message.
  835. /// - options: Options to apply to this RPC.
  836. /// - handleResponse: A closure which handles the response, the result of which is
  837. /// returned to the caller. Returning from the closure will cancel the RPC if it
  838. /// hasn't already finished.
  839. /// - Returns: The result of `handleResponse`.
  840. \(access) func serverStreaming<Result>(
  841. request: GRPCCore.ClientRequest<Test_TestInput>,
  842. options: GRPCCore.CallOptions = .defaults,
  843. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  844. ) async throws -> Result where Result: Sendable {
  845. try await self.serverStreaming(
  846. request: request,
  847. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestInput>(),
  848. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestOutput>(),
  849. options: options,
  850. onResponse: handleResponse
  851. )
  852. }
  853. /// Call the "BidirectionalStreaming" method.
  854. ///
  855. /// > Source IDL Documentation:
  856. /// >
  857. /// > Bidirectional streaming docs.
  858. ///
  859. /// - Parameters:
  860. /// - request: A streaming request producing `Test_TestInput` messages.
  861. /// - options: Options to apply to this RPC.
  862. /// - handleResponse: A closure which handles the response, the result of which is
  863. /// returned to the caller. Returning from the closure will cancel the RPC if it
  864. /// hasn't already finished.
  865. /// - Returns: The result of `handleResponse`.
  866. \(access) func bidirectionalStreaming<Result>(
  867. request: GRPCCore.StreamingClientRequest<Test_TestInput>,
  868. options: GRPCCore.CallOptions = .defaults,
  869. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  870. ) async throws -> Result where Result: Sendable {
  871. try await self.bidirectionalStreaming(
  872. request: request,
  873. serializer: GRPCProtobuf.ProtobufSerializer<Test_TestInput>(),
  874. deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestOutput>(),
  875. options: options,
  876. onResponse: handleResponse
  877. )
  878. }
  879. }
  880. // Helpers providing sugared APIs for 'ClientProtocol' methods.
  881. extension Test_TestService.ClientProtocol {
  882. /// Call the "Unary" method.
  883. ///
  884. /// > Source IDL Documentation:
  885. /// >
  886. /// > Unary docs.
  887. ///
  888. /// - Parameters:
  889. /// - message: request message to send.
  890. /// - metadata: Additional metadata to send, defaults to empty.
  891. /// - options: Options to apply to this RPC, defaults to `.defaults`.
  892. /// - handleResponse: A closure which handles the response, the result of which is
  893. /// returned to the caller. Returning from the closure will cancel the RPC if it
  894. /// hasn't already finished.
  895. /// - Returns: The result of `handleResponse`.
  896. \(access) func unary<Result>(
  897. _ message: Test_TestInput,
  898. metadata: GRPCCore.Metadata = [:],
  899. options: GRPCCore.CallOptions = .defaults,
  900. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
  901. try response.message
  902. }
  903. ) async throws -> Result where Result: Sendable {
  904. let request = GRPCCore.ClientRequest<Test_TestInput>(
  905. message: message,
  906. metadata: metadata
  907. )
  908. return try await self.unary(
  909. request: request,
  910. options: options,
  911. onResponse: handleResponse
  912. )
  913. }
  914. /// Call the "ClientStreaming" method.
  915. ///
  916. /// > Source IDL Documentation:
  917. /// >
  918. /// > Client streaming docs.
  919. ///
  920. /// - Parameters:
  921. /// - metadata: Additional metadata to send, defaults to empty.
  922. /// - options: Options to apply to this RPC, defaults to `.defaults`.
  923. /// - producer: A closure producing request messages to send to the server. The request
  924. /// stream is closed when the closure returns.
  925. /// - handleResponse: A closure which handles the response, the result of which is
  926. /// returned to the caller. Returning from the closure will cancel the RPC if it
  927. /// hasn't already finished.
  928. /// - Returns: The result of `handleResponse`.
  929. \(access) func clientStreaming<Result>(
  930. metadata: GRPCCore.Metadata = [:],
  931. options: GRPCCore.CallOptions = .defaults,
  932. requestProducer producer: @Sendable @escaping (GRPCCore.RPCWriter<Test_TestInput>) async throws -> Void,
  933. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
  934. try response.message
  935. }
  936. ) async throws -> Result where Result: Sendable {
  937. let request = GRPCCore.StreamingClientRequest<Test_TestInput>(
  938. metadata: metadata,
  939. producer: producer
  940. )
  941. return try await self.clientStreaming(
  942. request: request,
  943. options: options,
  944. onResponse: handleResponse
  945. )
  946. }
  947. /// Call the "ServerStreaming" method.
  948. ///
  949. /// > Source IDL Documentation:
  950. /// >
  951. /// > Server streaming docs.
  952. ///
  953. /// - Parameters:
  954. /// - message: request message to send.
  955. /// - metadata: Additional metadata to send, defaults to empty.
  956. /// - options: Options to apply to this RPC, defaults to `.defaults`.
  957. /// - handleResponse: A closure which handles the response, the result of which is
  958. /// returned to the caller. Returning from the closure will cancel the RPC if it
  959. /// hasn't already finished.
  960. /// - Returns: The result of `handleResponse`.
  961. \(access) func serverStreaming<Result>(
  962. _ message: Test_TestInput,
  963. metadata: GRPCCore.Metadata = [:],
  964. options: GRPCCore.CallOptions = .defaults,
  965. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  966. ) async throws -> Result where Result: Sendable {
  967. let request = GRPCCore.ClientRequest<Test_TestInput>(
  968. message: message,
  969. metadata: metadata
  970. )
  971. return try await self.serverStreaming(
  972. request: request,
  973. options: options,
  974. onResponse: handleResponse
  975. )
  976. }
  977. /// Call the "BidirectionalStreaming" method.
  978. ///
  979. /// > Source IDL Documentation:
  980. /// >
  981. /// > Bidirectional streaming docs.
  982. ///
  983. /// - Parameters:
  984. /// - metadata: Additional metadata to send, defaults to empty.
  985. /// - options: Options to apply to this RPC, defaults to `.defaults`.
  986. /// - producer: A closure producing request messages to send to the server. The request
  987. /// stream is closed when the closure returns.
  988. /// - handleResponse: A closure which handles the response, the result of which is
  989. /// returned to the caller. Returning from the closure will cancel the RPC if it
  990. /// hasn't already finished.
  991. /// - Returns: The result of `handleResponse`.
  992. \(access) func bidirectionalStreaming<Result>(
  993. metadata: GRPCCore.Metadata = [:],
  994. options: GRPCCore.CallOptions = .defaults,
  995. requestProducer producer: @Sendable @escaping (GRPCCore.RPCWriter<Test_TestInput>) async throws -> Void,
  996. onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
  997. ) async throws -> Result where Result: Sendable {
  998. let request = GRPCCore.StreamingClientRequest<Test_TestInput>(
  999. metadata: metadata,
  1000. producer: producer
  1001. )
  1002. return try await self.bidirectionalStreaming(
  1003. request: request,
  1004. options: options,
  1005. onResponse: handleResponse
  1006. )
  1007. }
  1008. }
  1009. """
  1010. #expect(generated == expected)
  1011. }
  1012. }