Created
October 1, 2023 14:34
-
-
Save Sherlouk/fa3e49e24f9be7232c642b945177b05e to your computer and use it in GitHub Desktop.
MeiliSearch Swift Async Sourcery Template
This file contains hidden or 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
{% macro extractResultType parameter %}{% typed parameter as MethodParameter %}{{ parameter.typeName.closure.parameters[0].typeName.generic.typeParameters[0].typeName }}{% endmacro %} | |
{% macro extractParameters parameters %}{% typed parameters as [MethodParameter] %}{% map parameters into newArray %}{% if maploop.last == false %}{{ maploop.item.asSource }}{% endif %}{% endmap %}{{ newArray|join:", " |replace:", $","","regex" }}{% endmacro %} | |
{% macro extractFunctionName method %}{% for var in method.name|split:"(" %}{% if forloop.first %}{{ var|replace:">","" }}{% if method.isGeneric %}{% for conformance in method.returnTypeName.name|split:"where T" %}{% if forloop.last %}{{ conformance }}>{% endif %}{% endfor %}{% endif %}{% endif %}{% endfor %}{% endmacro %} | |
{% macro extractParametersCalled parameters %}{% map parameters into newArray %}{% if not maploop.last %}{% if maploop.item.argumentLabel %}{{ maploop.item.argumentLabel }}: {{ maploop.item.name }}{% else %}{{ maploop.item.name }}{% endif %}{% endif %}{% endmap %}{{ newArray|join:", "|replace:", $","","regex" }}{% endmacro %} | |
{% macro extractFunctionCall method %}{{ method.callName }}({% call extractParametersCalled method.parameters %}){% endmacro %} | |
import Foundation | |
{% for type in types.all %} | |
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) | |
extension {{ type.name }} { | |
{% for method in type.instanceMethods where method.parameters.last.name == "completion" and method.accessLevel == "public" and not method.attributes["available"] %} | |
{% set functionCall %}{% call extractFunctionCall method %}{% endset %} | |
{% set functionName %}{% call extractFunctionName method %}{% endset %} | |
{% set returnType %} -> {% call extractResultType method.parameters.last %}{% endset %} | |
/** | |
See `{{method.selectorName}}` | |
*/ | |
{{ method.accessLevel }} func {{ functionName|replace:", T:"," &" }}({% call extractParameters method.parameters %}) async throws{{ returnType|replace:" -> ()", "" }} { | |
try await withCheckedThrowingContinuation { continuation in | |
self.{{ functionCall|replace:"()","" }} { result in | |
continuation.resume(with: result) | |
} | |
} | |
} | |
{% endfor %} | |
} | |
{% endfor %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Relates to meilisearch/meilisearch-swift#427