From 77ca81797df0033337f268e5824b0bb776d3dca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=CC=8Cimon=20S=CC=8Cesta=CC=81k?= Date: Thu, 2 Jan 2025 12:47:11 +0100 Subject: [PATCH] Enable multiple ids per case --- .../EnumIdentableMacro.swift | 6 +- .../EnumIdentableTests.swift | 55 ++++++++++++++++++- 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/Sources/EnumIdentableMacros/EnumIdentableMacro.swift b/Sources/EnumIdentableMacros/EnumIdentableMacro.swift index 523670a..8234da0 100644 --- a/Sources/EnumIdentableMacros/EnumIdentableMacro.swift +++ b/Sources/EnumIdentableMacros/EnumIdentableMacro.swift @@ -110,11 +110,11 @@ public struct EnumIdentableMacro: MemberMacro { try SwitchExprSyntax("switch self") { for item in caseIds { if case let parameters = item.parameters, !parameters.isEmpty, parameters.contains(where: { $0.name != "_" }) { - let parameters = parameters.map(\.name).filter { $0 != "_" }.joined(separator: ", ") + let parameters = parameters.map(\.name).filter { $0 != "_" } SwitchCaseSyntax(stringLiteral: """ - case let .\(item.case)(\(parameters)): - "\(item.case)-\\(\(parameters))" + case let .\(item.case)(\(parameters.joined(separator: ", "))): + "\(item.case)-\(parameters.map { "\\(\($0))" }.joined(separator: "-"))" """ ) } else { diff --git a/Tests/EnumIdentableTests/EnumIdentableTests.swift b/Tests/EnumIdentableTests/EnumIdentableTests.swift index 7ec877a..7f90c86 100644 --- a/Tests/EnumIdentableTests/EnumIdentableTests.swift +++ b/Tests/EnumIdentableTests/EnumIdentableTests.swift @@ -244,7 +244,7 @@ final class EnumIdentableTests: XCTestCase { #endif } - func testEnumWithAssociatedValuesMarkedAsIdProject() throws { + func testEnumWithAssociatedValuesMarkedAsId2() throws { #if canImport(EnumIdentableMacros) assertMacroExpansion( """ @@ -296,4 +296,57 @@ final class EnumIdentableTests: XCTestCase { throw XCTSkip("macros are only supported when running tests for the host platform") #endif } + + func testEnumWithMoreAssociatedValuesMarkedAsId() throws { + #if canImport(EnumIdentableMacros) + assertMacroExpansion( + """ + @EnumIdentable + enum Destination: Hashable, Identifiable { + case destination(id1: Int, id2: String, a: String) + } + """ + , + expandedSource: + #""" + enum Destination: Hashable, Identifiable { + case destination(id1: Int, id2: String, a: String) + + enum CaseID { + case destination(id1: Int, id2: String) + var rawValue: String { + switch self { + case let .destination(id1, id2): + "destination-\(id1)-\(id2)" + } + } + } + + var caseId: CaseID { + switch self { + case let .destination(id1, id2, _): + .destination(id1: id1, id2: id2) + } + } + + var id: String { + self.caseId.rawValue + } + + func hash(into hasher: inout Hasher) { + hasher.combine(id) + } + + static func == (lhs: Self, rhs: Self) -> Bool { + lhs.id == rhs.id + } + } + """# + , + macros: testMacros + ) + #else + throw XCTSkip("macros are only supported when running tests for the host platform") + #endif + } }