Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Sourcery template to generate a default initializer in your `struct`s (working in version 0.8.0)
{% for type in types.structs %}
{% if type|annotated:"AutoInit" %}
{% set spacing %}{% if type.parentName %} {% endif %}{% endset %}
{% map type.storedVariables into parameters using var %}{{ var.name }}: {{ var.typeName }}{% endmap %}
// sourcery:inline:auto:{{ type.name }}.AutoInit
{{spacing}} {{ type.accessLevel }} init({{ parameters|join }}) { // swiftlint:disable:this line_length
{{spacing}} {% for variable in type.storedVariables %}
{{spacing}} self.{{ variable.name }} = {{ variable.name }}
{{spacing}} {% endfor %}
{{spacing}} }
// sourcery:end
{% endif %}
{% endfor %}
@txuslee

This comment has been minimized.

txuslee commented Dec 14, 2017

Since last Sourcery update (0.10), the stencil generates an invalid initializer. Something may have changed in 'join' filter because now it doesn't append a comma by default. You must force the comma with an argument: {{ parameters|join:", " }}

And thanks for this great template, it's been very useful!

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