Skip to content

Commit

Permalink
remove autoclosure from extracted tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
griotspeak committed Dec 25, 2024
1 parent e7f4920 commit 224d63e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Sources/DiscriminatedUnionMacros/DiscriminatedUnionMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,17 @@ extension DiscriminatedUnionMacro: MemberMacro {
output = "(\(rawOut.joined(separator: ", ")))"
}

let strippedParams = if parameterClause.parameters.count == 1 {
String(describing: parameterClause.parameters.first!.type)
} else {
"(\(parameterClause.parameters.description))"
}


Swift.print("usiyan::: strippedParams: \(String(describing: strippedParams))")
return (
String(describing: singleCase.name),
parameterClause.parameters.count == 1 ? String(describing: parameterClause.parameters.first!.type) : "(\(parameterClause.parameters.description))",
strippedParams.replacingOccurrences(of: "@autoclosure ", with: ""),
bindings.joined(separator: ", "),
output
)
Expand Down
15 changes: 15 additions & 0 deletions Tests/DiscriminatedUnionTests/DiscriminatedUnionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final class DiscriminatedUnionTests: XCTestCase {
case parrot
case snake
case bird(name: String, Int)
case hydra(@autoclosure () -> String, String)
}
""",

Expand All @@ -35,13 +36,15 @@ enum Pet {
case parrot
case snake
case bird(name: String, Int)
case hydra(@autoclosure () -> String, String)
public enum Discriminant: DiscriminantType {
case dog
case cat
case parrot
case snake
case bird
case hydra
public var hasAssociatedType: Bool {
return switch self {
Expand All @@ -55,6 +58,8 @@ enum Pet {
false // nil
case .bird:
true // Optional("name: String, Int")
case .hydra:
true // Optional("@autoclosure () -> String, String")
}
}
}
Expand All @@ -71,6 +76,8 @@ enum Pet {
return .snake
case .bird:
return .bird
case .hydra:
return .hydra
}
}
Expand All @@ -93,6 +100,14 @@ enum Pet {
.failure(.invalidExtraction(expected: .bird, actual: self.discriminant))
}
}
public func tupleFromHydra() -> Swift.Result<(() -> String, String), PayloadExtractionError> {
if case .hydra(let index0, let index1) = self {
.success((index0, index1))
} else {
.failure(.invalidExtraction(expected: .hydra, actual: self.discriminant))
}
}
}
""",
Expand Down

0 comments on commit 224d63e

Please sign in to comment.