Skip to content

Instantly share code, notes, and snippets.

@Liquidsoul
Last active March 15, 2023 08:02
Show Gist options
  • Save Liquidsoul/efa4f65af055acfde64afed6cda0007d to your computer and use it in GitHub Desktop.
Save Liquidsoul/efa4f65af055acfde64afed6cda0007d to your computer and use it in GitHub Desktop.
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
Copy link

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!

@ferologics
Copy link

Thank you so much @txuslee for the fix!

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