AutoGenerateProtocolRelayDelegate.swifttemplate 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <%
  2. func methodDeclaration(_ method: SourceryRuntime.Method) -> String {
  3. var result = method.name
  4. if method.throws {
  5. result = result + " throws"
  6. } else if method.rethrows {
  7. result = result + " rethrows"
  8. }
  9. return result + " -> \(method.returnTypeName)"
  10. }
  11. -%>
  12. <%# Constructs method call string passing in parameters with their local names -%>
  13. <%
  14. func methodCall(
  15. _ method: SourceryRuntime.Method,
  16. replaceOf: String,
  17. replaceWith: String
  18. ) -> String {
  19. let params = method.parameters.map({
  20. if let label = $0.argumentLabel {
  21. return "\(label): \($0.name)"
  22. } else {
  23. return $0.name
  24. }
  25. }).joined(separator: ", ")
  26. var result = "\(method.callName)(\(params))"
  27. if method.throws {
  28. result = "try " + result
  29. }
  30. if !method.returnTypeName.isVoid {
  31. result = "return " + result
  32. }
  33. result = result.replacingOccurrences(of: replaceOf, with: replaceWith)
  34. return result
  35. }
  36. -%>
  37. <% for type in types.implementing["AutoGenerateProtocolRelayDelegate"] {
  38. guard let replaceOf = type.annotations["replaceOf"] as? String else { continue }
  39. guard let replaceWith = type.annotations["replaceWith"] as? String else { continue }
  40. guard let protocolToGenerate = type.annotations["protocolName"] as? String else { continue }
  41. guard let aProtocol = types.protocols.first(where: { $0.name == protocolToGenerate }) else { continue } -%>
  42. // sourcery:inline:<%= type.name %>.AutoGenerateProtocolRelayDelegate
  43. <% for method in aProtocol.methods { -%>
  44. func <%= method.name -%> {
  45. <%= methodCall(method, replaceOf: replaceOf, replaceWith: replaceWith) %>
  46. }
  47. <% } -%>
  48. // sourcery:end
  49. <% } %>