Skip to content

Instantly share code, notes, and snippets.

@khanlou
Created September 4, 2017 16:49
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save khanlou/b0d3d3c9dc3551e33db363800a2a368c to your computer and use it in GitHub Desktop.
{% for type in types.implementing.AutoRelationshipable %}
extension {{ type.name }} {
{% for variable in type.storedVariables %}
{% if variable.name|hasSuffix:"ID" and variable.annotations.relationshipType %}
var {% if variable.annotations.relationshipName %}{{ variable.annotations.relationshipName }}{% else %}{{ variable.name|replace:"ID","" }}{% endif %}: {{ variable.annotations.relationshipType }}? {
{% if variable.isOptional %}
guard let {{ variable.name }} = {{ variable.name }} else { return nil }
return FlatCache.shared.get(id: {{ variable.name }})
{% else %}
return FlatCache.shared.get(id: {{ variable.name }})
{% endif %}
}
{% elif variable.name|hasSuffix:"IDs" and variable.annotations.relationshipType %}
var {% if variable.annotations.relationshipName %}{{ variable.annotations.relationshipName }}{% else %}{{ variable.name|replace:"ID","" }}{% endif %}: [{{ variable.annotations.relationshipType }}] {
return {{variable.name}}.flatMap { id -> {{ variable.annotations.relationshipType }}? in
return FlatCache.shared.get(id: id)
}
}
{% endif %}
{% endfor %}
}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment