|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift.org project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of Swift.org project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +package extension JavaTranslator { |
| 16 | + struct SwiftTypeName: Hashable { |
| 17 | + let swiftType: String |
| 18 | + let swiftModule: String? |
| 19 | + |
| 20 | + package init(swiftType: String, swiftModule: String?) { |
| 21 | + self.swiftType = swiftType |
| 22 | + self.swiftModule = swiftModule |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + struct SwiftToJavaMapping: Equatable { |
| 27 | + let swiftType: SwiftTypeName |
| 28 | + let javaTypes: [String] |
| 29 | + |
| 30 | + package init(swiftType: SwiftTypeName, javaTypes: [String]) { |
| 31 | + self.swiftType = swiftType |
| 32 | + self.javaTypes = javaTypes |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + enum ValidationError: Error, CustomStringConvertible { |
| 37 | + case multipleClassesMappedToSameName(swiftToJavaMapping: [SwiftToJavaMapping]) |
| 38 | + |
| 39 | + package var description: String { |
| 40 | + switch self { |
| 41 | + case .multipleClassesMappedToSameName(let swiftToJavaMapping): |
| 42 | + """ |
| 43 | + The following Java classes were mapped to the same Swift type name: |
| 44 | + \(swiftToJavaMapping.map(mappingDescription(mapping:)).joined(separator: "\n")) |
| 45 | + """ |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private func mappingDescription(mapping: SwiftToJavaMapping) -> String { |
| 50 | + let javaTypes = mapping.javaTypes.map { "'\($0)'" }.joined(separator: ", ") |
| 51 | + return "Swift Type: '\(mapping.swiftType.swiftModule ?? "")'.'\(mapping.swiftType.swiftType)', Java Types: \(javaTypes)" |
| 52 | + |
| 53 | + } |
| 54 | + } |
| 55 | + func validateClassConfiguration() throws { |
| 56 | + // Group all classes by swift name |
| 57 | + let groupedDictionary: [SwiftTypeName: [(String, (String, String?))]] = Dictionary(grouping: translatedClasses, by: { SwiftTypeName(swiftType: $0.value.swiftType, swiftModule: $0.value.swiftModule) }) |
| 58 | + // Find all that are mapped to multiple names |
| 59 | + let multipleClassesMappedToSameName: [SwiftTypeName: [(String, (String, String?))]] = groupedDictionary.filter { (key: SwiftTypeName, value: [(String, (String, String?))]) in |
| 60 | + value.count > 1 |
| 61 | + } |
| 62 | + |
| 63 | + if !multipleClassesMappedToSameName.isEmpty { |
| 64 | + // Convert them to swift object and throw |
| 65 | + var errorMappings = [SwiftToJavaMapping]() |
| 66 | + for (swiftType, swiftJavaMappings) in multipleClassesMappedToSameName { |
| 67 | + errorMappings.append(SwiftToJavaMapping(swiftType: swiftType, javaTypes: swiftJavaMappings.map(\.0).sorted())) |
| 68 | + } |
| 69 | + throw ValidationError.multipleClassesMappedToSameName(swiftToJavaMapping: errorMappings) |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | +} |
0 commit comments