ProtobufCodeGeneratorTests.swift 47 KB

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