Generator-Client+AsyncAwait.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. }
  99. }
  100. case .clientStreaming, .bidirectionalStreaming:
  101. self.printFunction(
  102. name: "make\(self.method.name)Call",
  103. arguments: ["callOptions: \(Types.clientCallOptions)? = nil"],
  104. returnType: "\(callType)<\(self.methodInputName), \(self.methodOutputName)>",
  105. access: self.access
  106. ) {
  107. self.withIndentation("return self.make\(callTypeWithoutPrefix)", braces: .round) {
  108. self.println("path: \(self.methodPath),")
  109. self.println("callOptions: callOptions ?? self.defaultCallOptions")
  110. }
  111. }
  112. }
  113. }
  114. }
  115. }
  116. }
  117. // MARK: - Client protocol extension: "Simple, but safe" call wrappers.
  118. extension Generator {
  119. internal func printAsyncClientProtocolSafeWrappersExtension() {
  120. self.printAvailabilityForAsyncAwait()
  121. self.withIndentation("extension \(self.asyncClientProtocolName)", braces: .curly) {
  122. for (i, method) in self.service.methods.enumerated() {
  123. self.method = method
  124. let rpcType = streamingType(self.method)
  125. let callTypeWithoutPrefix = Types.call(for: rpcType, withGRPCPrefix: false)
  126. let streamsResponses = [.serverStreaming, .bidirectionalStreaming].contains(rpcType)
  127. let streamsRequests = [.clientStreaming, .bidirectionalStreaming].contains(rpcType)
  128. let sequenceProtocols = streamsRequests ? ["Sequence", "AsyncSequence"] : [nil]
  129. for (j, sequenceProtocol) in sequenceProtocols.enumerated() {
  130. // Print a new line if this is not the first function in the extension.
  131. if i > 0 || j > 0 {
  132. self.println()
  133. }
  134. let functionName = streamsRequests
  135. ? "\(self.methodFunctionName)<RequestStream>"
  136. : self.methodFunctionName
  137. let requestParamName = streamsRequests ? "requests" : "request"
  138. let requestParamType = streamsRequests ? "RequestStream" : self.methodInputName
  139. let returnType = streamsResponses
  140. ? Types.responseStream(of: self.methodOutputName)
  141. : self.methodOutputName
  142. let maybeWhereClause = sequenceProtocol.map {
  143. "where RequestStream: \($0), RequestStream.Element == \(self.methodInputName)"
  144. }
  145. self.printFunction(
  146. name: functionName,
  147. arguments: [
  148. "_ \(requestParamName): \(requestParamType)",
  149. "callOptions: \(Types.clientCallOptions)? = nil",
  150. ],
  151. returnType: returnType,
  152. access: self.access,
  153. async: !streamsResponses,
  154. throws: !streamsResponses,
  155. genericWhereClause: maybeWhereClause
  156. ) {
  157. self.withIndentation(
  158. "return\(!streamsResponses ? " try await" : "") self.perform\(callTypeWithoutPrefix)",
  159. braces: .round
  160. ) {
  161. self.println("path: \(self.methodPath),")
  162. self.println("\(requestParamName): \(requestParamName),")
  163. self.println("callOptions: callOptions ?? self.defaultCallOptions")
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. }
  171. // MARK: - Client protocol implementation
  172. extension Generator {
  173. internal func printAsyncServiceClientImplementation() {
  174. self.printAvailabilityForAsyncAwait()
  175. self.withIndentation(
  176. "\(self.access) struct \(self.asyncClientClassName): \(self.asyncClientProtocolName)",
  177. braces: .curly
  178. ) {
  179. self.println("\(self.access) var channel: GRPCChannel")
  180. self.println("\(self.access) var defaultCallOptions: CallOptions")
  181. self.println("\(self.access) var interceptors: \(self.clientInterceptorProtocolName)?")
  182. self.println()
  183. self.println("\(self.access) init(")
  184. self.withIndentation {
  185. self.println("channel: GRPCChannel,")
  186. self.println("defaultCallOptions: CallOptions = CallOptions(),")
  187. self.println("interceptors: \(self.clientInterceptorProtocolName)? = nil")
  188. }
  189. self.println(") {")
  190. self.withIndentation {
  191. self.println("self.channel = channel")
  192. self.println("self.defaultCallOptions = defaultCallOptions")
  193. self.println("self.interceptors = interceptors")
  194. }
  195. self.println("}")
  196. }
  197. }
  198. }