Created
September 16, 2017 02:28
-
-
Save klundberg/33f1558761b20fd5cadd71382d815dfb to your computer and use it in GitHub Desktop.
Sourcery template that adds CodingKeys overrides for Codable types to convert camelCase property names to snake_case in the serialized format.
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.Codable|!enum %} | |
public extension {{ type.name }} { | |
enum CodingKeys: String, CodingKey { | |
{% for variable in type.variables|instance %} | |
{% if variable|annotated:"keyOverride" %} | |
case {{ variable.name }} = "{{ variable.annotations.keyOverride }}" | |
{% else %} | |
case {{ variable.name }} = "{{ variable.name|camelToSnakeCase }}" | |
{% endif %} | |
{% endfor %} | |
} | |
} | |
{% endfor %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment