Generator-Names.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 clientClassName: String {
  71. return nameForPackageService(file, service) + "Client"
  72. }
  73. internal var testClientClassName: String {
  74. return nameForPackageService(self.file, self.service) + "TestClient"
  75. }
  76. internal var clientProtocolName: String {
  77. return nameForPackageService(file, service) + "ClientProtocol"
  78. }
  79. internal var clientInterceptorProtocolName: String {
  80. return nameForPackageService(file, service) + "ClientInterceptorFactoryProtocol"
  81. }
  82. internal var serverInterceptorProtocolName: String {
  83. return nameForPackageService(file, service) + "ServerInterceptorFactoryProtocol"
  84. }
  85. internal var callName: String {
  86. return nameForPackageServiceMethod(file, service, method) + "Call"
  87. }
  88. internal var methodFunctionName: String {
  89. var name = method.name
  90. if !self.options.keepMethodCasing {
  91. name = name.prefix(1).lowercased() + name.dropFirst()
  92. }
  93. return self.sanitize(fieldName: name)
  94. }
  95. internal func sanitize(fieldName string: String) -> String {
  96. if quotableFieldNames.contains(string) {
  97. return "`\(string)`"
  98. }
  99. return string
  100. }
  101. internal var methodInputName: String {
  102. return protobufNamer.fullName(message: method.inputType)
  103. }
  104. internal var methodOutputName: String {
  105. return protobufNamer.fullName(message: method.outputType)
  106. }
  107. internal var methodInterceptorFactoryName: String {
  108. return "make\(self.method.name)Interceptors"
  109. }
  110. internal var servicePath: String {
  111. if !file.package.isEmpty {
  112. return file.package + "." + service.name
  113. } else {
  114. return service.name
  115. }
  116. }
  117. internal var methodPath: String {
  118. return "\"/" + self.servicePath + "/" + method.name + "\""
  119. }
  120. }