You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I added these arrays allowing for filtering on properties since stencil does not support filtering. Only map.
Here are some use cases for these filtered properties;
CustomStringConvertible
If I declare a protocol in my template.stencil and let that protocol, let's call it MyModel as CustomStringConvertible, we can then give it a default implementation of var description: String that we can override. We don't want to print optional properties without unwrapping them, so we want to first iterate through all nonoptional properties and then unwrap all optional. So we need the nonOptionalProperties and the nonOptionalProperties computed properties, an ugly example of usage:
public extension {{ protocol }} {
var description: String {
var nonOptional = ""
nonOptional = "{% for p in spec.nonOptionalProperties %}{{p.name}}: `\({{p.name}})`{% if not forloop.last %}, {% endif %}{% endfor %}"
{% if spec.optionalProperties.count > 0 %}
{% for p in spec.optionalProperties %}
if let value = {{ p.name }} {
nonOptional.append(", {{ p.name }}: `\(value)`")
}
{% endfor %}
{% endif %}
return nonOptional
}
}
CodingKeys
Or let's say we want to make MyModel conform to Codable with default implementations, then we want to separate between those properties who have a custom jsonKey and those who have not:
enum {{ codingKeys }}: String, CodingKey {
//MARK: Custom JSON key cases
{% for customKeyCase in spec.customKeyProperties %}
case {{ customKeyCase.name }} = "{{ customKeyCase.jsonKey }}"
{% endfor %}
//MARK: Standard cases
{% if spec.nonCustomKeyProperties.count > 0 %}
case {% for case in spec.nonCustomKeyProperties %}{{ case.name }}{% if not forloop.last %}, {% endif %}{% endfor %}
{% endif %}
}
Which result in e.g. this Swift code for a Hotel model:
enumHotelCodingKeys:String,CodingKey{
//MARK: Custom JSON key cases
case location ="geolocation"
//MARK: Standard cases
case name, propertyCode, image, phoneNumber, address
}
@hebertialmeida sorry for slow response, I just created this PR, not mergable, so that you can copy paste/Git cherry pick if you find anything interesting.
In a fork of this repo I have added some more metadata helpful in our stencil templates,
In
Schema
:Inside
JsonParser
:I added these arrays allowing for filtering on
properties
since stencil does not supportfiltering
. Only map.Here are some use cases for these filtered properties;
CustomStringConvertible
If I declare a protocol in my template.stencil and let that protocol, let's call it
MyModel
asCustomStringConvertible
, we can then give it a default implementation ofvar description: String
that we can override. We don't want to print optional properties without unwrapping them, so we want to first iterate through all nonoptional properties and then unwrap all optional. So we need thenonOptionalProperties
and thenonOptionalProperties
computed properties, an ugly example of usage:CodingKeys
Or let's say we want to make
MyModel
conform toCodable
with default implementations, then we want to separate between those properties who have a custom jsonKey and those who have not:Which result in e.g. this Swift code for a Hotel model:
For this template:
I have already implemented this in a fork, do you feel like this is something that you want part of this repo? If so I can create a PR?
The text was updated successfully, but these errors were encountered: