Options.swift 7.0 KB

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