CodeGenerationRequest.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. /// The name of the private interface for an `@_spi` import.
  82. ///
  83. /// For example, if `spi` was "Secret" and the module name was "Foo" then the import
  84. /// would be `@_spi(Secret) import Foo`.
  85. public var spi: String?
  86. /// Requirements for the `@preconcurrency` attribute.
  87. public var preconcurrency: PreconcurrencyRequirement
  88. public init(
  89. item: Item? = nil,
  90. module: String,
  91. spi: String? = nil,
  92. preconcurrency: PreconcurrencyRequirement = .notRequired
  93. ) {
  94. self.item = item
  95. self.module = module
  96. self.spi = spi
  97. self.preconcurrency = preconcurrency
  98. }
  99. /// Represents an item imported from a module.
  100. public struct Item {
  101. /// The keyword that specifies the item's kind (e.g. `func`, `struct`).
  102. public var kind: Kind
  103. /// The name of the imported item.
  104. public var name: String
  105. public init(kind: Kind, name: String) {
  106. self.kind = kind
  107. self.name = name
  108. }
  109. /// Represents the imported item's kind.
  110. public struct Kind {
  111. /// Describes the keyword associated with the imported item.
  112. internal enum Value: String {
  113. case `typealias`
  114. case `struct`
  115. case `class`
  116. case `enum`
  117. case `protocol`
  118. case `let`
  119. case `var`
  120. case `func`
  121. }
  122. internal var value: Value
  123. internal init(_ value: Value) {
  124. self.value = value
  125. }
  126. /// The imported item is a typealias.
  127. public static var `typealias`: Self {
  128. Self(.`typealias`)
  129. }
  130. /// The imported item is a struct.
  131. public static var `struct`: Self {
  132. Self(.`struct`)
  133. }
  134. /// The imported item is a class.
  135. public static var `class`: Self {
  136. Self(.`class`)
  137. }
  138. /// The imported item is an enum.
  139. public static var `enum`: Self {
  140. Self(.`enum`)
  141. }
  142. /// The imported item is a protocol.
  143. public static var `protocol`: Self {
  144. Self(.`protocol`)
  145. }
  146. /// The imported item is a let.
  147. public static var `let`: Self {
  148. Self(.`let`)
  149. }
  150. /// The imported item is a var.
  151. public static var `var`: Self {
  152. Self(.`var`)
  153. }
  154. /// The imported item is a function.
  155. public static var `func`: Self {
  156. Self(.`func`)
  157. }
  158. }
  159. }
  160. /// Describes any requirement for the `@preconcurrency` attribute.
  161. public struct PreconcurrencyRequirement {
  162. internal enum Value {
  163. case required
  164. case notRequired
  165. case requiredOnOS([String])
  166. }
  167. internal var value: Value
  168. internal init(_ value: Value) {
  169. self.value = value
  170. }
  171. /// The attribute is always required.
  172. public static var required: Self {
  173. Self(.required)
  174. }
  175. /// The attribute is not required.
  176. public static var notRequired: Self {
  177. Self(.notRequired)
  178. }
  179. /// The attribute is required only on the named operating systems.
  180. public static func requiredOnOS(_ OSs: [String]) -> PreconcurrencyRequirement {
  181. return Self(.requiredOnOS(OSs))
  182. }
  183. }
  184. }
  185. /// Represents a service described in an IDL file.
  186. public struct ServiceDescriptor {
  187. /// Documentation from comments above the IDL service description.
  188. public var documentation: String
  189. /// Service name.
  190. public var name: String
  191. /// The service namespace.
  192. ///
  193. /// For `.proto` files it is the package name.
  194. public var namespace: String
  195. /// A description of each method of a service.
  196. ///
  197. /// - SeeAlso: ``MethodDescriptor``.
  198. public var methods: [MethodDescriptor]
  199. public init(
  200. documentation: String,
  201. name: String,
  202. namespace: String,
  203. methods: [MethodDescriptor]
  204. ) {
  205. self.documentation = documentation
  206. self.name = name
  207. self.namespace = namespace
  208. self.methods = methods
  209. }
  210. /// Represents a method described in an IDL file.
  211. public struct MethodDescriptor {
  212. /// Documentation from comments above the IDL method description.
  213. public var documentation: String
  214. /// Method name.
  215. public var name: String
  216. /// Identifies if the method is input streaming.
  217. public var isInputStreaming: Bool
  218. /// Identifies if the method is output streaming.
  219. public var isOutputStreaming: Bool
  220. /// The generated input type for the described method.
  221. public var inputType: String
  222. /// The generated output type for the described method.
  223. public var outputType: String
  224. public init(
  225. documentation: String,
  226. name: String,
  227. isInputStreaming: Bool,
  228. isOutputStreaming: Bool,
  229. inputType: String,
  230. outputType: String
  231. ) {
  232. self.documentation = documentation
  233. self.name = name
  234. self.isInputStreaming = isInputStreaming
  235. self.isOutputStreaming = isOutputStreaming
  236. self.inputType = inputType
  237. self.outputType = outputType
  238. }
  239. }
  240. }
  241. }