Forked from Banck/gist:0aa2d992173f972999772cda768929f5
Created
December 11, 2018 10:09
-
-
Save VadimYakovliev/cb86b2571ec1b2a6a0eb23170832ce21 to your computer and use it in GitHub Desktop.
JSONRepresentable.stencil
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.based.JSONAutoRepresentable| %} | |
// sourcery:inline:auto:{{ type.name }}.JSONAutoRepresentable | |
init(_ json: JSON) { | |
{% for variable in type.storedVariables where variable|!annotated:"skip" %} | |
{% elif variable.type.based.JSONAutoRepresentable %} | |
{{ variable.name }} = {{ variable.unwrappedTypeName }}(json["{{ variable.name|camelToSnakeCase }}"]) | |
{% elif variable.isArray %} | |
{{ variable.name }} = json["{{ variable.name|camelToSnakeCase }}"].arrayValue.map({{ variable.typeName.array.elementTypeName.unwrappedTypeName }}.init) | |
{% elif variable.type.kind == "enum" %} | |
{{ variable.name }} = {{ variable.unwrappedTypeName }}(rawValue: json["{{ variable.name|camelToSnakeCase }}"].stringValue) ?? {{ variable.defaultValue }} | |
{% elif variable.annotations.jsonKey %} | |
{{ variable.name }} = json["{{ variable.annotations.jsonKey}}"].{{ variable.unwrappedTypeName|lowerFirstLetter }}{% ifnot variable.isOptional %}Value{% endif %} | |
{% else %} | |
{{ variable.name }} = json["{{ variable.name|camelToSnakeCase }}"].{{ variable.unwrappedTypeName|lowerFirstLetter }}{% ifnot variable.isOptional %}Value{% endif %} | |
{% endif %} | |
{% endfor %} | |
} | |
// sourcery:end | |
{% endfor %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment