Created
June 4, 2020 08:10
-
-
Save adurbalo/b210d806b8b6ed1e385b26d7d6178c9d to your computer and use it in GitHub Desktop.
Sourcery template that extends AutoInit by setting default values as nil to optional's fields
This file contains hidden or 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:"AutoInitDefaultValues" or type.implements.AutoInitDefaultValues %} | |
{% set spacing %}{% if type.parentName %} {% endif %}{% endset %} | |
{% map type.storedVariables into parameters using var %}{{ var.name }}: {{ var.typeName }}{% if var.defaultValue %} = {{var.defaultValue}}{% elif var.typeName.isOptional %} = nil{% endif %}{% endmap %} | |
// sourcery:inline:auto:{{ type.name }}.AutoInitDefaultValues | |
{{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
Usage
protocol AutoInitDefaultValues {}
extension <SOME_STRUCT_NAME>: AutoInitDefaultValues {}