ClientCodeTranslatorSnippetBasedTests.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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: Name(base: "MethodA", generatedUpperCase: "MethodA", generatedLowerCase: "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: Name(base: "ServiceA", generatedUpperCase: "ServiceA", generatedLowerCase: ""),
  33. namespace: Name(base: "namespaceA", generatedUpperCase: "NamespaceA", generatedLowerCase: ""),
  34. methods: [method]
  35. )
  36. let expectedSwift = """
  37. extension NamespaceA_ServiceA {
  38. /// Generated client protocol for the "namespaceA.ServiceA" service.
  39. ///
  40. /// You don't need to implement this protocol directly, use the generated
  41. /// implementation, ``Client``.
  42. ///
  43. /// > Source IDL Documentation:
  44. /// >
  45. /// > Documentation for ServiceA
  46. public protocol ClientProtocol: Sendable {
  47. /// Call the "MethodA" method.
  48. ///
  49. /// > Source IDL Documentation:
  50. /// >
  51. /// > Documentation for MethodA
  52. ///
  53. /// - Parameters:
  54. /// - request: A request containing a single `NamespaceA_ServiceARequest` message.
  55. /// - serializer: A serializer for `NamespaceA_ServiceARequest` messages.
  56. /// - deserializer: A deserializer for `NamespaceA_ServiceAResponse` messages.
  57. /// - options: Options to apply to this RPC.
  58. /// - handleResponse: A closure which handles the response, the result of which is
  59. /// returned to the caller. Returning from the closure will cancel the RPC if it
  60. /// hasn't already finished.
  61. /// - Returns: The result of `handleResponse`.
  62. func methodA<Result>(
  63. request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
  64. serializer: some GRPCCore.MessageSerializer<NamespaceA_ServiceARequest>,
  65. deserializer: some GRPCCore.MessageDeserializer<NamespaceA_ServiceAResponse>,
  66. options: GRPCCore.CallOptions,
  67. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<NamespaceA_ServiceAResponse>) async throws -> Result
  68. ) async throws -> Result where Result: Sendable
  69. }
  70. /// Generated client for the "namespaceA.ServiceA" service.
  71. ///
  72. /// The ``Client`` provides an implementation of ``ClientProtocol`` which wraps
  73. /// a `GRPCCore.GRPCCClient`. The underlying `GRPCClient` provides the long-lived
  74. /// means of communication with the remote peer.
  75. ///
  76. /// > Source IDL Documentation:
  77. /// >
  78. /// > Documentation for ServiceA
  79. public struct Client: ClientProtocol {
  80. private let client: GRPCCore.GRPCClient
  81. /// Creates a new client wrapping the provided `GRPCCore.GRPCClient`.
  82. ///
  83. /// - Parameters:
  84. /// - client: A `GRPCCore.GRPCClient` providing a communication channel to the service.
  85. public init(wrapping client: GRPCCore.GRPCClient) {
  86. self.client = client
  87. }
  88. /// Call the "MethodA" method.
  89. ///
  90. /// > Source IDL Documentation:
  91. /// >
  92. /// > Documentation for MethodA
  93. ///
  94. /// - Parameters:
  95. /// - request: A request containing a single `NamespaceA_ServiceARequest` message.
  96. /// - serializer: A serializer for `NamespaceA_ServiceARequest` messages.
  97. /// - deserializer: A deserializer for `NamespaceA_ServiceAResponse` messages.
  98. /// - options: Options to apply to this RPC.
  99. /// - handleResponse: A closure which handles the response, the result of which is
  100. /// returned to the caller. Returning from the closure will cancel the RPC if it
  101. /// hasn't already finished.
  102. /// - Returns: The result of `handleResponse`.
  103. public func methodA<Result>(
  104. request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
  105. serializer: some GRPCCore.MessageSerializer<NamespaceA_ServiceARequest>,
  106. deserializer: some GRPCCore.MessageDeserializer<NamespaceA_ServiceAResponse>,
  107. options: GRPCCore.CallOptions = .defaults,
  108. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<NamespaceA_ServiceAResponse>) async throws -> Result = { response in
  109. try response.message
  110. }
  111. ) async throws -> Result where Result: Sendable {
  112. try await self.client.unary(
  113. request: request,
  114. descriptor: NamespaceA_ServiceA.Method.MethodA.descriptor,
  115. serializer: serializer,
  116. deserializer: deserializer,
  117. options: options,
  118. onResponse: handleResponse
  119. )
  120. }
  121. }
  122. }
  123. // Helpers providing default arguments to 'ClientProtocol' methods.
  124. extension NamespaceA_ServiceA.ClientProtocol {
  125. /// Call the "MethodA" method.
  126. ///
  127. /// > Source IDL Documentation:
  128. /// >
  129. /// > Documentation for MethodA
  130. ///
  131. /// - Parameters:
  132. /// - request: A request containing a single `NamespaceA_ServiceARequest` message.
  133. /// - options: Options to apply to this RPC.
  134. /// - handleResponse: A closure which handles the response, the result of which is
  135. /// returned to the caller. Returning from the closure will cancel the RPC if it
  136. /// hasn't already finished.
  137. /// - Returns: The result of `handleResponse`.
  138. public func methodA<Result>(
  139. request: GRPCCore.ClientRequest<NamespaceA_ServiceARequest>,
  140. options: GRPCCore.CallOptions = .defaults,
  141. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<NamespaceA_ServiceAResponse>) async throws -> Result = { response in
  142. try response.message
  143. }
  144. ) async throws -> Result where Result: Sendable {
  145. try await self.methodA(
  146. request: request,
  147. serializer: GRPCProtobuf.ProtobufSerializer<NamespaceA_ServiceARequest>(),
  148. deserializer: GRPCProtobuf.ProtobufDeserializer<NamespaceA_ServiceAResponse>(),
  149. options: options,
  150. onResponse: handleResponse
  151. )
  152. }
  153. }
  154. // Helpers providing sugared APIs for 'ClientProtocol' methods.
  155. extension NamespaceA_ServiceA.ClientProtocol {
  156. /// Call the "MethodA" method.
  157. ///
  158. /// > Source IDL Documentation:
  159. /// >
  160. /// > Documentation for MethodA
  161. ///
  162. /// - Parameters:
  163. /// - message: request message to send.
  164. /// - metadata: Additional metadata to send, defaults to empty.
  165. /// - options: Options to apply to this RPC, defaults to `.defaults`.
  166. /// - handleResponse: A closure which handles the response, the result of which is
  167. /// returned to the caller. Returning from the closure will cancel the RPC if it
  168. /// hasn't already finished.
  169. /// - Returns: The result of `handleResponse`.
  170. public func methodA<Result>(
  171. _ message: NamespaceA_ServiceARequest,
  172. metadata: GRPCCore.Metadata = [:],
  173. options: GRPCCore.CallOptions = .defaults,
  174. onResponse handleResponse: @Sendable @escaping (GRPCCore.ClientResponse<NamespaceA_ServiceAResponse>) async throws -> Result = { response in
  175. try response.message
  176. }
  177. ) async throws -> Result where Result: Sendable {
  178. let request = GRPCCore.ClientRequest<NamespaceA_ServiceARequest>(
  179. message: message,
  180. metadata: metadata
  181. )
  182. return try await self.methodA(
  183. request: request,
  184. options: options,
  185. onResponse: handleResponse
  186. )
  187. }
  188. }
  189. """
  190. let rendered = self.render(accessLevel: .public, service: service)
  191. #expect(rendered == expectedSwift)
  192. }
  193. private func render(
  194. accessLevel: AccessModifier,
  195. service: ServiceDescriptor
  196. ) -> String {
  197. let translator = ClientCodeTranslator()
  198. let codeBlocks = translator.translate(accessModifier: accessLevel, service: service) {
  199. "GRPCProtobuf.ProtobufSerializer<\($0)>()"
  200. } deserializer: {
  201. "GRPCProtobuf.ProtobufDeserializer<\($0)>()"
  202. }
  203. let renderer = TextBasedRenderer.default
  204. renderer.renderCodeBlocks(codeBlocks)
  205. return renderer.renderedContents()
  206. }
  207. }