Created
February 17, 2021 15:00
-
-
Save 4brunu/75e257447d987212253659c59125b72b to your computer and use it in GitHub Desktop.
Sourcery template for wrapping delegates with closures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
{% macro swiftifyMethodName name %}{{ name | replace:"(","_" | replace:")","" | replace:":","_" | replace:"`","" | snakeToCamelCase | lowerFirstWord }}{% endmacro %} | |
{% for protocol in types.protocols where protocol.based.AutoDelegateWrapper or protocol|annotated:"AutoDelegateWrapper" %} | |
{% if type.name != "AutoDelegateWrapper" %} | |
class {{ protocol.name }}Wrapper: {{ protocol.name }} { | |
init({% for method in protocol.allMethods %}{% call swiftifyMethodName method.selectorName %}: (({% for parameter in method.parameters %}{{parameter.typeName}}{% if not forloop.last %}, {% endif %}{% endfor %}) -> {{method.returnTypeName}}){% if method.returnTypeName.isVoid %}?{% endif %}{% if not forloop.last %}, {% endif %}{% endfor %}) { | |
{% for method in protocol.allMethods %} | |
self.{% call swiftifyMethodName method.selectorName %} = {% call swiftifyMethodName method.selectorName %} | |
{% endfor %} | |
} | |
{% for method in protocol.allMethods %} | |
private var {% call swiftifyMethodName method.selectorName %}: (({% for parameter in method.parameters %}{{parameter.typeName}}{% if not forloop.last %}, {% endif %}{% endfor %}) -> {{method.returnTypeName}}){% if method.returnTypeName.isVoid %}?{% endif %} | |
@objc func {{ method.name }}{% if not method.returnTypeName.isVoid %} -> {{ method.returnTypeName }}{% endif %} { | |
{% if not method.returnTypeName.isVoid %}return {% endif %}self.{% call swiftifyMethodName method.selectorName %}{% if method.returnTypeName.isVoid %}?{% endif %}({% for parameter in method.parameters %}{{parameter.name}}{% if not forloop.last %}, {% endif %}{% endfor %}) | |
} | |
{% endfor %} | |
} | |
{% endif %} | |
{% endfor %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment