Unable to create Sourcery template for generic methods

39 views Asked by At

I trying to get Sourcery to generate code for a method which returns a generic object.

Would anyone be able to help me get the <T: Codable> out of this method?

func get<T: Codable>(for key: String) -> T?

This is my template AutoStub.stencil:

{% macro methodReturnName method -%}
{{ method.callName }}
{%- for param in method.parameters -%}
{{ param.name | upperFirstLetter }}
{%- endfor -%}_returnValue
{%- endmacro -%}

{% macro methodReturnType method -%}
{% if method.throws -%}
Result<{{ method.returnTypeName }}, Error>
{%- else -%}
{{ method.returnTypeName }}
{%- endif -%}
{%- endmacro -%}

{% macro methodReturnNameAndType method -%}
{%- if method.isGeneric -%}
{%- call methodReturnName method -%}
: {{ method.returnTypeName }} // <- I'm stuck on this
{%- else -%}
{% call methodReturnName method %}: {% call methodReturnType method %}
{%- endif -%}
{%- endmacro -%}

public protocol {{ protocol.name }}Stubbing {
{% for method in protocol.methods %}
    var {% call methodReturnNameAndType method %} { get set }
{% endfor %}
}
0

There are 0 answers