Generator-Server.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. }
  23. private func printServerProtocol() {
  24. println("/// To build a server, implement a class that conforms to this protocol.")
  25. println("\(access) protocol \(providerName): CallHandlerProvider {")
  26. indent()
  27. for method in service.methods {
  28. self.method = method
  29. switch streamingType(method) {
  30. case .unary:
  31. println(self.method.protoSourceComments(), newline: false)
  32. println(
  33. "func \(methodFunctionName)(request: \(methodInputName), context: StatusOnlyCallContext) -> EventLoopFuture<\(methodOutputName)>"
  34. )
  35. case .serverStreaming:
  36. println(self.method.protoSourceComments(), newline: false)
  37. println(
  38. "func \(methodFunctionName)(request: \(methodInputName), context: StreamingResponseCallContext<\(methodOutputName)>) -> EventLoopFuture<GRPCStatus>"
  39. )
  40. case .clientStreaming:
  41. println(self.method.protoSourceComments(), newline: false)
  42. println(
  43. "func \(methodFunctionName)(context: UnaryResponseCallContext<\(methodOutputName)>) -> EventLoopFuture<(StreamEvent<\(methodInputName)>) -> Void>"
  44. )
  45. case .bidirectionalStreaming:
  46. println(self.method.protoSourceComments(), newline: false)
  47. println(
  48. "func \(methodFunctionName)(context: StreamingResponseCallContext<\(methodOutputName)>) -> EventLoopFuture<(StreamEvent<\(methodInputName)>) -> Void>"
  49. )
  50. }
  51. }
  52. outdent()
  53. println("}")
  54. println()
  55. println("extension \(providerName) {")
  56. indent()
  57. println("\(access) var serviceName: Substring { return \"\(servicePath)\" }")
  58. println()
  59. println(
  60. "/// Determines, calls and returns the appropriate request handler, depending on the request's method."
  61. )
  62. println("/// Returns nil for methods not handled by this service.")
  63. println(
  64. "\(access) func handleMethod(_ methodName: Substring, callHandlerContext: CallHandlerContext) -> GRPCCallHandler? {"
  65. )
  66. indent()
  67. println("switch methodName {")
  68. for method in service.methods {
  69. self.method = method
  70. println("case \"\(method.name)\":")
  71. indent()
  72. let callHandlerType: String
  73. switch streamingType(method) {
  74. case .unary: callHandlerType = "CallHandlerFactory.makeUnary"
  75. case .serverStreaming: callHandlerType = "CallHandlerFactory.makeServerStreaming"
  76. case .clientStreaming: callHandlerType = "CallHandlerFactory.makeClientStreaming"
  77. case .bidirectionalStreaming: callHandlerType =
  78. "CallHandlerFactory.makeBidirectionalStreaming"
  79. }
  80. println("return \(callHandlerType)(callHandlerContext: callHandlerContext) { context in")
  81. indent()
  82. switch streamingType(method) {
  83. case .unary, .serverStreaming:
  84. println("return { request in")
  85. indent()
  86. println("self.\(methodFunctionName)(request: request, context: context)")
  87. outdent()
  88. println("}")
  89. case .clientStreaming, .bidirectionalStreaming:
  90. println("return self.\(methodFunctionName)(context: context)")
  91. }
  92. outdent()
  93. println("}")
  94. outdent()
  95. println()
  96. }
  97. println("default: return nil")
  98. println("}")
  99. outdent()
  100. println("}")
  101. outdent()
  102. println("}")
  103. println()
  104. }
  105. }