Generator-Client+AsyncAwait.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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("var serviceName: String { 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: "make\(self.method.name)Call",
  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 name. TODO: use static metadata.
  65. self.withIndentation("\(self.access) var serviceName: String", braces: .curly) {
  66. self.println("return \"\(self.servicePath)\"")
  67. }
  68. self.println()
  69. // Interceptor factory.
  70. self.withIndentation(
  71. "\(self.access) var interceptors: \(self.clientInterceptorProtocolName)?",
  72. braces: .curly
  73. ) {
  74. self.println("return nil")
  75. }
  76. // 'Unsafe' calls.
  77. for method in self.service.methods {
  78. self.println()
  79. self.method = method
  80. let rpcType = streamingType(self.method)
  81. let callType = Types.call(for: rpcType)
  82. let callTypeWithoutPrefix = Types.call(for: rpcType, withGRPCPrefix: false)
  83. switch rpcType {
  84. case .unary, .serverStreaming:
  85. self.printFunction(
  86. name: "make\(self.method.name)Call",
  87. arguments: [
  88. "_ request: \(self.methodInputName)",
  89. "callOptions: \(Types.clientCallOptions)? = nil",
  90. ],
  91. returnType: "\(callType)<\(self.methodInputName), \(self.methodOutputName)>",
  92. access: self.access
  93. ) {
  94. self.withIndentation("return self.make\(callTypeWithoutPrefix)", braces: .round) {
  95. self.println("path: \(self.methodPath),")
  96. self.println("request: request,")
  97. self.println("callOptions: callOptions ?? self.defaultCallOptions,")
  98. self.println(
  99. "interceptors: self.interceptors?.\(self.methodInterceptorFactoryName)() ?? []"
  100. )
  101. }
  102. }
  103. case .clientStreaming, .bidirectionalStreaming:
  104. self.printFunction(
  105. name: "make\(self.method.name)Call",
  106. arguments: ["callOptions: \(Types.clientCallOptions)? = nil"],
  107. returnType: "\(callType)<\(self.methodInputName), \(self.methodOutputName)>",
  108. access: self.access
  109. ) {
  110. self.withIndentation("return self.make\(callTypeWithoutPrefix)", braces: .round) {
  111. self.println("path: \(self.methodPath),")
  112. self.println("callOptions: callOptions ?? self.defaultCallOptions,")
  113. self.println(
  114. "interceptors: self.interceptors?.\(self.methodInterceptorFactoryName)() ?? []"
  115. )
  116. }
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. // MARK: - Client protocol extension: "Simple, but safe" call wrappers.
  124. extension Generator {
  125. internal func printAsyncClientProtocolSafeWrappersExtension() {
  126. self.printAvailabilityForAsyncAwait()
  127. self.withIndentation("extension \(self.asyncClientProtocolName)", braces: .curly) {
  128. for (i, method) in self.service.methods.enumerated() {
  129. self.method = method
  130. let rpcType = streamingType(self.method)
  131. let callTypeWithoutPrefix = Types.call(for: rpcType, withGRPCPrefix: false)
  132. let streamsResponses = [.serverStreaming, .bidirectionalStreaming].contains(rpcType)
  133. let streamsRequests = [.clientStreaming, .bidirectionalStreaming].contains(rpcType)
  134. let sequenceProtocols = streamsRequests ? ["Sequence", "AsyncSequence"] : [nil]
  135. for (j, sequenceProtocol) in sequenceProtocols.enumerated() {
  136. // Print a new line if this is not the first function in the extension.
  137. if i > 0 || j > 0 {
  138. self.println()
  139. }
  140. let functionName = streamsRequests
  141. ? "\(self.methodFunctionName)<RequestStream>"
  142. : self.methodFunctionName
  143. let requestParamName = streamsRequests ? "requests" : "request"
  144. let requestParamType = streamsRequests ? "RequestStream" : self.methodInputName
  145. let returnType = streamsResponses
  146. ? Types.responseStream(of: self.methodOutputName)
  147. : self.methodOutputName
  148. let maybeWhereClause = sequenceProtocol.map {
  149. "where RequestStream: \($0), RequestStream.Element == \(self.methodInputName)"
  150. }
  151. self.printFunction(
  152. name: functionName,
  153. arguments: [
  154. "_ \(requestParamName): \(requestParamType)",
  155. "callOptions: \(Types.clientCallOptions)? = nil",
  156. ],
  157. returnType: returnType,
  158. access: self.access,
  159. async: !streamsResponses,
  160. throws: !streamsResponses,
  161. genericWhereClause: maybeWhereClause
  162. ) {
  163. self.withIndentation(
  164. "return\(!streamsResponses ? " try await" : "") self.perform\(callTypeWithoutPrefix)",
  165. braces: .round
  166. ) {
  167. self.println("path: \(self.methodPath),")
  168. self.println("\(requestParamName): \(requestParamName),")
  169. self.println("callOptions: callOptions ?? self.defaultCallOptions,")
  170. self.println(
  171. "interceptors: self.interceptors?.\(self.methodInterceptorFactoryName)() ?? []"
  172. )
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. // MARK: - Client protocol implementation
  181. extension Generator {
  182. internal func printAsyncServiceClientImplementation() {
  183. self.printAvailabilityForAsyncAwait()
  184. self.withIndentation(
  185. "\(self.access) struct \(self.asyncClientClassName): \(self.asyncClientProtocolName)",
  186. braces: .curly
  187. ) {
  188. self.println("\(self.access) var channel: GRPCChannel")
  189. self.println("\(self.access) var defaultCallOptions: CallOptions")
  190. self.println("\(self.access) var interceptors: \(self.clientInterceptorProtocolName)?")
  191. self.println()
  192. self.println("\(self.access) init(")
  193. self.withIndentation {
  194. self.println("channel: GRPCChannel,")
  195. self.println("defaultCallOptions: CallOptions = CallOptions(),")
  196. self.println("interceptors: \(self.clientInterceptorProtocolName)? = nil")
  197. }
  198. self.println(") {")
  199. self.withIndentation {
  200. self.println("self.channel = channel")
  201. self.println("self.defaultCallOptions = defaultCallOptions")
  202. self.println("self.interceptors = interceptors")
  203. }
  204. self.println("}")
  205. }
  206. }
  207. }