| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131 |
- /*
- * 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 {
- var config = ProtobufCodeGenerator.Config.defaults
- config.accessLevel = accessLevel
- config.indentation = 2
- let generator = ProtobufCodeGenerator(config: config)
- 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
- // swiftlint:disable all
- //
- // 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
- import SwiftProtobuf
- // 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)
- }
- @Test("Generate with different module names")
- func generateWithDifferentModuleNames() throws {
- var config = ProtobufCodeGenerator.Config.defaults
- let defaultNames = config.moduleNames
- config.accessLevel = .public
- config.indentation = 2
- config.moduleNames.grpcCore = String(config.moduleNames.grpcCore.reversed())
- config.moduleNames.grpcProtobuf = String(config.moduleNames.grpcProtobuf.reversed())
- config.moduleNames.swiftProtobuf = String(config.moduleNames.swiftProtobuf.reversed())
- let generator = ProtobufCodeGenerator(config: config)
- let generated = try generator.generateCode(
- fileDescriptor: Self.fileDescriptor,
- protoFileModuleMappings: ProtoFileToModuleMappings(),
- extraModuleImports: []
- )
- // Mustn't contain the default names.
- #expect(!generated.contains(defaultNames.grpcCore))
- #expect(!generated.contains(defaultNames.grpcProtobuf))
- #expect(!generated.contains(defaultNames.swiftProtobuf))
- // Must contain the configured names.
- #expect(generated.contains(config.moduleNames.grpcCore))
- #expect(generated.contains(config.moduleNames.grpcProtobuf))
- #expect(generated.contains(config.moduleNames.swiftProtobuf))
- }
- }
- @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 {
- var config: ProtobufCodeGenerator.Config = .defaults
- config.accessLevel = .public
- config.indentation = 2
- let generator = ProtobufCodeGenerator(config: config)
- let generated = try generator.generateCode(
- fileDescriptor: Self.fileDescriptor,
- protoFileModuleMappings: ProtoFileToModuleMappings(),
- extraModuleImports: []
- )
- let expected = """
- /// Leading trivia.
- // DO NOT EDIT.
- // swift-format-ignore-file
- // swiftlint:disable all
- //
- // 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)
- }
- }
- }
|