Iterating over generic types in a Sourcery template / Swift-Stencil

614 views Asked by At

Does anyone know if there's a way in Sourcery to get a reference to the type a generic inherits from? I'm assuming this question doesn't make immediate sense, so here's some code for what I'm trying to do:

Say I have the following in Swift:

public protocol GenericProtocol { }

public enum GenericType1: GenericProtocol { }

public enum GenericType2: GenericProtocol { }

public protocol RandomProtocol { }

public struct RandomStruct<T: GenericProtocol>: RandomProtocol { }

What I'm wanting to do in Sourcery is something like this:

{% for type in types.based.RandomProtocol %}
    {% if type.isGeneric %}
        {% for genericType in types based on the protocol that this generic type inherits from %}
            {{ type.name }}<{{ genericType.name }}>
        {% endfor %}
    {% endif %}
{% endfor %}

So that it would end up printing:

RandomStruct<GenericType1>
RandomStruct<GenericType2>

The "for genericType in types based on the protocol that this generic type inherits from" part is what I'm trying to figure out how to do. Thank you for any help!

0

There are 0 answers