Skip to content

Instantly share code, notes, and snippets.

@klundberg
Created September 16, 2017 02:28
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 klundberg/33f1558761b20fd5cadd71382d815dfb to your computer and use it in GitHub Desktop.
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.
{% 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