Skip to content

Instantly share code, notes, and snippets.

@Sherlouk
Created October 1, 2023 14: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 Sherlouk/fa3e49e24f9be7232c642b945177b05e to your computer and use it in GitHub Desktop.
Save Sherlouk/fa3e49e24f9be7232c642b945177b05e to your computer and use it in GitHub Desktop.
MeiliSearch Swift Async Sourcery Template
{% 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 %}
@Sherlouk
Copy link
Author

Sherlouk commented Oct 1, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment