Skip to content

Instantly share code, notes, and snippets.

@stephanecopin
Last active October 19, 2018 18:56
Show Gist options
  • Save stephanecopin/c125d08b9730210a67d51ed5e8456527 to your computer and use it in GitHub Desktop.
Save stephanecopin/c125d08b9730210a67d51ed5e8456527 to your computer and use it in GitHub Desktop.
A Sourcery template for generating helper functions to emulate default parameters in enum case associated values
// swiftlint:disable all
// Copyright 2018 Stéphane Copin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Requires Swift 4.2+
// Example usage:
// struct MyModel {
// let id: String
// }
//
// enum MyEnum {
// // sourcery: 1 = "nil"
// // sourcery: foo = "true"
// // sourcery: bar = "0"
// case customCase(MyModel, String?, foo: Bool, bar: Int)
// }
//
// let customCase = MyEnum.customCase(MyModel(id: ""))
<%
typealias CaseInfo = (localName: String?, externalName: String, typeName: String, isClosure: Bool, defaultValue: String?)
for type in types.enums where type.hasAssociatedValues {
var allValues: [String: [CaseInfo]] = [:]
for `case` in type.cases {
for (index, associatedValue) in `case`.associatedValues.enumerated() {
let associatedValueLocalName = associatedValue.localName
let associatedValueExternalName = associatedValue.externalName ?? "data"
let defaultValue = `case`.annotations[associatedValueExternalName] as? String
allValues[`case`.name, default: []].append((associatedValueLocalName, "arg\(index)", associatedValue.typeName.name, associatedValue.typeName.isClosure && !associatedValue.typeName.isOptional, defaultValue))
}
}
if allValues.flatMap({ $0.value }).compactMap({ $0.defaultValue }).isEmpty {
continue
}
-%>
extension <%= type.name %> {
<%
for (associatedValueName, allValues) in allValues {
func combos(_ elements: [CaseInfo]) -> [[CaseInfo]] {
guard let first = elements.first else {
return [[]]
}
var result: [[CaseInfo]] = []
let subcombos = combos(Array(elements.dropFirst()))
if first.defaultValue != nil {
result += subcombos.map { [(first.localName, first.externalName, first.typeName, first.isClosure, nil)] + $0 }
}
result += subcombos.map { [first] + $0 }
return result
}
var allCombinations: [[CaseInfo]] = combos(allValues)
for combinations in allCombinations where combinations.contains(where: { $0.defaultValue != nil }) {
let allParameters = combinations.filter { $0.defaultValue == nil }
-%>
<%= type.accessLevel %> static <% if allParameters.isEmpty { -%>var<% } else { -%>func<% } -%> <%= associatedValueName %><% if !allParameters.isEmpty { -%>(<% } -%>
<%
for i in allParameters.indices {
let (localName, externalName, typeName, isClosure, defaultValue) = allParameters[i]
-%>
<% if let localName = localName { -%><%= localName %> <% } else {-%>_ <% } -%><%= externalName %>: <% if isClosure { -%>@escaping <% } -%><%= typeName %><% if i < allParameters.count - 1 { -%>,<% } -%><%_ %>
<%
}
-%>
<% if allParameters.isEmpty { -%>:<% } else { -%>) -><% } -%> <%= type.name %> {
return .<%= associatedValueName %>(
<%
for i in combinations.indices {
let (localName, externalName, typeName, _, defaultValue) = combinations[i]
-%>
<%_ %><% if let localName = localName { -%><%= localName %>: <% }; if let defaultValue = defaultValue { -%><%= defaultValue %><% } else { -%><%= externalName %><% } -%><% if i < combinations.count - 1 { -%>,
<% } -%>
<%
}
-%><%_ %>
)
}
<%
}
}
-%><%_ %>
}
<%_ } -%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment