ClientCodeTranslatorSnippetBasedTests.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright 2023, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Testing
  17. @testable import GRPCCodeGen
  18. @Suite
  19. struct ClientCodeTranslatorSnippetBasedTests {
  20. @Test
  21. func translate() {
  22. let method = MethodDescriptor(
  23. documentation: "/// Documentation for MethodA",
  24. name: MethodName(identifyingName: "MethodA", typeName: "MethodA", functionName: "methodA"),
  25. isInputStreaming: false,
  26. isOutputStreaming: false,
  27. inputType: "NamespaceA_ServiceARequest",
  28. outputType: "NamespaceA_ServiceAResponse"
  29. )
  30. let service = ServiceDescriptor(
  31. documentation: "/// Documentation for ServiceA",
  32. name: ServiceName(
  33. identifyingName: "namespaceA.ServiceA",
  34. typeName: "NamespaceA_ServiceA",
  35. propertyName: ""
  36. ),
  37. methods: [method]
  38. )
  39. let expectedSwift = """
  40. extension NamespaceA_ServiceA {
  41. /// Generated client protocol for the "namespaceA.ServiceA" service.
  42. ///
  43. /// You don't need to implement this protocol directly, use the generated
  44. /// implementation, ``Client``.
  45. ///
  46. /// > Source IDL Documentation:
  47. /// >
  48. /// > Documentation for ServiceA
  49. public protocol ClientProtocol: Sendable {
  50. /// Call the "MethodA" method.
  51. ///
  52. /// > Source IDL Documentation:
  53. /// >
  54. /// > Documentation for MethodA
  55. ///
  56. /// - Parameters:
  57. /// - request: A request containing a single `NamespaceA_ServiceARequest` message.
  58. /// - serializer: A serializer for `NamespaceA_ServiceARequest` messages.
  59. /// - deserializer: A deserializer for `NamespaceA_ServiceAResponse` messages.
  60. /// - options: Options to apply to this RPC.
  61. /// - handleResponse: A closure which handles the response, the result of which is
  62. /// returned to the caller. Returning from the closure will cancel the RPC if it
  63. /// hasn't already finished.
  64. /// - Returns: The result of `handleResponse`.
  65. func methodA<Result>(
  66. request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
  67. serializer: some GRPCCore.MessageSerializer<NamespaceA_ServiceARequest>,
  68. deserializer: some GRPCCore.MessageDeserializer<NamespaceA_ServiceAResponse>,
  69. options: GRPCCore.CallOptions,
  70. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<NamespaceA_ServiceAResponse>) async throws -> Result
  71. ) async throws -> Result where Result: Sendable
  72. }
  73. /// Generated client for the "namespaceA.ServiceA" service.
  74. ///
  75. /// The ``Client`` provides an implementation of ``ClientProtocol`` which wraps
  76. /// a `GRPCCore.GRPCCClient`. The underlying `GRPCClient` provides the long-lived
  77. /// means of communication with the remote peer.
  78. ///
  79. /// > Source IDL Documentation:
  80. /// >
  81. /// > Documentation for ServiceA
  82. public struct Client<Transport>: ClientProtocol where Transport: GRPCCore.ClientTransport {
  83. private let client: GRPCCore.GRPCClient<Transport>
  84. /// Creates a new client wrapping the provided `GRPCCore.GRPCClient`.
  85. ///
  86. /// - Parameters:
  87. /// - client: A `GRPCCore.GRPCClient` providing a communication channel to the service.
  88. public init(wrapping client: GRPCCore.GRPCClient<Transport>) {
  89. self.client = client
  90. }
  91. /// Call the "MethodA" method.
  92. ///
  93. /// > Source IDL Documentation:
  94. /// >
  95. /// > Documentation for MethodA
  96. ///
  97. /// - Parameters:
  98. /// - request: A request containing a single `NamespaceA_ServiceARequest` message.
  99. /// - serializer: A serializer for `NamespaceA_ServiceARequest` messages.
  100. /// - deserializer: A deserializer for `NamespaceA_ServiceAResponse` messages.
  101. /// - options: Options to apply to this RPC.
  102. /// - handleResponse: A closure which handles the response, the result of which is
  103. /// returned to the caller. Returning from the closure will cancel the RPC if it
  104. /// hasn't already finished.
  105. /// - Returns: The result of `handleResponse`.
  106. public func methodA<Result>(
  107. request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
  108. serializer: some GRPCCore.MessageSerializer<NamespaceA_ServiceARequest>,
  109. deserializer: some GRPCCore.MessageDeserializer<NamespaceA_ServiceAResponse>,
  110. options: GRPCCore.CallOptions = .defaults,
  111. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<NamespaceA_ServiceAResponse>) async throws -> Result = { response in
  112. try response.message
  113. }
  114. ) async throws -> Result where Result: Sendable {
  115. try await self.client.unary(
  116. request: request,
  117. descriptor: NamespaceA_ServiceA.Method.MethodA.descriptor,
  118. serializer: serializer,
  119. deserializer: deserializer,
  120. options: options,
  121. onResponse: handleResponse
  122. )
  123. }
  124. }
  125. }
  126. // Helpers providing default arguments to 'ClientProtocol' methods.
  127. extension NamespaceA_ServiceA.ClientProtocol {
  128. /// Call the "MethodA" method.
  129. ///
  130. /// > Source IDL Documentation:
  131. /// >
  132. /// > Documentation for MethodA
  133. ///
  134. /// - Parameters:
  135. /// - request: A request containing a single `NamespaceA_ServiceARequest` message.
  136. /// - options: Options to apply to this RPC.
  137. /// - handleResponse: A closure which handles the response, the result of which is
  138. /// returned to the caller. Returning from the closure will cancel the RPC if it
  139. /// hasn't already finished.
  140. /// - Returns: The result of `handleResponse`.
  141. public func methodA<Result>(
  142. request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
  143. options: GRPCCore.CallOptions = .defaults,
  144. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<NamespaceA_ServiceAResponse>) async throws -> Result = { response in
  145. try response.message
  146. }
  147. ) async throws -> Result where Result: Sendable {
  148. try await self.methodA(
  149. request: request,
  150. serializer: GRPCProtobuf.ProtobufSerializer<NamespaceA_ServiceARequest>(),
  151. deserializer: GRPCProtobuf.ProtobufDeserializer<NamespaceA_ServiceAResponse>(),
  152. options: options,
  153. onResponse: handleResponse
  154. )
  155. }
  156. }
  157. // Helpers providing sugared APIs for 'ClientProtocol' methods.
  158. extension NamespaceA_ServiceA.ClientProtocol {
  159. /// Call the "MethodA" method.
  160. ///
  161. /// > Source IDL Documentation:
  162. /// >
  163. /// > Documentation for MethodA
  164. ///
  165. /// - Parameters:
  166. /// - message: request message to send.
  167. /// - metadata: Additional metadata to send, defaults to empty.
  168. /// - options: Options to apply to this RPC, defaults to `.defaults`.
  169. /// - handleResponse: A closure which handles the response, the result of which is
  170. /// returned to the caller. Returning from the closure will cancel the RPC if it
  171. /// hasn't already finished.
  172. /// - Returns: The result of `handleResponse`.
  173. public func methodA<Result>(
  174. _ message: NamespaceA_ServiceARequest,
  175. metadata: GRPCCore.Metadata = [:],
  176. options: GRPCCore.CallOptions = .defaults,
  177. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<NamespaceA_ServiceAResponse>) async throws -> Result = { response in
  178. try response.message
  179. }
  180. ) async throws -> Result where Result: Sendable {
  181. let request = GRPCCore.ClientRequest<NamespaceA_ServiceARequest>(
  182. message: message,
  183. metadata: metadata
  184. )
  185. return try await self.methodA(
  186. request: request,
  187. options: options,
  188. onResponse: handleResponse
  189. )
  190. }
  191. }
  192. """
  193. let rendered = self.render(accessLevel: .public, service: service)
  194. #expect(rendered == expectedSwift)
  195. }
  196. private func render(
  197. accessLevel: AccessModifier,
  198. service: ServiceDescriptor
  199. ) -> String {
  200. let translator = ClientCodeTranslator()
  201. let codeBlocks = translator.translate(accessModifier: accessLevel, service: service) {
  202. "GRPCProtobuf.ProtobufSerializer<\($0)>()"
  203. } deserializer: {
  204. "GRPCProtobuf.ProtobufDeserializer<\($0)>()"
  205. }
  206. let renderer = TextBasedRenderer.default
  207. renderer.renderCodeBlocks(codeBlocks)
  208. return renderer.renderedContents()
  209. }
  210. }