Generator-Names.swift 5.0 KB

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