Fix generating relay delegate methods with return values

This commit is contained in:
Jed Fox 2022-12-03 13:07:55 -05:00
parent 348e176f89
commit 3212e54bf5
No known key found for this signature in database
GPG Key ID: 0B61D18EA54B47E1
2 changed files with 19 additions and 2 deletions

View File

@ -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"] { <% for type in types.implementing["AutoGenerateProtocolDelegate"] {
guard let replaceOf = type.annotations["replaceOf"] as? String else { continue } guard let replaceOf = type.annotations["replaceOf"] as? String else { continue }
guard let replaceWith = type.annotations["replaceWith"] 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 } -%> guard let aProtocol = types.protocols.first(where: { $0.name == protocolToGenerate }) else { continue } -%>
// sourcery:inline:<%= type.name %>.AutoGenerateProtocolDelegate // sourcery:inline:<%= type.name %>.AutoGenerateProtocolDelegate
<% for method in aProtocol.methods { -%> <% for method in aProtocol.methods { -%>
<%= method.name.replacingOccurrences(of: replaceOf, with: replaceWith) %> <%= methodDeclaration(method, newName: method.name.replacingOccurrences(of: replaceOf, with: replaceWith)) %>
<% } -%> <% } -%>
// sourcery:end // sourcery:end
<% } %> <% } %>

View File

@ -6,6 +6,9 @@ func methodDeclaration(_ method: SourceryRuntime.Method) -> String {
} else if method.rethrows { } else if method.rethrows {
result = result + " rethrows" result = result + " rethrows"
} }
if method.returnTypeName.isVoid {
return result
}
return result + " -> \(method.returnTypeName)" return result + " -> \(method.returnTypeName)"
} }
-%> -%>
@ -42,7 +45,7 @@ func methodCall(
guard let aProtocol = types.protocols.first(where: { $0.name == protocolToGenerate }) else { continue } -%> guard let aProtocol = types.protocols.first(where: { $0.name == protocolToGenerate }) else { continue } -%>
// sourcery:inline:<%= type.name %>.AutoGenerateProtocolRelayDelegate // sourcery:inline:<%= type.name %>.AutoGenerateProtocolRelayDelegate
<% for method in aProtocol.methods { -%> <% for method in aProtocol.methods { -%>
func <%= method.name -%> { func <%= methodDeclaration(method) -%> {
<%= methodCall(method, replaceOf: replaceOf, replaceWith: replaceWith) %> <%= methodCall(method, replaceOf: replaceOf, replaceWith: replaceWith) %>
} }