IDLToStructuredSwiftTranslator.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright 2023, 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. struct IDLToStructuredSwiftTranslator: Translator {
  17. private let serverCodeTranslator = ServerCodeTranslator()
  18. func translate(
  19. codeGenerationRequest: CodeGenerationRequest,
  20. client: Bool,
  21. server: Bool
  22. ) throws -> StructuredSwiftRepresentation {
  23. try self.validateInput(codeGenerationRequest)
  24. let typealiasTranslator = TypealiasTranslator(client: client, server: server)
  25. let topComment = Comment.doc(codeGenerationRequest.leadingTrivia)
  26. let imports: [ImportDescription] = [
  27. ImportDescription(moduleName: "GRPCCore")
  28. ]
  29. var codeBlocks: [CodeBlock] = []
  30. codeBlocks.append(
  31. contentsOf: try typealiasTranslator.translate(from: codeGenerationRequest)
  32. )
  33. if server {
  34. codeBlocks.append(
  35. contentsOf: try self.serverCodeTranslator.translate(from: codeGenerationRequest)
  36. )
  37. }
  38. let fileDescription = FileDescription(
  39. topComment: topComment,
  40. imports: imports,
  41. codeBlocks: codeBlocks
  42. )
  43. let fileName = String(codeGenerationRequest.fileName.split(separator: ".")[0])
  44. let file = NamedFileDescription(name: fileName, contents: fileDescription)
  45. return StructuredSwiftRepresentation(file: file)
  46. }
  47. }
  48. extension IDLToStructuredSwiftTranslator {
  49. private func validateInput(_ codeGenerationRequest: CodeGenerationRequest) throws {
  50. let servicesByNamespace = Dictionary(
  51. grouping: codeGenerationRequest.services,
  52. by: { $0.namespace }
  53. )
  54. try self.checkServiceNamesAreUnique(for: servicesByNamespace)
  55. for service in codeGenerationRequest.services {
  56. try self.checkMethodNamesAreUnique(in: service)
  57. }
  58. }
  59. // Verify service names are unique within each namespace and that services with no namespace
  60. // don't have the same names as any of the namespaces.
  61. private func checkServiceNamesAreUnique(
  62. for servicesByNamespace: [String: [CodeGenerationRequest.ServiceDescriptor]]
  63. ) throws {
  64. // Check that if there are services in an empty namespace, none have names which match other namespaces
  65. if let noNamespaceServices = servicesByNamespace[""] {
  66. let namespaces = servicesByNamespace.keys
  67. for service in noNamespaceServices {
  68. if namespaces.contains(service.name) {
  69. throw CodeGenError(
  70. code: .nonUniqueServiceName,
  71. message: """
  72. Services with no namespace must not have the same names as the namespaces. \
  73. \(service.name) is used as a name for a service with no namespace and a namespace.
  74. """
  75. )
  76. }
  77. }
  78. }
  79. // Check that service names are unique within each namespace.
  80. for (namespace, services) in servicesByNamespace {
  81. var serviceNames: Set<String> = []
  82. for service in services {
  83. if serviceNames.contains(service.name) {
  84. let errorMessage: String
  85. if namespace.isEmpty {
  86. errorMessage = """
  87. Services in an empty namespace must have unique names. \
  88. \(service.name) is used as a name for multiple services without namespaces.
  89. """
  90. } else {
  91. errorMessage = """
  92. Services within the same namespace must have unique names. \
  93. \(service.name) is used as a name for multiple services in the \(service.namespace) namespace.
  94. """
  95. }
  96. throw CodeGenError(
  97. code: .nonUniqueServiceName,
  98. message: errorMessage
  99. )
  100. }
  101. serviceNames.insert(service.name)
  102. }
  103. }
  104. }
  105. // Verify method names are unique for the service.
  106. private func checkMethodNamesAreUnique(
  107. in service: CodeGenerationRequest.ServiceDescriptor
  108. ) throws {
  109. let methodNames = service.methods.map { $0.name }
  110. var seenNames = Set<String>()
  111. for methodName in methodNames {
  112. if seenNames.contains(methodName) {
  113. throw CodeGenError(
  114. code: .nonUniqueMethodName,
  115. message: """
  116. Methods of a service must have unique names. \
  117. \(methodName) is used as a name for multiple methods of the \(service.name) service.
  118. """
  119. )
  120. }
  121. seenNames.insert(methodName)
  122. }
  123. }
  124. }
  125. extension CodeGenerationRequest.ServiceDescriptor {
  126. var namespacedTypealiasPrefix: String {
  127. if self.namespace.isEmpty {
  128. return self.name
  129. } else {
  130. return "\(self.namespace).\(self.name)"
  131. }
  132. }
  133. var namespacedPrefix: String {
  134. if self.namespace.isEmpty {
  135. return self.name
  136. } else {
  137. return "\(self.namespace)_\(self.name)"
  138. }
  139. }
  140. }