Generator-Names.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2018, 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. import SwiftProtobuf
  18. import SwiftProtobufPluginLibrary
  19. internal func nameForPackageService(_ file: FileDescriptor,
  20. _ service: ServiceDescriptor) -> String {
  21. if !file.package.isEmpty {
  22. return SwiftProtobufNamer().typePrefix(forFile: file) + service.name
  23. } else {
  24. return service.name
  25. }
  26. }
  27. internal func nameForPackageServiceMethod(_ file: FileDescriptor,
  28. _ service: ServiceDescriptor,
  29. _ method: MethodDescriptor) -> String {
  30. return nameForPackageService(file, service) + method.name
  31. }
  32. private let swiftKeywordsUsedInDeclarations: Set<String> = [
  33. "associatedtype", "class", "deinit", "enum", "extension",
  34. "fileprivate", "func", "import", "init", "inout", "internal",
  35. "let", "open", "operator", "private", "protocol", "public",
  36. "static", "struct", "subscript", "typealias", "var",
  37. ]
  38. private let swiftKeywordsUsedInStatements: Set<String> = [
  39. "break", "case",
  40. "continue", "default", "defer", "do", "else", "fallthrough",
  41. "for", "guard", "if", "in", "repeat", "return", "switch", "where",
  42. "while",
  43. ]
  44. private let swiftKeywordsUsedInExpressionsAndTypes: Set<String> = [
  45. "as",
  46. "Any", "catch", "false", "is", "nil", "rethrows", "super", "self",
  47. "Self", "throw", "throws", "true", "try",
  48. ]
  49. private let quotableFieldNames: Set<String> = { () -> Set<String> in
  50. var names: Set<String> = []
  51. names = names.union(swiftKeywordsUsedInDeclarations)
  52. names = names.union(swiftKeywordsUsedInStatements)
  53. names = names.union(swiftKeywordsUsedInExpressionsAndTypes)
  54. return names
  55. }()
  56. extension Generator {
  57. internal var access: String {
  58. return options.visibility.sourceSnippet
  59. }
  60. internal var serviceClassName: String {
  61. return nameForPackageService(file, service) + "Service"
  62. }
  63. internal var providerName: String {
  64. return nameForPackageService(file, service) + "Provider"
  65. }
  66. internal var clientClassName: String {
  67. return nameForPackageService(file, service) + "Client"
  68. }
  69. internal var testClientClassName: String {
  70. return nameForPackageService(self.file, self.service) + "TestClient"
  71. }
  72. internal var clientProtocolName: String {
  73. return nameForPackageService(file, service) + "ClientProtocol"
  74. }
  75. internal var clientInterceptorProtocolName: String {
  76. return nameForPackageService(file, service) + "ClientInterceptorFactoryProtocol"
  77. }
  78. internal var serverInterceptorProtocolName: String {
  79. return nameForPackageService(file, service) + "ServerInterceptorFactoryProtocol"
  80. }
  81. internal var callName: String {
  82. return nameForPackageServiceMethod(file, service, method) + "Call"
  83. }
  84. internal var methodFunctionName: String {
  85. var name = method.name
  86. if !self.options.keepMethodCasing {
  87. name = name.prefix(1).lowercased() + name.dropFirst()
  88. }
  89. return self.sanitize(fieldName: name)
  90. }
  91. internal func sanitize(fieldName string: String) -> String {
  92. if quotableFieldNames.contains(string) {
  93. return "`\(string)`"
  94. }
  95. return string
  96. }
  97. internal var methodInputName: String {
  98. return protobufNamer.fullName(message: method.inputType)
  99. }
  100. internal var methodOutputName: String {
  101. return protobufNamer.fullName(message: method.outputType)
  102. }
  103. internal var methodInterceptorFactoryName: String {
  104. return "make\(self.method.name)Interceptors"
  105. }
  106. internal var servicePath: String {
  107. if !file.package.isEmpty {
  108. return file.package + "." + service.name
  109. } else {
  110. return service.name
  111. }
  112. }
  113. internal var methodPath: String {
  114. return "\"/" + self.servicePath + "/" + method.name + "\""
  115. }
  116. }