CodeGenerationRequest.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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: Hashable {
  187. /// Documentation from comments above the IDL service description.
  188. public var documentation: String
  189. /// The service name in different formats.
  190. ///
  191. /// All properties of this object must be unique for each service from within a namespace.
  192. public var name: Name
  193. /// The service namespace in different formats.
  194. ///
  195. /// All different services from within the same namespace must have
  196. /// the same ``Name`` object as this property.
  197. /// For `.proto` files the base name of this object is the package name.
  198. public var namespace: Name
  199. /// A description of each method of a service.
  200. ///
  201. /// - SeeAlso: ``MethodDescriptor``.
  202. public var methods: [MethodDescriptor]
  203. public init(
  204. documentation: String,
  205. name: Name,
  206. namespace: Name,
  207. methods: [MethodDescriptor]
  208. ) {
  209. self.documentation = documentation
  210. self.name = name
  211. self.namespace = namespace
  212. self.methods = methods
  213. }
  214. /// Represents a method described in an IDL file.
  215. public struct MethodDescriptor: Hashable {
  216. /// Documentation from comments above the IDL method description.
  217. public var documentation: String
  218. /// Method name in different formats.
  219. ///
  220. /// All properties of this object must be unique for each method
  221. /// from within a service.
  222. public var name: Name
  223. /// Identifies if the method is input streaming.
  224. public var isInputStreaming: Bool
  225. /// Identifies if the method is output streaming.
  226. public var isOutputStreaming: Bool
  227. /// The generated input type for the described method.
  228. public var inputType: String
  229. /// The generated output type for the described method.
  230. public var outputType: String
  231. public init(
  232. documentation: String,
  233. name: Name,
  234. isInputStreaming: Bool,
  235. isOutputStreaming: Bool,
  236. inputType: String,
  237. outputType: String
  238. ) {
  239. self.documentation = documentation
  240. self.name = name
  241. self.isInputStreaming = isInputStreaming
  242. self.isOutputStreaming = isOutputStreaming
  243. self.inputType = inputType
  244. self.outputType = outputType
  245. }
  246. }
  247. }
  248. /// Represents the name associated with a namespace, service or a method, in three different formats.
  249. public struct Name: Hashable {
  250. /// The base name is the name used for the namespace/service/method in the IDL file, so it should follow
  251. /// the specific casing of the IDL.
  252. ///
  253. /// The base name is also used in the descriptors that identify a specific method or service :
  254. /// `<service_namespace_baseName>.<service_baseName>.<method_baseName>`.
  255. public var base: String
  256. /// The `generatedUpperCase` name is used in the generated code. It is expected
  257. /// to be the UpperCamelCase version of the base name
  258. ///
  259. /// For example, if `base` is "fooBar", then `generatedUpperCase` is "FooBar".
  260. public var generatedUpperCase: String
  261. /// The `generatedLowerCase` name is used in the generated code. It is expected
  262. /// to be the lowerCamelCase version of the base name
  263. ///
  264. /// For example, if `base` is "FooBar", then `generatedLowerCase` is "fooBar".
  265. public var generatedLowerCase: String
  266. public init(base: String, generatedUpperCase: String, generatedLowerCase: String) {
  267. self.base = base
  268. self.generatedUpperCase = generatedUpperCase
  269. self.generatedLowerCase = generatedLowerCase
  270. }
  271. }
  272. }