CodeGenerator.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2024, 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. @available(*, deprecated, renamed: "CodeGenerator")
  17. public typealias SourceGenerator = CodeGenerator
  18. /// Generates ``SourceFile`` objects containing generated code for the RPCs represented
  19. /// in a ``CodeGenerationRequest`` object.
  20. public struct CodeGenerator: Sendable {
  21. /// The options regarding the access level, indentation for the generated code
  22. /// and whether to generate server and client code.
  23. public var config: Config
  24. public init(config: Config) {
  25. self.config = config
  26. }
  27. /// User options for the CodeGeneration.
  28. public struct Config: Sendable {
  29. /// The access level the generated code will have.
  30. public var accessLevel: AccessLevel
  31. /// Whether imports have explicit access levels.
  32. public var accessLevelOnImports: Bool
  33. /// The indentation of the generated code as the number of spaces.
  34. public var indentation: Int
  35. /// Whether or not client code should be generated.
  36. public var client: Bool
  37. /// Whether or not server code should be generated.
  38. public var server: Bool
  39. /// The name of the core gRPC module.
  40. public var grpcCoreModuleName: String
  41. /// Creates a new configuration.
  42. ///
  43. /// - Parameters:
  44. /// - accessLevel: The access level the generated code will have.
  45. /// - accessLevelOnImports: Whether imports have explicit access levels.
  46. /// - client: Whether or not client code should be generated.
  47. /// - server: Whether or not server code should be generated.
  48. /// - indentation: The indentation of the generated code as the number of spaces.
  49. public init(
  50. accessLevel: AccessLevel,
  51. accessLevelOnImports: Bool,
  52. client: Bool,
  53. server: Bool,
  54. indentation: Int = 4
  55. ) {
  56. self.accessLevel = accessLevel
  57. self.accessLevelOnImports = accessLevelOnImports
  58. self.indentation = indentation
  59. self.client = client
  60. self.server = server
  61. self.grpcCoreModuleName = "GRPCCore"
  62. }
  63. /// The possible access levels for the generated code.
  64. public struct AccessLevel: Sendable, Hashable {
  65. package var level: Level
  66. package enum Level {
  67. case `internal`
  68. case `public`
  69. case `package`
  70. }
  71. /// The generated code will have `internal` access level.
  72. public static var `internal`: Self { Self(level: .`internal`) }
  73. /// The generated code will have `public` access level.
  74. public static var `public`: Self { Self(level: .`public`) }
  75. /// The generated code will have `package` access level.
  76. public static var `package`: Self { Self(level: .`package`) }
  77. }
  78. }
  79. /// Transforms a ``CodeGenerationRequest`` object into a ``SourceFile`` object containing
  80. /// the generated code.
  81. public func generate(
  82. _ request: CodeGenerationRequest
  83. ) throws -> SourceFile {
  84. let translator = IDLToStructuredSwiftTranslator()
  85. let textRenderer = TextBasedRenderer(indentation: self.config.indentation)
  86. let structuredSwiftRepresentation = try translator.translate(
  87. codeGenerationRequest: request,
  88. accessLevel: self.config.accessLevel,
  89. accessLevelOnImports: self.config.accessLevelOnImports,
  90. client: self.config.client,
  91. server: self.config.server,
  92. grpcCoreModuleName: self.config.grpcCoreModuleName
  93. )
  94. let sourceFile = try textRenderer.render(structured: structuredSwiftRepresentation)
  95. return sourceFile
  96. }
  97. }