CodeGenerationRequest.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright 2023, 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. /// Describes the services, dependencies and trivia from an IDL file,
  17. /// and the IDL itself through its specific serializer and deserializer.
  18. public struct CodeGenerationRequest {
  19. /// The name of the source file containing the IDL, including the extension if applicable.
  20. public var fileName: String
  21. /// Any comments at the top of the file such as documentation and copyright headers.
  22. /// They will be placed at the top of the generated file.
  23. public var leadingTrivia: String
  24. /// The Swift imports that the generated file depends on. The gRPC specific imports aren't required
  25. /// as they will be added by default in the generated file.
  26. ///
  27. /// - SeeAlso: ``Dependency``.
  28. public var dependencies: [Dependency]
  29. /// A description of each service to generate.
  30. ///
  31. /// - SeeAlso: ``ServiceDescriptor``.
  32. public var services: [ServiceDescriptor]
  33. /// Closure that receives a message type as a `String` and returns a code snippet to
  34. /// initialise a `MessageSerializer` for that type as a `String`.
  35. ///
  36. /// The result is inserted in the generated code, where clients serialize RPC inputs and
  37. /// servers serialize RPC outputs.
  38. ///
  39. /// For example, to serialize Protobuf messages you could specify a serializer as:
  40. /// ```swift
  41. /// request.lookupSerializer = { messageType in
  42. /// "ProtobufSerializer<\(messageType)>()"
  43. /// }
  44. /// ```
  45. public var lookupSerializer: (_ messageType: String) -> String
  46. /// Closure that receives a message type as a `String` and returns a code snippet to
  47. /// initialize a `MessageDeserializer` for that type as a `String`.
  48. ///
  49. /// The result is inserted in the generated code, where clients deserialize RPC outputs and
  50. /// servers deserialize RPC inputs.
  51. ///
  52. /// For example, to serialize Protobuf messages you could specify a serializer as:
  53. /// ```swift
  54. /// request.lookupDeserializer = { messageType in
  55. /// "ProtobufDeserializer<\(messageType)>()"
  56. /// }
  57. /// ```
  58. public var lookupDeserializer: (_ messageType: String) -> String
  59. public init(
  60. fileName: String,
  61. leadingTrivia: String,
  62. dependencies: [Dependency],
  63. services: [ServiceDescriptor],
  64. lookupSerializer: @escaping (String) -> String,
  65. lookupDeserializer: @escaping (String) -> String
  66. ) {
  67. self.fileName = fileName
  68. self.leadingTrivia = leadingTrivia
  69. self.dependencies = dependencies
  70. self.services = services
  71. self.lookupSerializer = lookupSerializer
  72. self.lookupDeserializer = lookupDeserializer
  73. }
  74. /// Represents an import: a module or a specific item from a module.
  75. public struct Dependency {
  76. /// If the dependency is an item, the property's value is the item representation.
  77. /// If the dependency is a module, this property is nil.
  78. public var item: Item? = nil
  79. /// The name of the imported module or of the module an item is imported from.
  80. public var module: String
  81. public init(item: Item? = nil, module: String) {
  82. self.item = item
  83. self.module = module
  84. }
  85. /// Represents an item imported from a module.
  86. public struct Item {
  87. /// The keyword that specifies the item's kind (e.g. `func`, `struct`).
  88. public var kind: Kind
  89. /// The name of the imported item.
  90. public var name: String
  91. public init(kind: Kind, name: String) {
  92. self.kind = kind
  93. self.name = name
  94. }
  95. /// Represents the imported item's kind.
  96. public struct Kind {
  97. /// Describes the keyword associated with the imported item.
  98. internal enum Value {
  99. case `typealias`
  100. case `struct`
  101. case `class`
  102. case `enum`
  103. case `protocol`
  104. case `let`
  105. case `var`
  106. case `func`
  107. }
  108. internal var value: Value
  109. internal init(_ value: Value) {
  110. self.value = value
  111. }
  112. /// The imported item is a typealias.
  113. public static var `typealias`: Self {
  114. Self(.`typealias`)
  115. }
  116. /// The imported item is a struct.
  117. public static var `struct`: Self {
  118. Self(.`struct`)
  119. }
  120. /// The imported item is a class.
  121. public static var `class`: Self {
  122. Self(.`class`)
  123. }
  124. /// The imported item is an enum.
  125. public static var `enum`: Self {
  126. Self(.`enum`)
  127. }
  128. /// The imported item is a protocol.
  129. public static var `protocol`: Self {
  130. Self(.`protocol`)
  131. }
  132. /// The imported item is a let.
  133. public static var `let`: Self {
  134. Self(.`let`)
  135. }
  136. /// The imported item is a var.
  137. public static var `var`: Self {
  138. Self(.`var`)
  139. }
  140. /// The imported item is a function.
  141. public static var `func`: Self {
  142. Self(.`func`)
  143. }
  144. }
  145. }
  146. }
  147. /// Represents a service described in an IDL file.
  148. public struct ServiceDescriptor {
  149. /// Documentation from comments above the IDL service description.
  150. public var documentation: String
  151. /// Service name.
  152. public var name: String
  153. /// The service namespace.
  154. ///
  155. /// For `.proto` files it is the package name.
  156. public var namespace: String
  157. /// A description of each method of a service.
  158. ///
  159. /// - SeeAlso: ``MethodDescriptor``.
  160. public var methods: [MethodDescriptor]
  161. public init(
  162. documentation: String,
  163. name: String,
  164. namespace: String,
  165. methods: [MethodDescriptor]
  166. ) {
  167. self.documentation = documentation
  168. self.name = name
  169. self.namespace = namespace
  170. self.methods = methods
  171. }
  172. /// Represents a method described in an IDL file.
  173. public struct MethodDescriptor {
  174. /// Documentation from comments above the IDL method description.
  175. public var documentation: String
  176. /// Method name.
  177. public var name: String
  178. /// Identifies if the method is input streaming.
  179. public var isInputStreaming: Bool
  180. /// Identifies if the method is output streaming.
  181. public var isOutputStreaming: Bool
  182. /// The generated input type for the described method.
  183. public var inputType: String
  184. /// The generated output type for the described method.
  185. public var outputType: String
  186. public init(
  187. documentation: String,
  188. name: String,
  189. isInputStreaming: Bool,
  190. isOutputStreaming: Bool,
  191. inputType: String,
  192. outputType: String
  193. ) {
  194. self.documentation = documentation
  195. self.name = name
  196. self.isInputStreaming = isInputStreaming
  197. self.isOutputStreaming = isOutputStreaming
  198. self.inputType = inputType
  199. self.outputType = outputType
  200. }
  201. }
  202. }
  203. }