Last active
March 15, 2023 08:02
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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 %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!