options.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. init(parameter: String?) throws {
  51. for pair in GeneratorOptions.parseParameter(string: parameter) {
  52. switch pair.key {
  53. case "Visibility":
  54. if let value = Visibility(rawValue: pair.value) {
  55. visibility = value
  56. } else {
  57. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  58. }
  59. case "Server":
  60. if let value = Bool(pair.value) {
  61. generateServer = value
  62. } else {
  63. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  64. }
  65. case "Client":
  66. if let value = Bool(pair.value) {
  67. generateClient = value
  68. } else {
  69. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  70. }
  71. case "Async":
  72. if let value = Bool(pair.value) {
  73. generateAsynchronous = value
  74. } else {
  75. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  76. }
  77. case "Sync":
  78. if let value = Bool(pair.value) {
  79. generateSynchronous = value
  80. } else {
  81. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  82. }
  83. case "TestStubs":
  84. if let value = Bool(pair.value) {
  85. generateTestStubs = value
  86. } else {
  87. throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
  88. }
  89. default:
  90. throw GenerationError.unknownParameter(name: pair.key)
  91. }
  92. }
  93. }
  94. static func parseParameter(string: String?) -> [(key: String, value: String)] {
  95. guard let string = string, !string.isEmpty else {
  96. return []
  97. }
  98. let parts = string.components(separatedBy: ",")
  99. // Partitions the string into the section before the = and after the =
  100. let result = parts.map { string -> (key: String, value: String) in
  101. // Finds the equal sign and exits early if none
  102. guard let index = string.range(of: "=")?.lowerBound else {
  103. return (string, "")
  104. }
  105. // Creates key/value pair and trims whitespace
  106. let key = string[..<index]
  107. .trimmingCharacters(in: .whitespacesAndNewlines)
  108. let value = string[string.index(after: index)...]
  109. .trimmingCharacters(in: .whitespacesAndNewlines)
  110. return (key: key, value: value)
  111. }
  112. return result
  113. }
  114. }