ProtobufCodeGeneratorTests.swift 52 KB

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