2
0

Generator-Client+AsyncAwait.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 implementation
  118. extension Generator {
  119. internal func printAsyncServiceClientImplementation() {
  120. self.printAvailabilityForAsyncAwait()
  121. self.withIndentation(
  122. "\(self.access) struct \(self.asyncClientClassName): \(self.asyncClientProtocolName)",
  123. braces: .curly
  124. ) {
  125. self.println("\(self.access) var channel: GRPCChannel")
  126. self.println("\(self.access) var defaultCallOptions: CallOptions")
  127. self.println("\(self.access) var interceptors: \(self.clientInterceptorProtocolName)?")
  128. self.println()
  129. self.println("\(self.access) init(")
  130. self.withIndentation {
  131. self.println("channel: GRPCChannel,")
  132. self.println("defaultCallOptions: CallOptions = CallOptions(),")
  133. self.println("interceptors: \(self.clientInterceptorProtocolName)? = nil")
  134. }
  135. self.println(") {")
  136. self.withIndentation {
  137. self.println("self.channel = channel")
  138. self.println("self.defaultCallOptions = defaultCallOptions")
  139. self.println("self.interceptors = interceptors")
  140. }
  141. self.println("}")
  142. }
  143. }
  144. }