|
| 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 | +import Foundation |
| 16 | + |
| 17 | +package extension JavaTranslator { |
| 18 | + struct SwiftTypeName: Hashable { |
| 19 | + let swiftType: String |
| 20 | + let swiftModule: String? |
| 21 | + |
| 22 | + package init(swiftType: String, swiftModule: String?) { |
| 23 | + self.swiftType = swiftType |
| 24 | + self.swiftModule = swiftModule |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + struct SwiftToJavaMapping: Equatable { |
| 29 | + let swiftType: SwiftTypeName |
| 30 | + let javaTypes: [String] |
| 31 | + |
| 32 | + package init(swiftType: SwiftTypeName, javaTypes: [String]) { |
| 33 | + self.swiftType = swiftType |
| 34 | + self.javaTypes = javaTypes |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + enum ValidationError: Error, CustomStringConvertible { |
| 39 | + case multipleClassesMappedToSameName(swiftToJavaMapping: [SwiftToJavaMapping]) |
| 40 | + |
| 41 | + package var description: String { |
| 42 | + switch self { |
| 43 | + case .multipleClassesMappedToSameName(let swiftToJavaMapping): |
| 44 | + """ |
| 45 | + The following Java classes were mapped to the same Swift type: |
| 46 | + \(swiftToJavaMapping.map { mapping in |
| 47 | + "Swift Type: \(mapping.swiftType.swiftModule ?? "").\(mapping.swiftType.swiftType), Java Types: \(mapping.javaTypes.sorted().joined(separator: ", "))" |
| 48 | + }.joined(separator: "\n")) |
| 49 | + """ |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + } |
| 54 | + func validateClassConfiguration() throws { |
| 55 | + // Group all classes by swift name |
| 56 | + let groupedDictionary: [SwiftTypeName: [(String, (String, String?))]] = Dictionary(grouping: translatedClasses, by: { SwiftTypeName(swiftType: $0.value.swiftType, swiftModule: $0.value.swiftModule) }) |
| 57 | + // Find all that are mapped to multiple names |
| 58 | + let multipleClassesMappedToSameName: [SwiftTypeName: [(String, (String, String?))]] = groupedDictionary.filter { (key: SwiftTypeName, value: [(String, (String, String?))]) in |
| 59 | + value.count > 1 |
| 60 | + } |
| 61 | + |
| 62 | + if !multipleClassesMappedToSameName.isEmpty { |
| 63 | + // Convert them to swift object and throw |
| 64 | + var errorMappings = [SwiftToJavaMapping]() |
| 65 | + for (swiftType, swiftJavaMappings) in multipleClassesMappedToSameName { |
| 66 | + errorMappings.append(SwiftToJavaMapping(swiftType: swiftType, javaTypes: swiftJavaMappings.map(\.0))) |
| 67 | + } |
| 68 | + throw ValidationError.multipleClassesMappedToSameName(swiftToJavaMapping: errorMappings) |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | +} |
0 commit comments