Generator-Client+AsyncAwait.swift 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright 2021, 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 SwiftProtobuf
  17. import SwiftProtobufPluginLibrary
  18. // MARK: - Client protocol
  19. extension Generator {
  20. internal func printAsyncServiceClientProtocol() {
  21. let comments = self.service.protoSourceComments()
  22. if !comments.isEmpty {
  23. // Source comments already have the leading '///'
  24. self.println(comments, newline: false)
  25. }
  26. self.printAvailabilityForAsyncAwait()
  27. self.println("\(self.access) protocol \(self.asyncClientProtocolName): GRPCClient {")
  28. self.withIndentation {
  29. self.println("static var serviceDescriptor: GRPCServiceDescriptor { get }")
  30. self.println("var interceptors: \(self.clientInterceptorProtocolName)? { get }")
  31. for method in service.methods {
  32. self.println()
  33. self.method = method
  34. let rpcType = streamingType(self.method)
  35. let callType = Types.call(for: rpcType)
  36. let arguments: [String]
  37. switch rpcType {
  38. case .unary, .serverStreaming:
  39. arguments = [
  40. "_ request: \(self.methodInputName)",
  41. "callOptions: \(Types.clientCallOptions)?",
  42. ]
  43. case .clientStreaming, .bidirectionalStreaming:
  44. arguments = [
  45. "callOptions: \(Types.clientCallOptions)?"
  46. ]
  47. }
  48. self.printFunction(
  49. name: self.methodMakeFunctionCallName,
  50. arguments: arguments,
  51. returnType: "\(callType)<\(self.methodInputName), \(self.methodOutputName)>",
  52. bodyBuilder: nil
  53. )
  54. }
  55. }
  56. self.println("}") // protocol
  57. }
  58. }
  59. // MARK: - Client protocol default implementation: Calls
  60. extension Generator {
  61. internal func printAsyncClientProtocolExtension() {
  62. self.printAvailabilityForAsyncAwait()
  63. self.withIndentation("extension \(self.asyncClientProtocolName)", braces: .curly) {
  64. // Service descriptor.
  65. self.withIndentation(
  66. "\(self.access) static var serviceDescriptor: GRPCServiceDescriptor",
  67. braces: .curly
  68. ) {
  69. self.println("return \(self.serviceClientMetadata).serviceDescriptor")
  70. }
  71. self.println()
  72. // Interceptor factory.
  73. self.withIndentation(
  74. "\(self.access) var interceptors: \(self.clientInterceptorProtocolName)?",
  75. braces: .curly
  76. ) {
  77. self.println("return nil")
  78. }
  79. // 'Unsafe' calls.
  80. for method in self.service.methods {
  81. self.println()
  82. self.method = method
  83. let rpcType = streamingType(self.method)
  84. printRPCFunctionImplementation(rpcType: rpcType)
  85. printRPCFunctionWrapper(rpcType: rpcType)
  86. }
  87. }
  88. }
  89. private func printRPCFunctionImplementation(rpcType: StreamingType) {
  90. let argumentsBuilder: (() -> Void)?
  91. switch rpcType {
  92. case .unary, .serverStreaming:
  93. argumentsBuilder = {
  94. self.println("request: request,")
  95. }
  96. default:
  97. argumentsBuilder = nil
  98. }
  99. let callTypeWithoutPrefix = Types.call(for: rpcType, withGRPCPrefix: false)
  100. printRPCFunction(rpcType: rpcType, name: self.methodMakeFunctionCallName) {
  101. self.withIndentation("return self.make\(callTypeWithoutPrefix)", braces: .round) {
  102. self.println("path: \(self.methodPathUsingClientMetadata),")
  103. argumentsBuilder?()
  104. self.println("callOptions: callOptions ?? self.defaultCallOptions,")
  105. self.println(
  106. "interceptors: self.interceptors?.\(self.methodInterceptorFactoryName)() ?? []"
  107. )
  108. }
  109. }
  110. }
  111. private func printRPCFunctionWrapper(rpcType: StreamingType) {
  112. let functionName = methodMakeFunctionCallName
  113. let functionWrapperName = methodMakeFunctionCallWrapperName
  114. guard functionName != functionWrapperName else { return }
  115. self.println()
  116. let argumentsBuilder: (() -> Void)?
  117. switch rpcType {
  118. case .unary, .serverStreaming:
  119. argumentsBuilder = {
  120. self.println("request,")
  121. }
  122. default:
  123. argumentsBuilder = nil
  124. }
  125. printRPCFunction(rpcType: rpcType, name: functionWrapperName) {
  126. self.withIndentation("return self.\(functionName)", braces: .round) {
  127. argumentsBuilder?()
  128. self.println("callOptions: callOptions")
  129. }
  130. }
  131. }
  132. private func printRPCFunction(rpcType: StreamingType, name: String, bodyBuilder: (() -> Void)?) {
  133. let callType = Types.call(for: rpcType)
  134. self.printFunction(
  135. name: name,
  136. arguments: rpcFunctionArguments(rpcType: rpcType),
  137. returnType: "\(callType)<\(self.methodInputName), \(self.methodOutputName)>",
  138. access: self.access,
  139. bodyBuilder: bodyBuilder
  140. )
  141. }
  142. private func rpcFunctionArguments(rpcType: StreamingType) -> [String] {
  143. var arguments = ["callOptions: \(Types.clientCallOptions)? = nil"]
  144. switch rpcType {
  145. case .unary, .serverStreaming:
  146. arguments.insert("_ request: \(self.methodInputName)", at: .zero)
  147. default:
  148. break
  149. }
  150. return arguments
  151. }
  152. }
  153. // MARK: - Client protocol extension: "Simple, but safe" call wrappers.
  154. extension Generator {
  155. internal func printAsyncClientProtocolSafeWrappersExtension() {
  156. self.printAvailabilityForAsyncAwait()
  157. self.withIndentation("extension \(self.asyncClientProtocolName)", braces: .curly) {
  158. for (i, method) in self.service.methods.enumerated() {
  159. self.method = method
  160. let rpcType = streamingType(self.method)
  161. let callTypeWithoutPrefix = Types.call(for: rpcType, withGRPCPrefix: false)
  162. let streamsResponses = [.serverStreaming, .bidirectionalStreaming].contains(rpcType)
  163. let streamsRequests = [.clientStreaming, .bidirectionalStreaming].contains(rpcType)
  164. // (protocol, requires sendable)
  165. let sequenceProtocols: [(String, Bool)?] =
  166. streamsRequests
  167. ? [("Sequence", false), ("AsyncSequence", true)]
  168. : [nil]
  169. for (j, sequenceProtocol) in sequenceProtocols.enumerated() {
  170. // Print a new line if this is not the first function in the extension.
  171. if i > 0 || j > 0 {
  172. self.println()
  173. }
  174. let functionName =
  175. streamsRequests
  176. ? "\(self.methodFunctionName)<RequestStream>"
  177. : self.methodFunctionName
  178. let requestParamName = streamsRequests ? "requests" : "request"
  179. let requestParamType = streamsRequests ? "RequestStream" : self.methodInputName
  180. let returnType =
  181. streamsResponses
  182. ? Types.responseStream(of: self.methodOutputName)
  183. : self.methodOutputName
  184. let maybeWhereClause = sequenceProtocol.map { protocolName, mustBeSendable -> String in
  185. let constraints = [
  186. "RequestStream: \(protocolName)" + (mustBeSendable ? " & Sendable" : ""),
  187. "RequestStream.Element == \(self.methodInputName)",
  188. ]
  189. return "where " + constraints.joined(separator: ", ")
  190. }
  191. self.printFunction(
  192. name: functionName,
  193. arguments: [
  194. "_ \(requestParamName): \(requestParamType)",
  195. "callOptions: \(Types.clientCallOptions)? = nil",
  196. ],
  197. returnType: returnType,
  198. access: self.access,
  199. async: !streamsResponses,
  200. throws: !streamsResponses,
  201. genericWhereClause: maybeWhereClause
  202. ) {
  203. self.withIndentation(
  204. "return\(!streamsResponses ? " try await" : "") self.perform\(callTypeWithoutPrefix)",
  205. braces: .round
  206. ) {
  207. self.println("path: \(self.methodPathUsingClientMetadata),")
  208. self.println("\(requestParamName): \(requestParamName),")
  209. self.println("callOptions: callOptions ?? self.defaultCallOptions,")
  210. self.println(
  211. "interceptors: self.interceptors?.\(self.methodInterceptorFactoryName)() ?? []"
  212. )
  213. }
  214. }
  215. }
  216. }
  217. }
  218. }
  219. }
  220. // MARK: - Client protocol implementation
  221. extension Generator {
  222. internal func printAsyncServiceClientImplementation() {
  223. self.printAvailabilityForAsyncAwait()
  224. self.withIndentation(
  225. "\(self.access) struct \(self.asyncClientStructName): \(self.asyncClientProtocolName)",
  226. braces: .curly
  227. ) {
  228. self.println("\(self.access) var channel: GRPCChannel")
  229. self.println("\(self.access) var defaultCallOptions: CallOptions")
  230. self.println("\(self.access) var interceptors: \(self.clientInterceptorProtocolName)?")
  231. self.println()
  232. self.println("\(self.access) init(")
  233. self.withIndentation {
  234. self.println("channel: GRPCChannel,")
  235. self.println("defaultCallOptions: CallOptions = CallOptions(),")
  236. self.println("interceptors: \(self.clientInterceptorProtocolName)? = nil")
  237. }
  238. self.println(") {")
  239. self.withIndentation {
  240. self.println("self.channel = channel")
  241. self.println("self.defaultCallOptions = defaultCallOptions")
  242. self.println("self.interceptors = interceptors")
  243. }
  244. self.println("}")
  245. }
  246. }
  247. }