From 3212e54bf52e482e232493efc6e706c13c22482f Mon Sep 17 00:00:00 2001 From: Jed Fox Date: Sat, 3 Dec 2022 13:07:55 -0500 Subject: [PATCH] Fix generating relay delegate methods with return values --- .../AutoGenerateProtocolDelegate.swifttemplate | 16 +++++++++++++++- ...toGenerateProtocolRelayDelegate.swifttemplate | 5 ++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Mastodon/Template/AutoGenerateProtocolDelegate.swifttemplate b/Mastodon/Template/AutoGenerateProtocolDelegate.swifttemplate index 47eb4ce19..e8677d970 100644 --- a/Mastodon/Template/AutoGenerateProtocolDelegate.swifttemplate +++ b/Mastodon/Template/AutoGenerateProtocolDelegate.swifttemplate @@ -1,3 +1,17 @@ +<% +func methodDeclaration(_ method: SourceryRuntime.Method, newName: String) -> String { + var result = newName + if method.throws { + result = result + " throws" + } else if method.rethrows { + result = result + " rethrows" + } + if method.returnTypeName.isVoid { + return result + } + return result + " -> \(method.returnTypeName)" +} +-%> <% for type in types.implementing["AutoGenerateProtocolDelegate"] { guard let replaceOf = type.annotations["replaceOf"] as? String else { continue } guard let replaceWith = type.annotations["replaceWith"] as? String else { continue } @@ -5,7 +19,7 @@ guard let aProtocol = types.protocols.first(where: { $0.name == protocolToGenerate }) else { continue } -%> // sourcery:inline:<%= type.name %>.AutoGenerateProtocolDelegate <% for method in aProtocol.methods { -%> -<%= method.name.replacingOccurrences(of: replaceOf, with: replaceWith) %> +<%= methodDeclaration(method, newName: method.name.replacingOccurrences(of: replaceOf, with: replaceWith)) %> <% } -%> // sourcery:end <% } %> diff --git a/Mastodon/Template/AutoGenerateProtocolRelayDelegate.swifttemplate b/Mastodon/Template/AutoGenerateProtocolRelayDelegate.swifttemplate index b57f26038..d2b0dd58c 100644 --- a/Mastodon/Template/AutoGenerateProtocolRelayDelegate.swifttemplate +++ b/Mastodon/Template/AutoGenerateProtocolRelayDelegate.swifttemplate @@ -6,6 +6,9 @@ func methodDeclaration(_ method: SourceryRuntime.Method) -> String { } else if method.rethrows { result = result + " rethrows" } + if method.returnTypeName.isVoid { + return result + } return result + " -> \(method.returnTypeName)" } -%> @@ -42,7 +45,7 @@ func methodCall( guard let aProtocol = types.protocols.first(where: { $0.name == protocolToGenerate }) else { continue } -%> // sourcery:inline:<%= type.name %>.AutoGenerateProtocolRelayDelegate <% for method in aProtocol.methods { -%> -func <%= method.name -%> { +func <%= methodDeclaration(method) -%> { <%= methodCall(method, replaceOf: replaceOf, replaceWith: replaceWith) %> }