options.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 .unknownParameter(let name):
  28. return "Unknown generation parameter '\(name)'"
  29. case .invalidParameterValue(let name, let value):
  30. return "Unknown value for generation parameter '\(name)': '\(value)'"
  31. case .wrappedError(let message, let error):
  32. return "\(message): \(error.localizedDescription)"
  33. }
  34. }
  35. }
  36. final class GeneratorOptions {
  37. enum Visibility: String {
  38. case `internal` = "Internal"
  39. case `public` = "Public"
  40. var sourceSnippet: String {
  41. switch self {
  42. case .internal:
  43. return "internal"
  44. case .public:
  45. return "public"
  46. }
  47. }
  48. }
  49. private(set) var visibility = Visibility.internal
  50. private(set) var generateServer = true
  51. private(set) var generateClient = true
  52. private(set) var generateAsynchronous = true
  53. private(set) var generateSynchronous = true
  54. private(set) var generateTestStubs = false
  55. private(set) var generateNIOImplementation = false
  56. private(set) var protoToModuleMappings = ProtoFileToModuleMappings()
  57. private(set) var fileNaming = FileNaming.FullPath
  58. init(parameter: String?) throws {
  59. for pair in GeneratorOptions.parseParameter(string: parameter) {
  60. switch pair.key {
  61. case "Visibility":
  62. if let value = Visibility(rawValue: pair.value) {
  63. visibility = value
  64. } else {
  65. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  66. }
  67. case "Server":
  68. if let value = Bool(pair.value) {
  69. generateServer = value
  70. } else {
  71. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  72. }
  73. case "Client":
  74. if let value = Bool(pair.value) {
  75. generateClient = value
  76. } else {
  77. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  78. }
  79. case "Async":
  80. if let value = Bool(pair.value) {
  81. generateAsynchronous = value
  82. } else {
  83. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  84. }
  85. case "Sync":
  86. if let value = Bool(pair.value) {
  87. generateSynchronous = value
  88. } else {
  89. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  90. }
  91. case "TestStubs":
  92. if let value = Bool(pair.value) {
  93. generateTestStubs = value
  94. } else {
  95. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  96. }
  97. case "NIO":
  98. if let value = Bool(pair.value) {
  99. generateNIOImplementation = value
  100. } else {
  101. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  102. }
  103. case "ProtoPathModuleMappings":
  104. if !pair.value.isEmpty {
  105. do {
  106. protoToModuleMappings = try ProtoFileToModuleMappings(path: pair.value)
  107. } catch let e {
  108. throw GenerationError.wrappedError(
  109. message: "Parameter 'ProtoPathModuleMappings=\(pair.value)'",
  110. error: e)
  111. }
  112. }
  113. case "FileNaming":
  114. if let value = FileNaming(rawValue: pair.value) {
  115. fileNaming = value
  116. } else {
  117. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  118. }
  119. default:
  120. throw GenerationError.unknownParameter(name: pair.key)
  121. }
  122. }
  123. }
  124. static func parseParameter(string: String?) -> [(key: String, value: String)] {
  125. guard let string = string, !string.isEmpty else {
  126. return []
  127. }
  128. let parts = string.components(separatedBy: ",")
  129. // Partitions the string into the section before the = and after the =
  130. let result = parts.map { string -> (key: String, value: String) in
  131. // Finds the equal sign and exits early if none
  132. guard let index = string.range(of: "=")?.lowerBound else {
  133. return (string, "")
  134. }
  135. // Creates key/value pair and trims whitespace
  136. let key = string[..<index]
  137. .trimmingCharacters(in: .whitespacesAndNewlines)
  138. let value = string[string.index(after: index)...]
  139. .trimmingCharacters(in: .whitespacesAndNewlines)
  140. return (key: key, value: value)
  141. }
  142. return result
  143. }
  144. }