Skip to content

Instantly share code, notes, and snippets.

@4brunu
Created February 15, 2021 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 4brunu/8b3bb886697824e67268fec9d57026d8 to your computer and use it in GitHub Desktop.
Save 4brunu/8b3bb886697824e67268fec9d57026d8 to your computer and use it in GitHub Desktop.
Sourcery template for Codable on Objc classes
{% for type in types.all where type.based.AutoCodableObjc or type|annotated:"AutoCodableObjc" %}
// sourcery:inline:auto:{{ type.name }}.AutoCodableObjc
enum CodingKeys: String, CodingKey {
{% for variable in type.allVariables where variable.isComputed == false %}case {{variable.name}}
{% endfor %}
}
{{type.accessLevel}} required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
{% for variable in type.allVariables where variable.isComputed == false %}{{variable.name}} = try{% if variable.isOptional %}?{% endif %} container.decode({{variable.typeName}}.self, forKey: .{{variable.name}})
{% endfor %}
super.init()
}
@available(*, unavailable)
required init(coder _: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// sourcery:end
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment