Skip to content

Instantly share code, notes, and snippets.

@darrarski
Created August 27, 2022 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrarski/f433d35a847acadffe39bf7925721e10 to your computer and use it in GitHub Desktop.
Save darrarski/f433d35a847acadffe39bf7925721e10 to your computer and use it in GitHub Desktop.
Sourcery template for creating functors from functions
import XCTestDynamicOverlay
{% for func in functions %}
public struct {% for text in func.name|split:"(" %}{% if forloop.first %}{{ text|upperFirstLetter }}{% endif %}{% endfor %} {
public var run: ({% for param in func.parameters %}{{ param.typeName }}{% ifnot forloop.last %}, {% endif %}{% endfor %}){% if func.throws %} throws{% endif %} -> {{ func.returnTypeName }}
public func callAsFunction({% if func.parameters.count > 0 %}
{% for param in func.parameters %}
{{ param.asSource }}{% ifnot forloop.last %},{% endif %}
{% endfor %}
{% endif %}){% if func.throws %} throws{% endif %} -> {{ func.returnTypeName }} {
{% if func.throws %}try {% endif %}run({% for param in func.parameters %}{{ param.name }}{% ifnot forloop.last %}, {% endif %}{% endfor %})
}
}
extension {% for text in func.name|split:"(" %}{% if forloop.first %}{{ text|upperFirstLetter }}{% endif %}{% endfor %} {
public static func live() -> {% for text in func.name|split:"(" %}{% if forloop.first %}{{ text|upperFirstLetter }}{% endif %}{% endfor %} {
{% for text in func.name|split:"(" %}{% if forloop.first %}{{ text|upperFirstLetter }}{% endif %}{% endfor %} { {% if func.parameters %}{% for arg in func.parameters %}{{ arg.name }}{% ifnot forloop.last %}, {% endif %}{% endfor %} in{% endif %}
// TODO: live implementation
}
}
}
extension {% for text in func.name|split:"(" %}{% if forloop.first %}{{ text|upperFirstLetter }}{% endif %}{% endfor %} {
public static let unimplemented = {% for text in func.name|split:"(" %}{% if forloop.first %}{{ text|capitalize }}{% endif %}{% endfor %}(
run: XCTUnimplemented("\(Self.self)")
)
}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment