Skip to content

Commit

Permalink
Mapping a static function is clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
griotspeak committed Dec 25, 2024
1 parent 224d63e commit 17e4108
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ extension DiscriminatedUnionMacro: MemberMacro {
}


Swift.print("usiyan::: strippedParams: \(String(describing: strippedParams))")
return (
String(describing: singleCase.name),
strippedParams.replacingOccurrences(of: "@autoclosure ", with: ""),
Expand All @@ -158,11 +157,11 @@ extension DiscriminatedUnionMacro: MemberMacro {
let titleCasedName = "\(caseName.first!.uppercased())\(caseName.dropFirst())"
return """
public func tupleFrom\(raw: titleCasedName)() -> Swift.Result<\(raw: tupleType), PayloadExtractionError> {
if case .\(raw: caseName)(\(raw: pBindings)) = self {
public static func tupleFrom\(raw: titleCasedName)(_ instance: Self) -> Swift.Result<\(raw: tupleType), PayloadExtractionError> {
if case .\(raw: caseName)(\(raw: pBindings)) = instance {
.success(\(raw: returnValue))
} else {
.failure(.invalidExtraction(expected: .\(raw: caseName), actual: self.discriminant))
.failure(.invalidExtraction(expected: .\(raw: caseName), actual: instance.discriminant))
}
}
Expand Down
18 changes: 9 additions & 9 deletions Tests/DiscriminatedUnionTests/DiscriminatedUnionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,27 @@ enum Pet {
case invalidExtraction(expected: Discriminant, actual: Discriminant)
}
public func tupleFromCat() -> Swift.Result<Bool, PayloadExtractionError> {
if case .cat(let curious) = self {
public static func tupleFromCat(_ instance: Self) -> Swift.Result<Bool, PayloadExtractionError> {
if case .cat(let curious) = instance {
.success(curious)
} else {
.failure(.invalidExtraction(expected: .cat, actual: self.discriminant))
.failure(.invalidExtraction(expected: .cat, actual: instance.discriminant))
}
}
public func tupleFromBird() -> Swift.Result<(name: String, Int), PayloadExtractionError> {
if case .bird(let name, let index1) = self {
public static func tupleFromBird(_ instance: Self) -> Swift.Result<(name: String, Int), PayloadExtractionError> {
if case .bird(let name, let index1) = instance {
.success((name, index1))
} else {
.failure(.invalidExtraction(expected: .bird, actual: self.discriminant))
.failure(.invalidExtraction(expected: .bird, actual: instance.discriminant))
}
}
public func tupleFromHydra() -> Swift.Result<(() -> String, String), PayloadExtractionError> {
if case .hydra(let index0, let index1) = self {
public static func tupleFromHydra(_ instance: Self) -> Swift.Result<(() -> String, String), PayloadExtractionError> {
if case .hydra(let index0, let index1) = instance {
.success((index0, index1))
} else {
.failure(.invalidExtraction(expected: .hydra, actual: self.discriminant))
.failure(.invalidExtraction(expected: .hydra, actual: instance.discriminant))
}
}
}
Expand Down

0 comments on commit 17e4108

Please sign in to comment.