| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- /*
- * Copyright 2023, 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.
- */
- /// Creates a representation for the server code that will be generated based on the ``CodeGenerationRequest`` object
- /// specifications, using types from ``StructuredSwiftRepresentation``.
- ///
- /// For example, in the case of a service called "Bar", in the "foo" namespace which has
- /// one method "baz" with input type "Input" and output type "Output", the ``ServerCodeTranslator`` will create
- /// a representation for the following generated code:
- ///
- /// ```swift
- /// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
- /// public protocol Foo_BarStreamingServiceProtocol: GRPCCore.RegistrableRPCService {
- /// func baz(
- /// request: GRPCCore.StreamingServerRequest<Foo_Bar_Input>
- /// ) async throws -> GRPCCore.StreamingServerResponse<Foo_Bar_Output>
- /// }
- /// // Conformance to `GRPCCore.RegistrableRPCService`.
- /// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
- /// extension Foo_Bar.StreamingServiceProtocol {
- /// public func registerMethods(with router: inout GRPCCore.RPCRouter) {
- /// router.registerHandler(
- /// forMethod: Foo_Bar.Method.baz.descriptor,
- /// deserializer: GRPCProtobuf.ProtobufDeserializer<Foo_Bar_Input>(),
- /// serializer: GRPCProtobuf.ProtobufSerializer<Foo_Bar_Output>(),
- /// handler: { request in try await self.baz(request: request) }
- /// )
- /// }
- /// }
- /// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
- /// public protocol Foo_BarServiceProtocol: Foo_Bar.StreamingServiceProtocol {
- /// func baz(
- /// request: GRPCCore.ServerRequest<Foo_Bar_Input>
- /// ) async throws -> GRPCCore.ServerResponse<Foo_Bar_Output>
- /// }
- /// // Partial conformance to `Foo_BarStreamingServiceProtocol`.
- /// @available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
- /// extension Foo_Bar.ServiceProtocol {
- /// public func baz(
- /// request: GRPCCore.StreamingServerRequest<Foo_Bar_Input>
- /// ) async throws -> GRPCCore.StreamingServerResponse<Foo_Bar_Output> {
- /// let response = try await self.baz(request: GRPCCore.ServerRequest(stream: request))
- /// return GRPCCore.StreamingServerResponse(single: response)
- /// }
- /// }
- ///```
- struct ServerCodeTranslator {
- init() {}
- func translate(
- accessModifier: AccessModifier,
- service: ServiceDescriptor,
- serializer: (String) -> String,
- deserializer: (String) -> String
- ) -> [CodeBlock] {
- var blocks = [CodeBlock]()
- let `extension` = ExtensionDescription(
- onType: service.namespacedGeneratedName,
- declarations: [
- // protocol StreamingServiceProtocol { ... }
- .commentable(
- .preFormatted(service.documentation),
- .protocol(
- .streamingService(
- accessLevel: accessModifier,
- name: "StreamingServiceProtocol",
- methods: service.methods
- )
- )
- ),
- // protocol ServiceProtocol { ... }
- .commentable(
- .preFormatted(service.documentation),
- .protocol(
- .service(
- accessLevel: accessModifier,
- name: "ServiceProtocol",
- streamingProtocol: "\(service.namespacedGeneratedName).StreamingServiceProtocol",
- methods: service.methods
- )
- )
- ),
- ]
- )
- blocks.append(.declaration(.extension(`extension`)))
- // extension <Service>.StreamingServiceProtocol> { ... }
- let registerExtension: ExtensionDescription = .registrableRPCServiceDefaultImplementation(
- accessLevel: accessModifier,
- on: "\(service.namespacedGeneratedName).StreamingServiceProtocol",
- serviceNamespace: service.namespacedGeneratedName,
- methods: service.methods,
- serializer: serializer,
- deserializer: deserializer
- )
- blocks.append(
- CodeBlock(
- comment: .doc("Conformance to `GRPCCore.RegistrableRPCService`."),
- item: .declaration(.extension(registerExtension))
- )
- )
- // extension <Service>_ServiceProtocol { ... }
- let streamingServiceDefaultImplExtension: ExtensionDescription =
- .streamingServiceProtocolDefaultImplementation(
- accessModifier: accessModifier,
- on: "\(service.namespacedGeneratedName).ServiceProtocol",
- methods: service.methods
- )
- blocks.append(.declaration(.extension(streamingServiceDefaultImplExtension)))
- return blocks
- }
- }
|