2
0

options.swift 3.5 KB

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