| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108 |
- /*
- * Copyright 2024, gRPC Authors All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- import GRPCCodeGen
- import GRPCProtobufCodeGen
- import SwiftProtobufPluginLibrary
- import Testing
- struct ProtobufCodeGeneratorTests {
- @Suite("Self-contained service (test-service.proto)")
- struct TestService: UsesDescriptorSet {
- static let descriptorSetName = "test-service"
- static let fileDescriptorName = "test-service"
- @Test("Generate", arguments: [CodeGenerator.Config.AccessLevel.internal])
- func generate(accessLevel: GRPCCodeGen.CodeGenerator.Config.AccessLevel) throws {
- let generator = ProtobufCodeGenerator(
- config: CodeGenerator.Config(
- accessLevel: accessLevel,
- accessLevelOnImports: false,
- client: true,
- server: true,
- indentation: 2
- )
- )
- let access: String
- switch accessLevel {
- case .public:
- access = "public"
- case .internal:
- access = "internal"
- case .package:
- access = "package"
- default:
- fatalError()
- }
- let generated = try generator.generateCode(
- fileDescriptor: Self.fileDescriptor,
- protoFileModuleMappings: ProtoFileToModuleMappings(),
- extraModuleImports: []
- )
- let expected = """
- /// Leading trivia.
- // DO NOT EDIT.
- // swift-format-ignore-file
- //
- // Generated by the gRPC Swift generator plugin for the protocol buffer compiler.
- // Source: test-service.proto
- //
- // For information on using the generated types, please see the documentation:
- // https://github.com/grpc/grpc-swift
- import GRPCCore
- import GRPCProtobuf
- // MARK: - test.TestService
- /// Namespace containing generated types for the "test.TestService" service.
- \(access) enum Test_TestService {
- /// Service descriptor for the "test.TestService" service.
- \(access) static let descriptor = GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService")
- /// Namespace for method metadata.
- \(access) enum Method {
- /// Namespace for "Unary" metadata.
- \(access) enum Unary {
- /// Request type for "Unary".
- \(access) typealias Input = Test_TestInput
- /// Response type for "Unary".
- \(access) typealias Output = Test_TestOutput
- /// Descriptor for "Unary".
- \(access) static let descriptor = GRPCCore.MethodDescriptor(
- service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService"),
- method: "Unary"
- )
- }
- /// Namespace for "ClientStreaming" metadata.
- \(access) enum ClientStreaming {
- /// Request type for "ClientStreaming".
- \(access) typealias Input = Test_TestInput
- /// Response type for "ClientStreaming".
- \(access) typealias Output = Test_TestOutput
- /// Descriptor for "ClientStreaming".
- \(access) static let descriptor = GRPCCore.MethodDescriptor(
- service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService"),
- method: "ClientStreaming"
- )
- }
- /// Namespace for "ServerStreaming" metadata.
- \(access) enum ServerStreaming {
- /// Request type for "ServerStreaming".
- \(access) typealias Input = Test_TestInput
- /// Response type for "ServerStreaming".
- \(access) typealias Output = Test_TestOutput
- /// Descriptor for "ServerStreaming".
- \(access) static let descriptor = GRPCCore.MethodDescriptor(
- service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService"),
- method: "ServerStreaming"
- )
- }
- /// Namespace for "BidirectionalStreaming" metadata.
- \(access) enum BidirectionalStreaming {
- /// Request type for "BidirectionalStreaming".
- \(access) typealias Input = Test_TestInput
- /// Response type for "BidirectionalStreaming".
- \(access) typealias Output = Test_TestOutput
- /// Descriptor for "BidirectionalStreaming".
- \(access) static let descriptor = GRPCCore.MethodDescriptor(
- service: GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService"),
- method: "BidirectionalStreaming"
- )
- }
- /// Descriptors for all methods in the "test.TestService" service.
- \(access) static let descriptors: [GRPCCore.MethodDescriptor] = [
- Unary.descriptor,
- ClientStreaming.descriptor,
- ServerStreaming.descriptor,
- BidirectionalStreaming.descriptor
- ]
- }
- }
- extension GRPCCore.ServiceDescriptor {
- /// Service descriptor for the "test.TestService" service.
- \(access) static let test_TestService = GRPCCore.ServiceDescriptor(fullyQualifiedService: "test.TestService")
- }
- // MARK: test.TestService (server)
- extension Test_TestService {
- /// Streaming variant of the service protocol for the "test.TestService" service.
- ///
- /// This protocol is the lowest-level of the service protocols generated for this service
- /// giving you the most flexibility over the implementation of your service. This comes at
- /// the cost of more verbose and less strict APIs. Each RPC requires you to implement it in
- /// terms of a request stream and response stream. Where only a single request or response
- /// message is expected, you are responsible for enforcing this invariant is maintained.
- ///
- /// Where possible, prefer using the stricter, less-verbose ``ServiceProtocol``
- /// or ``SimpleServiceProtocol`` instead.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Service docs.
- \(access) protocol StreamingServiceProtocol: GRPCCore.RegistrableRPCService {
- /// Handle the "Unary" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Unary docs.
- ///
- /// - Parameters:
- /// - request: A streaming request of `Test_TestInput` messages.
- /// - context: Context providing information about the RPC.
- /// - Throws: Any error which occurred during the processing of the request. Thrown errors
- /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
- /// to an internal error.
- /// - Returns: A streaming response of `Test_TestOutput` messages.
- func unary(
- request: GRPCCore.StreamingServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
- /// Handle the "ClientStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Client streaming docs.
- ///
- /// - Parameters:
- /// - request: A streaming request of `Test_TestInput` messages.
- /// - context: Context providing information about the RPC.
- /// - Throws: Any error which occurred during the processing of the request. Thrown errors
- /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
- /// to an internal error.
- /// - Returns: A streaming response of `Test_TestOutput` messages.
- func clientStreaming(
- request: GRPCCore.StreamingServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
- /// Handle the "ServerStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Server streaming docs.
- ///
- /// - Parameters:
- /// - request: A streaming request of `Test_TestInput` messages.
- /// - context: Context providing information about the RPC.
- /// - Throws: Any error which occurred during the processing of the request. Thrown errors
- /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
- /// to an internal error.
- /// - Returns: A streaming response of `Test_TestOutput` messages.
- func serverStreaming(
- request: GRPCCore.StreamingServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
- /// Handle the "BidirectionalStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Bidirectional streaming docs.
- ///
- /// - Parameters:
- /// - request: A streaming request of `Test_TestInput` messages.
- /// - context: Context providing information about the RPC.
- /// - Throws: Any error which occurred during the processing of the request. Thrown errors
- /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
- /// to an internal error.
- /// - Returns: A streaming response of `Test_TestOutput` messages.
- func bidirectionalStreaming(
- request: GRPCCore.StreamingServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
- }
- /// Service protocol for the "test.TestService" service.
- ///
- /// This protocol is higher level than ``StreamingServiceProtocol`` but lower level than
- /// the ``SimpleServiceProtocol``, it provides access to request and response metadata and
- /// trailing response metadata. If you don't need these then consider using
- /// the ``SimpleServiceProtocol``. If you need fine grained control over your RPCs then
- /// use ``StreamingServiceProtocol``.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Service docs.
- \(access) protocol ServiceProtocol: Test_TestService.StreamingServiceProtocol {
- /// Handle the "Unary" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Unary docs.
- ///
- /// - Parameters:
- /// - request: A request containing a single `Test_TestInput` message.
- /// - context: Context providing information about the RPC.
- /// - Throws: Any error which occurred during the processing of the request. Thrown errors
- /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
- /// to an internal error.
- /// - Returns: A response containing a single `Test_TestOutput` message.
- func unary(
- request: GRPCCore.ServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.ServerResponse<Test_TestOutput>
- /// Handle the "ClientStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Client streaming docs.
- ///
- /// - Parameters:
- /// - request: A streaming request of `Test_TestInput` messages.
- /// - context: Context providing information about the RPC.
- /// - Throws: Any error which occurred during the processing of the request. Thrown errors
- /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
- /// to an internal error.
- /// - Returns: A response containing a single `Test_TestOutput` message.
- func clientStreaming(
- request: GRPCCore.StreamingServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.ServerResponse<Test_TestOutput>
- /// Handle the "ServerStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Server streaming docs.
- ///
- /// - Parameters:
- /// - request: A request containing a single `Test_TestInput` message.
- /// - context: Context providing information about the RPC.
- /// - Throws: Any error which occurred during the processing of the request. Thrown errors
- /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
- /// to an internal error.
- /// - Returns: A streaming response of `Test_TestOutput` messages.
- func serverStreaming(
- request: GRPCCore.ServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
- /// Handle the "BidirectionalStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Bidirectional streaming docs.
- ///
- /// - Parameters:
- /// - request: A streaming request of `Test_TestInput` messages.
- /// - context: Context providing information about the RPC.
- /// - Throws: Any error which occurred during the processing of the request. Thrown errors
- /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
- /// to an internal error.
- /// - Returns: A streaming response of `Test_TestOutput` messages.
- func bidirectionalStreaming(
- request: GRPCCore.StreamingServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput>
- }
- /// Simple service protocol for the "test.TestService" service.
- ///
- /// This is the highest level protocol for the service. The API is the easiest to use but
- /// doesn't provide access to request or response metadata. If you need access to these
- /// then use ``ServiceProtocol`` instead.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Service docs.
- \(access) protocol SimpleServiceProtocol: Test_TestService.ServiceProtocol {
- /// Handle the "Unary" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Unary docs.
- ///
- /// - Parameters:
- /// - request: A `Test_TestInput` message.
- /// - context: Context providing information about the RPC.
- /// - Throws: Any error which occurred during the processing of the request. Thrown errors
- /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
- /// to an internal error.
- /// - Returns: A `Test_TestOutput` to respond with.
- func unary(
- request: Test_TestInput,
- context: GRPCCore.ServerContext
- ) async throws -> Test_TestOutput
- /// Handle the "ClientStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Client streaming docs.
- ///
- /// - Parameters:
- /// - request: A stream of `Test_TestInput` messages.
- /// - context: Context providing information about the RPC.
- /// - Throws: Any error which occurred during the processing of the request. Thrown errors
- /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
- /// to an internal error.
- /// - Returns: A `Test_TestOutput` to respond with.
- func clientStreaming(
- request: GRPCCore.RPCAsyncSequence<Test_TestInput, any Swift.Error>,
- context: GRPCCore.ServerContext
- ) async throws -> Test_TestOutput
- /// Handle the "ServerStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Server streaming docs.
- ///
- /// - Parameters:
- /// - request: A `Test_TestInput` message.
- /// - response: A response stream of `Test_TestOutput` messages.
- /// - context: Context providing information about the RPC.
- /// - Throws: Any error which occurred during the processing of the request. Thrown errors
- /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
- /// to an internal error.
- func serverStreaming(
- request: Test_TestInput,
- response: GRPCCore.RPCWriter<Test_TestOutput>,
- context: GRPCCore.ServerContext
- ) async throws
- /// Handle the "BidirectionalStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Bidirectional streaming docs.
- ///
- /// - Parameters:
- /// - request: A stream of `Test_TestInput` messages.
- /// - response: A response stream of `Test_TestOutput` messages.
- /// - context: Context providing information about the RPC.
- /// - Throws: Any error which occurred during the processing of the request. Thrown errors
- /// of type `RPCError` are mapped to appropriate statuses. All other errors are converted
- /// to an internal error.
- func bidirectionalStreaming(
- request: GRPCCore.RPCAsyncSequence<Test_TestInput, any Swift.Error>,
- response: GRPCCore.RPCWriter<Test_TestOutput>,
- context: GRPCCore.ServerContext
- ) async throws
- }
- }
- // Default implementation of 'registerMethods(with:)'.
- extension Test_TestService.StreamingServiceProtocol {
- \(access) func registerMethods<Transport>(with router: inout GRPCCore.RPCRouter<Transport>) where Transport: GRPCCore.ServerTransport {
- router.registerHandler(
- forMethod: Test_TestService.Method.Unary.descriptor,
- deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestInput>(),
- serializer: GRPCProtobuf.ProtobufSerializer<Test_TestOutput>(),
- handler: { request, context in
- try await self.unary(
- request: request,
- context: context
- )
- }
- )
- router.registerHandler(
- forMethod: Test_TestService.Method.ClientStreaming.descriptor,
- deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestInput>(),
- serializer: GRPCProtobuf.ProtobufSerializer<Test_TestOutput>(),
- handler: { request, context in
- try await self.clientStreaming(
- request: request,
- context: context
- )
- }
- )
- router.registerHandler(
- forMethod: Test_TestService.Method.ServerStreaming.descriptor,
- deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestInput>(),
- serializer: GRPCProtobuf.ProtobufSerializer<Test_TestOutput>(),
- handler: { request, context in
- try await self.serverStreaming(
- request: request,
- context: context
- )
- }
- )
- router.registerHandler(
- forMethod: Test_TestService.Method.BidirectionalStreaming.descriptor,
- deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestInput>(),
- serializer: GRPCProtobuf.ProtobufSerializer<Test_TestOutput>(),
- handler: { request, context in
- try await self.bidirectionalStreaming(
- request: request,
- context: context
- )
- }
- )
- }
- }
- // Default implementation of streaming methods from 'StreamingServiceProtocol'.
- extension Test_TestService.ServiceProtocol {
- \(access) func unary(
- request: GRPCCore.StreamingServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
- let response = try await self.unary(
- request: GRPCCore.ServerRequest(stream: request),
- context: context
- )
- return GRPCCore.StreamingServerResponse(single: response)
- }
- \(access) func clientStreaming(
- request: GRPCCore.StreamingServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
- let response = try await self.clientStreaming(
- request: request,
- context: context
- )
- return GRPCCore.StreamingServerResponse(single: response)
- }
- \(access) func serverStreaming(
- request: GRPCCore.StreamingServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
- let response = try await self.serverStreaming(
- request: GRPCCore.ServerRequest(stream: request),
- context: context
- )
- return response
- }
- }
- // Default implementation of methods from 'ServiceProtocol'.
- extension Test_TestService.SimpleServiceProtocol {
- \(access) func unary(
- request: GRPCCore.ServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.ServerResponse<Test_TestOutput> {
- return GRPCCore.ServerResponse<Test_TestOutput>(
- message: try await self.unary(
- request: request.message,
- context: context
- ),
- metadata: [:]
- )
- }
- \(access) func clientStreaming(
- request: GRPCCore.StreamingServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.ServerResponse<Test_TestOutput> {
- return GRPCCore.ServerResponse<Test_TestOutput>(
- message: try await self.clientStreaming(
- request: request.messages,
- context: context
- ),
- metadata: [:]
- )
- }
- \(access) func serverStreaming(
- request: GRPCCore.ServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
- return GRPCCore.StreamingServerResponse<Test_TestOutput>(
- metadata: [:],
- producer: { writer in
- try await self.serverStreaming(
- request: request.message,
- response: writer,
- context: context
- )
- return [:]
- }
- )
- }
- \(access) func bidirectionalStreaming(
- request: GRPCCore.StreamingServerRequest<Test_TestInput>,
- context: GRPCCore.ServerContext
- ) async throws -> GRPCCore.StreamingServerResponse<Test_TestOutput> {
- return GRPCCore.StreamingServerResponse<Test_TestOutput>(
- metadata: [:],
- producer: { writer in
- try await self.bidirectionalStreaming(
- request: request.messages,
- response: writer,
- context: context
- )
- return [:]
- }
- )
- }
- }
- // MARK: test.TestService (client)
- extension Test_TestService {
- /// Generated client protocol for the "test.TestService" service.
- ///
- /// You don't need to implement this protocol directly, use the generated
- /// implementation, ``Client``.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Service docs.
- \(access) protocol ClientProtocol: Sendable {
- /// Call the "Unary" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Unary docs.
- ///
- /// - Parameters:
- /// - request: A request containing a single `Test_TestInput` message.
- /// - serializer: A serializer for `Test_TestInput` messages.
- /// - deserializer: A deserializer for `Test_TestOutput` messages.
- /// - options: Options to apply to this RPC.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- func unary<Result>(
- request: GRPCCore.ClientRequest<Test_TestInput>,
- serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
- deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
- options: GRPCCore.CallOptions,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result
- ) async throws -> Result where Result: Sendable
- /// Call the "ClientStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Client streaming docs.
- ///
- /// - Parameters:
- /// - request: A streaming request producing `Test_TestInput` messages.
- /// - serializer: A serializer for `Test_TestInput` messages.
- /// - deserializer: A deserializer for `Test_TestOutput` messages.
- /// - options: Options to apply to this RPC.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- func clientStreaming<Result>(
- request: GRPCCore.StreamingClientRequest<Test_TestInput>,
- serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
- deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
- options: GRPCCore.CallOptions,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result
- ) async throws -> Result where Result: Sendable
- /// Call the "ServerStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Server streaming docs.
- ///
- /// - Parameters:
- /// - request: A request containing a single `Test_TestInput` message.
- /// - serializer: A serializer for `Test_TestInput` messages.
- /// - deserializer: A deserializer for `Test_TestOutput` messages.
- /// - options: Options to apply to this RPC.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- func serverStreaming<Result>(
- request: GRPCCore.ClientRequest<Test_TestInput>,
- serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
- deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
- options: GRPCCore.CallOptions,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
- ) async throws -> Result where Result: Sendable
- /// Call the "BidirectionalStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Bidirectional streaming docs.
- ///
- /// - Parameters:
- /// - request: A streaming request producing `Test_TestInput` messages.
- /// - serializer: A serializer for `Test_TestInput` messages.
- /// - deserializer: A deserializer for `Test_TestOutput` messages.
- /// - options: Options to apply to this RPC.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- func bidirectionalStreaming<Result>(
- request: GRPCCore.StreamingClientRequest<Test_TestInput>,
- serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
- deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
- options: GRPCCore.CallOptions,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
- ) async throws -> Result where Result: Sendable
- }
- /// Generated client for the "test.TestService" service.
- ///
- /// The ``Client`` provides an implementation of ``ClientProtocol`` which wraps
- /// a `GRPCCore.GRPCCClient`. The underlying `GRPCClient` provides the long-lived
- /// means of communication with the remote peer.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Service docs.
- \(access) struct Client<Transport>: ClientProtocol where Transport: GRPCCore.ClientTransport {
- private let client: GRPCCore.GRPCClient<Transport>
- /// Creates a new client wrapping the provided `GRPCCore.GRPCClient`.
- ///
- /// - Parameters:
- /// - client: A `GRPCCore.GRPCClient` providing a communication channel to the service.
- \(access) init(wrapping client: GRPCCore.GRPCClient<Transport>) {
- self.client = client
- }
- /// Call the "Unary" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Unary docs.
- ///
- /// - Parameters:
- /// - request: A request containing a single `Test_TestInput` message.
- /// - serializer: A serializer for `Test_TestInput` messages.
- /// - deserializer: A deserializer for `Test_TestOutput` messages.
- /// - options: Options to apply to this RPC.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- \(access) func unary<Result>(
- request: GRPCCore.ClientRequest<Test_TestInput>,
- serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
- deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
- options: GRPCCore.CallOptions = .defaults,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
- try response.message
- }
- ) async throws -> Result where Result: Sendable {
- try await self.client.unary(
- request: request,
- descriptor: Test_TestService.Method.Unary.descriptor,
- serializer: serializer,
- deserializer: deserializer,
- options: options,
- onResponse: handleResponse
- )
- }
- /// Call the "ClientStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Client streaming docs.
- ///
- /// - Parameters:
- /// - request: A streaming request producing `Test_TestInput` messages.
- /// - serializer: A serializer for `Test_TestInput` messages.
- /// - deserializer: A deserializer for `Test_TestOutput` messages.
- /// - options: Options to apply to this RPC.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- \(access) func clientStreaming<Result>(
- request: GRPCCore.StreamingClientRequest<Test_TestInput>,
- serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
- deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
- options: GRPCCore.CallOptions = .defaults,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
- try response.message
- }
- ) async throws -> Result where Result: Sendable {
- try await self.client.clientStreaming(
- request: request,
- descriptor: Test_TestService.Method.ClientStreaming.descriptor,
- serializer: serializer,
- deserializer: deserializer,
- options: options,
- onResponse: handleResponse
- )
- }
- /// Call the "ServerStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Server streaming docs.
- ///
- /// - Parameters:
- /// - request: A request containing a single `Test_TestInput` message.
- /// - serializer: A serializer for `Test_TestInput` messages.
- /// - deserializer: A deserializer for `Test_TestOutput` messages.
- /// - options: Options to apply to this RPC.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- \(access) func serverStreaming<Result>(
- request: GRPCCore.ClientRequest<Test_TestInput>,
- serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
- deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
- options: GRPCCore.CallOptions = .defaults,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
- ) async throws -> Result where Result: Sendable {
- try await self.client.serverStreaming(
- request: request,
- descriptor: Test_TestService.Method.ServerStreaming.descriptor,
- serializer: serializer,
- deserializer: deserializer,
- options: options,
- onResponse: handleResponse
- )
- }
- /// Call the "BidirectionalStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Bidirectional streaming docs.
- ///
- /// - Parameters:
- /// - request: A streaming request producing `Test_TestInput` messages.
- /// - serializer: A serializer for `Test_TestInput` messages.
- /// - deserializer: A deserializer for `Test_TestOutput` messages.
- /// - options: Options to apply to this RPC.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- \(access) func bidirectionalStreaming<Result>(
- request: GRPCCore.StreamingClientRequest<Test_TestInput>,
- serializer: some GRPCCore.MessageSerializer<Test_TestInput>,
- deserializer: some GRPCCore.MessageDeserializer<Test_TestOutput>,
- options: GRPCCore.CallOptions = .defaults,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
- ) async throws -> Result where Result: Sendable {
- try await self.client.bidirectionalStreaming(
- request: request,
- descriptor: Test_TestService.Method.BidirectionalStreaming.descriptor,
- serializer: serializer,
- deserializer: deserializer,
- options: options,
- onResponse: handleResponse
- )
- }
- }
- }
- // Helpers providing default arguments to 'ClientProtocol' methods.
- extension Test_TestService.ClientProtocol {
- /// Call the "Unary" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Unary docs.
- ///
- /// - Parameters:
- /// - request: A request containing a single `Test_TestInput` message.
- /// - options: Options to apply to this RPC.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- \(access) func unary<Result>(
- request: GRPCCore.ClientRequest<Test_TestInput>,
- options: GRPCCore.CallOptions = .defaults,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
- try response.message
- }
- ) async throws -> Result where Result: Sendable {
- try await self.unary(
- request: request,
- serializer: GRPCProtobuf.ProtobufSerializer<Test_TestInput>(),
- deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestOutput>(),
- options: options,
- onResponse: handleResponse
- )
- }
- /// Call the "ClientStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Client streaming docs.
- ///
- /// - Parameters:
- /// - request: A streaming request producing `Test_TestInput` messages.
- /// - options: Options to apply to this RPC.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- \(access) func clientStreaming<Result>(
- request: GRPCCore.StreamingClientRequest<Test_TestInput>,
- options: GRPCCore.CallOptions = .defaults,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
- try response.message
- }
- ) async throws -> Result where Result: Sendable {
- try await self.clientStreaming(
- request: request,
- serializer: GRPCProtobuf.ProtobufSerializer<Test_TestInput>(),
- deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestOutput>(),
- options: options,
- onResponse: handleResponse
- )
- }
- /// Call the "ServerStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Server streaming docs.
- ///
- /// - Parameters:
- /// - request: A request containing a single `Test_TestInput` message.
- /// - options: Options to apply to this RPC.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- \(access) func serverStreaming<Result>(
- request: GRPCCore.ClientRequest<Test_TestInput>,
- options: GRPCCore.CallOptions = .defaults,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
- ) async throws -> Result where Result: Sendable {
- try await self.serverStreaming(
- request: request,
- serializer: GRPCProtobuf.ProtobufSerializer<Test_TestInput>(),
- deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestOutput>(),
- options: options,
- onResponse: handleResponse
- )
- }
- /// Call the "BidirectionalStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Bidirectional streaming docs.
- ///
- /// - Parameters:
- /// - request: A streaming request producing `Test_TestInput` messages.
- /// - options: Options to apply to this RPC.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- \(access) func bidirectionalStreaming<Result>(
- request: GRPCCore.StreamingClientRequest<Test_TestInput>,
- options: GRPCCore.CallOptions = .defaults,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
- ) async throws -> Result where Result: Sendable {
- try await self.bidirectionalStreaming(
- request: request,
- serializer: GRPCProtobuf.ProtobufSerializer<Test_TestInput>(),
- deserializer: GRPCProtobuf.ProtobufDeserializer<Test_TestOutput>(),
- options: options,
- onResponse: handleResponse
- )
- }
- }
- // Helpers providing sugared APIs for 'ClientProtocol' methods.
- extension Test_TestService.ClientProtocol {
- /// Call the "Unary" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Unary docs.
- ///
- /// - Parameters:
- /// - message: request message to send.
- /// - metadata: Additional metadata to send, defaults to empty.
- /// - options: Options to apply to this RPC, defaults to `.defaults`.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- \(access) func unary<Result>(
- _ message: Test_TestInput,
- metadata: GRPCCore.Metadata = [:],
- options: GRPCCore.CallOptions = .defaults,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
- try response.message
- }
- ) async throws -> Result where Result: Sendable {
- let request = GRPCCore.ClientRequest<Test_TestInput>(
- message: message,
- metadata: metadata
- )
- return try await self.unary(
- request: request,
- options: options,
- onResponse: handleResponse
- )
- }
- /// Call the "ClientStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Client streaming docs.
- ///
- /// - Parameters:
- /// - metadata: Additional metadata to send, defaults to empty.
- /// - options: Options to apply to this RPC, defaults to `.defaults`.
- /// - producer: A closure producing request messages to send to the server. The request
- /// stream is closed when the closure returns.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- \(access) func clientStreaming<Result>(
- metadata: GRPCCore.Metadata = [:],
- options: GRPCCore.CallOptions = .defaults,
- requestProducer producer: @Sendable @escaping (GRPCCore.RPCWriter<Test_TestInput>) async throws -> Void,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<Test_TestOutput>) async throws -> Result = { response in
- try response.message
- }
- ) async throws -> Result where Result: Sendable {
- let request = GRPCCore.StreamingClientRequest<Test_TestInput>(
- metadata: metadata,
- producer: producer
- )
- return try await self.clientStreaming(
- request: request,
- options: options,
- onResponse: handleResponse
- )
- }
- /// Call the "ServerStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Server streaming docs.
- ///
- /// - Parameters:
- /// - message: request message to send.
- /// - metadata: Additional metadata to send, defaults to empty.
- /// - options: Options to apply to this RPC, defaults to `.defaults`.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- \(access) func serverStreaming<Result>(
- _ message: Test_TestInput,
- metadata: GRPCCore.Metadata = [:],
- options: GRPCCore.CallOptions = .defaults,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
- ) async throws -> Result where Result: Sendable {
- let request = GRPCCore.ClientRequest<Test_TestInput>(
- message: message,
- metadata: metadata
- )
- return try await self.serverStreaming(
- request: request,
- options: options,
- onResponse: handleResponse
- )
- }
- /// Call the "BidirectionalStreaming" method.
- ///
- /// > Source IDL Documentation:
- /// >
- /// > Bidirectional streaming docs.
- ///
- /// - Parameters:
- /// - metadata: Additional metadata to send, defaults to empty.
- /// - options: Options to apply to this RPC, defaults to `.defaults`.
- /// - producer: A closure producing request messages to send to the server. The request
- /// stream is closed when the closure returns.
- /// - handleResponse: A closure which handles the response, the result of which is
- /// returned to the caller. Returning from the closure will cancel the RPC if it
- /// hasn't already finished.
- /// - Returns: The result of `handleResponse`.
- \(access) func bidirectionalStreaming<Result>(
- metadata: GRPCCore.Metadata = [:],
- options: GRPCCore.CallOptions = .defaults,
- requestProducer producer: @Sendable @escaping (GRPCCore.RPCWriter<Test_TestInput>) async throws -> Void,
- onResponse handleResponse: @Sendable @escaping (GRPCCore.StreamingClientResponse<Test_TestOutput>) async throws -> Result
- ) async throws -> Result where Result: Sendable {
- let request = GRPCCore.StreamingClientRequest<Test_TestInput>(
- metadata: metadata,
- producer: producer
- )
- return try await self.bidirectionalStreaming(
- request: request,
- options: options,
- onResponse: handleResponse
- )
- }
- }
- """
- #expect(generated == expected)
- }
- }
- @Suite("File-without-services (foo-messages.proto)")
- struct NoServices: UsesDescriptorSet {
- static let descriptorSetName = "foo-messages"
- static let fileDescriptorName = "foo-messages"
- @Test("Generate")
- func generate() throws {
- let generator = ProtobufCodeGenerator(
- config: CodeGenerator.Config(
- accessLevel: .public,
- accessLevelOnImports: false,
- client: true,
- server: true,
- indentation: 2
- )
- )
- let generated = try generator.generateCode(
- fileDescriptor: Self.fileDescriptor,
- protoFileModuleMappings: ProtoFileToModuleMappings(),
- extraModuleImports: []
- )
- let expected = """
- /// Leading trivia.
- // DO NOT EDIT.
- // swift-format-ignore-file
- //
- // Generated by the gRPC Swift generator plugin for the protocol buffer compiler.
- // Source: foo-messages.proto
- //
- // For information on using the generated types, please see the documentation:
- // https://github.com/grpc/grpc-swift
- // This file contained no services.
- """
- #expect(generated == expected)
- }
- }
- }
|