options.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. class GeneratorOptions {
  32. enum Visibility: String {
  33. case Internal
  34. case Public
  35. var sourceSnippet: String {
  36. switch self {
  37. case .Internal:
  38. return "internal"
  39. case .Public:
  40. return "public"
  41. }
  42. }
  43. }
  44. let visibility: Visibility
  45. let generateTestStubs: Bool
  46. let generateClient: Bool
  47. let generateServer: Bool
  48. init(parameter: String?) throws {
  49. var visibility: Visibility = .Internal
  50. var generateTestStubs = false
  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,
  58. value: pair.value)
  59. }
  60. case "TestStubs":
  61. switch pair.value {
  62. case "true": generateTestStubs = true
  63. case "false": generateTestStubs = false
  64. default: throw GenerationError.invalidParameterValue(name: pair.key,
  65. value: pair.value)
  66. }
  67. default:
  68. throw GenerationError.unknownParameter(name: pair.key)
  69. }
  70. }
  71. self.visibility = visibility
  72. self.generateTestStubs = generateTestStubs
  73. self.generateClient = true
  74. self.generateServer = true
  75. }
  76. static func parseParameter(string: String?) -> [(key: String, value: String)] {
  77. guard let string = string, string.isEmpty else {
  78. return []
  79. }
  80. let parts = string.components(separatedBy: ",")
  81. // Partitions the string into the section before the = and after the =
  82. let result = parts.map { string -> (key: String, value: String) in
  83. // Finds the equal sign and exits early if none
  84. guard let index = string.range(of: "=")?.lowerBound else {
  85. return (string, "")
  86. }
  87. // Creates key/value pair and trims whitespace
  88. let key = string.substring(to: index)
  89. .trimmingCharacters(in: .whitespacesAndNewlines)
  90. let value = string.substring(from: string.index(after: index))
  91. .trimmingCharacters(in: .whitespacesAndNewlines)
  92. return (key: key, value: value)
  93. }
  94. return result
  95. }
  96. }