Skip to content

Commit

Permalink
Make where clause and generic parameter detection shorter
Browse files Browse the repository at this point in the history
  • Loading branch information
rocxteady committed Oct 25, 2023
1 parent d26ccbf commit 9fb57ac
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
9 changes: 8 additions & 1 deletion Sources/ThrowPublisherClient/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ struct MyStruct {
}

@ThrowPublisher
func doSomething<T>(arg: T) throws -> String where T: Equatable {
func doSomething<T, P>(arg: T, arg2: P) throws -> String where T: Equatable {
"Something"
}
}

extension Array {
@ThrowPublisher
func doSomething(arg: String) throws -> String where Self.Element == Int {
"Something"
}
}
Expand Down
18 changes: 3 additions & 15 deletions Sources/ThrowPublisherMacros/ThrowPublisherMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ enum ThrowPublisherError: Error, CustomStringConvertible {
case identifierType
case asyncSpecifier
case noThrow
case unsupportedWhereClause

var description: String {
switch self {
Expand All @@ -22,8 +21,6 @@ enum ThrowPublisherError: Error, CustomStringConvertible {
return "Could not get the name of wildcard(_) parameter."
case .asyncSpecifier:
return "ThrowPublisher doesn't support async specifier."
case .unsupportedWhereClause:
return "Unsupported where clause."
case .noThrow:
return "Function doesn't throw."
}
Expand Down Expand Up @@ -61,21 +58,12 @@ public struct ThrowPublisherMacro: PeerMacro {
var newFunctionName = "\(functionName)_publisher"

if let genericParameterClause = functionDecl.genericParameterClause {
let generics = genericParameterClause.parameters.map {
$0.name.text
}.joined(separator: ", ")
newFunctionName += "<\(generics)>"
newFunctionName += "\(genericParameterClause)"
}

var genericPart: String?
if let genericWhereClause = functionDecl.genericWhereClause {
let generics = try genericWhereClause.requirements.map {
guard let conformanceRequirementSyntax = $0.requirement.as(ConformanceRequirementSyntax.self),
let leftType = conformanceRequirementSyntax.leftType.as(IdentifierTypeSyntax.self),
let rightType = conformanceRequirementSyntax.rightType.as(IdentifierTypeSyntax.self)else { throw ThrowPublisherError.unsupportedWhereClause }
return "\(leftType.name.text)\(conformanceRequirementSyntax.colon.text) \(rightType.name.text)"
}.joined(separator: ", ")
genericPart = "where \(generics)"
genericPart = "where \(genericWhereClause.requirements)"
}

let parameters = "\(functionDecl.signature.parameterClause)"
Expand Down Expand Up @@ -110,7 +98,7 @@ public struct ThrowPublisherMacro: PeerMacro {
startOfFunction = "\(modifiers) \(startOfFunction)"
}
let hop = """
\(startOfFunction) \(newFunctionName)\(parameters)-> \(returnPart) {
\(startOfFunction) \(newFunctionName)\(parameters)-> \(returnPart){
func getResult() -> Result<\(returnType), Error> {
do {
\(resultString)
Expand Down
14 changes: 7 additions & 7 deletions Tests/ThrowPublisherTests/ThrowPublisherTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,19 @@ final class ThrowPublisherTests: XCTestCase {
assertMacroExpansion(
"""
@ThrowPublisher
func someFunc<T>(arg1: T) throws -> String {
func someFunc<T, P>(arg1: T, arg2: P) throws -> String {
"something"
}
""",
expandedSource: """
func someFunc<T>(arg1: T) throws -> String {
func someFunc<T, P>(arg1: T, arg2: P) throws -> String {
"something"
}
func someFunc_publisher<T>(arg1: T) -> AnyPublisher<String, Error> {
func someFunc_publisher<T, P>(arg1: T, arg2: P) -> AnyPublisher<String, Error> {
func getResult() -> Result<String, Error> {
do {
let result = try someFunc(arg1: arg1)
let result = try someFunc(arg1: arg1, arg2: arg2)
return .success(result)
} catch {
return .failure(error)
Expand Down Expand Up @@ -227,16 +227,16 @@ final class ThrowPublisherTests: XCTestCase {
assertMacroExpansion(
"""
@ThrowPublisher
func someFunc<T>(arg1: T) throws -> String where T: Equatable {
func someFunc<T>(arg1: T) throws -> String where T: Equatable, Self.Element == Int {
"something"
}
""",
expandedSource: """
func someFunc<T>(arg1: T) throws -> String where T: Equatable {
func someFunc<T>(arg1: T) throws -> String where T: Equatable, Self.Element == Int {
"something"
}
func someFunc_publisher<T>(arg1: T) -> AnyPublisher<String, Error> where T: Equatable {
func someFunc_publisher<T>(arg1: T) -> AnyPublisher<String, Error> where T: Equatable, Self.Element == Int {
func getResult() -> Result<String, Error> {
do {
let result = try someFunc(arg1: arg1)
Expand Down

0 comments on commit 9fb57ac

Please sign in to comment.