Options.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright 2017, 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 SwiftProtobufPluginLibrary
  18. enum GenerationError: Error {
  19. /// Raised when parsing the parameter string and found an unknown key
  20. case unknownParameter(name: String)
  21. /// Raised when a parameter was giving an invalid value
  22. case invalidParameterValue(name: String, value: String)
  23. /// Raised to wrap another error but provide a context message.
  24. case wrappedError(message: String, error: Error)
  25. var localizedDescription: String {
  26. switch self {
  27. case let .unknownParameter(name):
  28. return "Unknown generation parameter '\(name)'"
  29. case let .invalidParameterValue(name, value):
  30. return "Unknown value for generation parameter '\(name)': '\(value)'"
  31. case let .wrappedError(message, error):
  32. return "\(message): \(error.localizedDescription)"
  33. }
  34. }
  35. }
  36. enum FileNaming: String {
  37. case fullPath = "FullPath"
  38. case pathToUnderscores = "PathToUnderscores"
  39. case dropPath = "DropPath"
  40. }
  41. struct GeneratorOptions {
  42. enum Visibility: String {
  43. case `internal` = "Internal"
  44. case `public` = "Public"
  45. case `package` = "Package"
  46. var sourceSnippet: String {
  47. switch self {
  48. case .internal:
  49. return "internal"
  50. case .public:
  51. return "public"
  52. case .package:
  53. return "package"
  54. }
  55. }
  56. }
  57. private(set) var visibility = Visibility.internal
  58. private(set) var generateServer = true
  59. private(set) var generateClient = true
  60. private(set) var generateTestClient = false
  61. private(set) var keepMethodCasing = false
  62. private(set) var protoToModuleMappings = ProtoFileToModuleMappings()
  63. private(set) var fileNaming = FileNaming.fullPath
  64. private(set) var extraModuleImports: [String] = []
  65. private(set) var gRPCModuleName = "GRPC"
  66. private(set) var swiftProtobufModuleName = "SwiftProtobuf"
  67. private(set) var generateReflectionData = false
  68. #if compiler(>=6.0)
  69. private(set) var v2 = false
  70. #endif
  71. private(set) var useAccessLevelOnImports = false
  72. init(parameter: any CodeGeneratorParameter) throws {
  73. try self.init(pairs: parameter.parsedPairs)
  74. }
  75. init(pairs: [(key: String, value: String)]) throws {
  76. for pair in pairs {
  77. switch pair.key {
  78. case "Visibility":
  79. if let value = Visibility(rawValue: pair.value) {
  80. self.visibility = value
  81. } else {
  82. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  83. }
  84. case "Server":
  85. if let value = Bool(pair.value) {
  86. self.generateServer = value
  87. } else {
  88. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  89. }
  90. case "Client":
  91. if let value = Bool(pair.value) {
  92. self.generateClient = value
  93. } else {
  94. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  95. }
  96. case "TestClient":
  97. if let value = Bool(pair.value) {
  98. self.generateTestClient = value
  99. } else {
  100. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  101. }
  102. case "KeepMethodCasing":
  103. if let value = Bool(pair.value) {
  104. self.keepMethodCasing = value
  105. } else {
  106. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  107. }
  108. case "ProtoPathModuleMappings":
  109. if !pair.value.isEmpty {
  110. do {
  111. self.protoToModuleMappings = try ProtoFileToModuleMappings(path: pair.value)
  112. } catch let e {
  113. throw GenerationError.wrappedError(
  114. message: "Parameter 'ProtoPathModuleMappings=\(pair.value)'",
  115. error: e
  116. )
  117. }
  118. }
  119. case "FileNaming":
  120. if let value = FileNaming(rawValue: pair.value) {
  121. self.fileNaming = value
  122. } else {
  123. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  124. }
  125. case "ExtraModuleImports":
  126. if !pair.value.isEmpty {
  127. self.extraModuleImports.append(pair.value)
  128. } else {
  129. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  130. }
  131. case "GRPCModuleName":
  132. if !pair.value.isEmpty {
  133. self.gRPCModuleName = pair.value
  134. } else {
  135. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  136. }
  137. case "SwiftProtobufModuleName":
  138. if !pair.value.isEmpty {
  139. self.swiftProtobufModuleName = pair.value
  140. } else {
  141. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  142. }
  143. case "ReflectionData":
  144. if let value = Bool(pair.value) {
  145. self.generateReflectionData = value
  146. } else {
  147. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  148. }
  149. #if compiler(>=6.0)
  150. case "_V2":
  151. if let value = Bool(pair.value) {
  152. self.v2 = value
  153. } else {
  154. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  155. }
  156. #endif
  157. case "UseAccessLevelOnImports":
  158. if let value = Bool(pair.value) {
  159. self.useAccessLevelOnImports = value
  160. } else {
  161. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  162. }
  163. default:
  164. throw GenerationError.unknownParameter(name: pair.key)
  165. }
  166. }
  167. }
  168. static func parseParameter(string: String?) -> [(key: String, value: String)] {
  169. guard let string = string, !string.isEmpty else {
  170. return []
  171. }
  172. let parts = string.components(separatedBy: ",")
  173. // Partitions the string into the section before the = and after the =
  174. let result = parts.map { string -> (key: String, value: String) in
  175. // Finds the equal sign and exits early if none
  176. guard let index = string.range(of: "=")?.lowerBound else {
  177. return (string, "")
  178. }
  179. // Creates key/value pair and trims whitespace
  180. let key = string[..<index]
  181. .trimmingCharacters(in: .whitespacesAndNewlines)
  182. let value = string[string.index(after: index)...]
  183. .trimmingCharacters(in: .whitespacesAndNewlines)
  184. return (key: key, value: value)
  185. }
  186. return result
  187. }
  188. }