Skip to content

Commit

Permalink
Merge pull request #4 from futuredapp/feature/Enable-multiple-IDs-per…
Browse files Browse the repository at this point in the history
…-destination

Enable multiple ids per case
  • Loading branch information
ssestak authored Jan 3, 2025
2 parents ef659df + 77ca817 commit fb1502d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
6 changes: 3 additions & 3 deletions Sources/EnumIdentableMacros/EnumIdentableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
55 changes: 54 additions & 1 deletion Tests/EnumIdentableTests/EnumIdentableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ final class EnumIdentableTests: XCTestCase {
#endif
}

func testEnumWithAssociatedValuesMarkedAsIdProject() throws {
func testEnumWithAssociatedValuesMarkedAsId2() throws {
#if canImport(EnumIdentableMacros)
assertMacroExpansion(
"""
Expand Down Expand Up @@ -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
}
}

0 comments on commit fb1502d

Please sign in to comment.