options.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. enum GenerationError: Error {
  18. /// Raised when parsing the parameter string and found an unknown key
  19. case unknownParameter(name: String)
  20. /// Raised when a parameter was giving an invalid value
  21. case invalidParameterValue(name: String, value: String)
  22. var localizedDescription: String {
  23. switch self {
  24. case .unknownParameter(let name):
  25. return "Unknown generation parameter '\(name)'"
  26. case .invalidParameterValue(let name, let value):
  27. return "Unknown value for generation parameter '\(name)': '\(value)'"
  28. }
  29. }
  30. }
  31. final class GeneratorOptions {
  32. enum Visibility: String {
  33. case `internal` = "Internal"
  34. case `public` = "Public"
  35. var sourceSnippet: String {
  36. switch self {
  37. case .internal:
  38. return "internal"
  39. case .public:
  40. return "public"
  41. }
  42. }
  43. }
  44. private(set) var visibility = Visibility.internal
  45. private(set) var generateServer = true
  46. private(set) var generateClient = true
  47. private(set) var generateAsynchronous = true
  48. private(set) var generateSynchronous = true
  49. private(set) var generateTestStubs = false
  50. private(set) var generateNIOImplementation = false
  51. init(parameter: String?) throws {
  52. for pair in GeneratorOptions.parseParameter(string: parameter) {
  53. switch pair.key {
  54. case "Visibility":
  55. if let value = Visibility(rawValue: pair.value) {
  56. visibility = value
  57. } else {
  58. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  59. }
  60. case "Server":
  61. if let value = Bool(pair.value) {
  62. generateServer = value
  63. } else {
  64. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  65. }
  66. case "Client":
  67. if let value = Bool(pair.value) {
  68. generateClient = value
  69. } else {
  70. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  71. }
  72. case "Async":
  73. if let value = Bool(pair.value) {
  74. generateAsynchronous = value
  75. } else {
  76. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  77. }
  78. case "Sync":
  79. if let value = Bool(pair.value) {
  80. generateSynchronous = value
  81. } else {
  82. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  83. }
  84. case "TestStubs":
  85. if let value = Bool(pair.value) {
  86. generateTestStubs = value
  87. } else {
  88. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  89. }
  90. case "NIO":
  91. if let value = Bool(pair.value) {
  92. generateNIOImplementation = value
  93. } else {
  94. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  95. }
  96. default:
  97. throw GenerationError.unknownParameter(name: pair.key)
  98. }
  99. }
  100. }
  101. static func parseParameter(string: String?) -> [(key: String, value: String)] {
  102. guard let string = string, !string.isEmpty else {
  103. return []
  104. }
  105. let parts = string.components(separatedBy: ",")
  106. // Partitions the string into the section before the = and after the =
  107. let result = parts.map { string -> (key: String, value: String) in
  108. // Finds the equal sign and exits early if none
  109. guard let index = string.range(of: "=")?.lowerBound else {
  110. return (string, "")
  111. }
  112. // Creates key/value pair and trims whitespace
  113. let key = string[..<index]
  114. .trimmingCharacters(in: .whitespacesAndNewlines)
  115. let value = string[string.index(after: index)...]
  116. .trimmingCharacters(in: .whitespacesAndNewlines)
  117. return (key: key, value: value)
  118. }
  119. return result
  120. }
  121. }