Generator-Server.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright 2018, 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 Foundation
  17. import SwiftProtobuf
  18. import SwiftProtobufPluginLibrary
  19. extension Generator {
  20. internal func printServer() {
  21. self.printServerProtocol()
  22. self.println()
  23. self.printServerProtocolExtension()
  24. self.println()
  25. self.printServerInterceptorFactoryProtocol()
  26. }
  27. private func printServerProtocol() {
  28. let comments = self.service.protoSourceComments()
  29. if !comments.isEmpty {
  30. // Source comments already have the leading '///'
  31. self.println(comments, newline: false)
  32. self.println("///")
  33. }
  34. println("/// To build a server, implement a class that conforms to this protocol.")
  35. println("\(access) protocol \(providerName): CallHandlerProvider {")
  36. self.withIndentation {
  37. println("var interceptors: \(self.serverInterceptorProtocolName)? { get }")
  38. for method in service.methods {
  39. self.method = method
  40. self.println()
  41. switch streamingType(method) {
  42. case .unary:
  43. println(self.method.protoSourceComments(), newline: false)
  44. println(
  45. "func \(methodFunctionName)(request: \(methodInputName), context: StatusOnlyCallContext) -> EventLoopFuture<\(methodOutputName)>"
  46. )
  47. case .serverStreaming:
  48. println(self.method.protoSourceComments(), newline: false)
  49. println(
  50. "func \(methodFunctionName)(request: \(methodInputName), context: StreamingResponseCallContext<\(methodOutputName)>) -> EventLoopFuture<GRPCStatus>"
  51. )
  52. case .clientStreaming:
  53. println(self.method.protoSourceComments(), newline: false)
  54. println(
  55. "func \(methodFunctionName)(context: UnaryResponseCallContext<\(methodOutputName)>) -> EventLoopFuture<(StreamEvent<\(methodInputName)>) -> Void>"
  56. )
  57. case .bidirectionalStreaming:
  58. println(self.method.protoSourceComments(), newline: false)
  59. println(
  60. "func \(methodFunctionName)(context: StreamingResponseCallContext<\(methodOutputName)>) -> EventLoopFuture<(StreamEvent<\(methodInputName)>) -> Void>"
  61. )
  62. }
  63. }
  64. }
  65. println("}")
  66. }
  67. private func printServerProtocolExtension() {
  68. self.println("extension \(self.providerName) {")
  69. self.withIndentation {
  70. self.println("\(self.access) var serviceName: Substring { return \"\(self.servicePath)\" }")
  71. self.println()
  72. self.println(
  73. "/// Determines, calls and returns the appropriate request handler, depending on the request's method."
  74. )
  75. self.println("/// Returns nil for methods not handled by this service.")
  76. self.printFunction(
  77. name: "handleMethod",
  78. arguments: [
  79. "_ methodName: Substring",
  80. "callHandlerContext: CallHandlerContext",
  81. ],
  82. returnType: "GRPCCallHandler?",
  83. access: self.access
  84. ) {
  85. self.println("switch methodName {")
  86. for method in self.service.methods {
  87. self.method = method
  88. self.println("case \"\(method.name)\":")
  89. self.withIndentation {
  90. // Get the factory name.
  91. let callHandlerType: String
  92. switch streamingType(method) {
  93. case .unary:
  94. callHandlerType = "CallHandlerFactory.makeUnary"
  95. case .serverStreaming:
  96. callHandlerType = "CallHandlerFactory.makeServerStreaming"
  97. case .clientStreaming:
  98. callHandlerType = "CallHandlerFactory.makeClientStreaming"
  99. case .bidirectionalStreaming:
  100. callHandlerType = "CallHandlerFactory.makeBidirectionalStreaming"
  101. }
  102. self.println("return \(callHandlerType)(")
  103. self.withIndentation {
  104. self.println("callHandlerContext: callHandlerContext,")
  105. self.println(
  106. "interceptors: self.interceptors?.\(self.methodInterceptorFactoryName)() ?? []"
  107. )
  108. }
  109. self.println(") { context in")
  110. self.withIndentation {
  111. switch streamingType(self.method) {
  112. case .unary, .serverStreaming:
  113. self.println("return { request in")
  114. self.withIndentation {
  115. self.println(
  116. "self.\(self.methodFunctionName)(request: request, context: context)"
  117. )
  118. }
  119. self.println("}")
  120. case .clientStreaming, .bidirectionalStreaming:
  121. self.println("self.\(self.methodFunctionName)(context: context)")
  122. }
  123. }
  124. self.println("}")
  125. }
  126. self.println()
  127. }
  128. // Default case.
  129. self.println("default:")
  130. self.withIndentation {
  131. self.println("return nil")
  132. }
  133. self.println("}")
  134. }
  135. }
  136. self.println("}")
  137. }
  138. private func printServerInterceptorFactoryProtocol() {
  139. self.println("\(self.access) protocol \(self.serverInterceptorProtocolName) {")
  140. self.withIndentation {
  141. // Method specific interceptors.
  142. for method in service.methods {
  143. self.println()
  144. self.method = method
  145. self.println(
  146. "/// - Returns: Interceptors to use when handling '\(self.methodFunctionName)'."
  147. )
  148. self.println("/// Defaults to calling `self.makeInterceptors()`.")
  149. // Skip the access, we're defining a protocol.
  150. self.printMethodInterceptorFactory(access: nil)
  151. }
  152. }
  153. self.println("}")
  154. }
  155. private func printMethodInterceptorFactory(
  156. access: String?,
  157. bodyBuilder: (() -> Void)? = nil
  158. ) {
  159. self.printFunction(
  160. name: self.methodInterceptorFactoryName,
  161. arguments: [],
  162. returnType: "[ServerInterceptor<\(self.methodInputName), \(self.methodOutputName)>]",
  163. access: access,
  164. bodyBuilder: bodyBuilder
  165. )
  166. }
  167. func printServerInterceptorFactoryProtocolExtension() {
  168. self.println("extension \(self.serverInterceptorProtocolName) {")
  169. self.withIndentation {
  170. // Default interceptor factory.
  171. self.printFunction(
  172. name: "makeInterceptors<Request: SwiftProtobuf.Message, Response: SwiftProtobuf.Message>",
  173. arguments: [],
  174. returnType: "[ServerInterceptor<Request, Response>]",
  175. access: self.access
  176. ) {
  177. self.println("return []")
  178. }
  179. for method in self.service.methods {
  180. self.println()
  181. self.method = method
  182. self.printMethodInterceptorFactory(access: self.access) {
  183. self.println("return self.makeInterceptors()")
  184. }
  185. }
  186. }
  187. self.println("}")
  188. }
  189. }