Generator-Server.swift 6.9 KB

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