stencilproject / Stencil

Stencil is a simple and powerful template language for Swift.
https://stencil.fuller.li
BSD 2-Clause "Simplified" License
2.34k stars 224 forks source link

question: how to combine keys of multiple dictionaries in a single list and remove duplicates #337

Open claire-lynch-okcupid opened 1 year ago

claire-lynch-okcupid commented 1 year ago

hello stencil team! i am working with stencil in the context of a custom swiftgen template. i am attempting to iterate over a list of dictionaries and extract all the unique keys which will then represent the cases of an enum. something like this:

{% macro extractUniqueKeys dictionaries %}
public enum AnalyticsPropertyKey: String {
  {% for dict in dictionaries %}
  {% for key,value in dict %}
  // Only want to execute setting key name and declaring case if I haven't already found that key!
  {% set keyName %}{{key|swiftIdentifier:"pretty"|lowerFirstWord|escapeReservedKeywords}}{% endset %}
    case {{keyName}} = "{{key}}"
  {% endfor %}
  {% endfor %}
}
{% endmacro %}

Is there a way to do this? I also asked on the swiftgen repo, too, in case that's the better forum. thank you!