Created
February 15, 2021 12:48
-
-
Save 4brunu/8b3bb886697824e67268fec9d57026d8 to your computer and use it in GitHub Desktop.
Sourcery template for Codable on Objc classes
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.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