options.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. init(parameter: String?) throws {
  47. var visibility: Visibility = .Internal
  48. var generateTestStubs = false
  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,
  56. value: pair.value)
  57. }
  58. case "TestStubs":
  59. switch pair.value {
  60. case "true": generateTestStubs = true
  61. case "false": generateTestStubs = false
  62. default: throw GenerationError.invalidParameterValue(name: pair.key,
  63. value: pair.value)
  64. }
  65. default:
  66. throw GenerationError.unknownParameter(name: pair.key)
  67. }
  68. }
  69. self.visibility = visibility
  70. self.generateTestStubs = generateTestStubs
  71. }
  72. static func parseParameter(string: String?) -> [(key: String, value: String)] {
  73. guard let string = string, string.characters.count > 0 else {
  74. return []
  75. }
  76. let parts = string.components(separatedBy: ",")
  77. // Partitions the string into the section before the = and after the =
  78. let result = parts.map { string -> (key: String, value: String) in
  79. // Finds the equal sign and exits early if none
  80. guard let index = string.range(of: "=")?.lowerBound else {
  81. return (string, "")
  82. }
  83. // Creates key/value pair and trims whitespace
  84. let key = string.substring(to: index)
  85. .trimmingCharacters(in: .whitespacesAndNewlines)
  86. let value = string.substring(from: string.index(after: index))
  87. .trimmingCharacters(in: .whitespacesAndNewlines)
  88. return (key: key, value: value)
  89. }
  90. return result
  91. }
  92. }