-
Notifications
You must be signed in to change notification settings - Fork 0
/
Equatable.stencil
58 lines (53 loc) · 2.58 KB
/
Equatable.stencil
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
{% for type in types.all where type.name == "SourceryImports_Equatable" %}
{% for variable in type.variables %}
{% if variable|annotated:"testable" %}@testable {% else %}{% endif %}{% if variable|annotated:"preconcurrency" %}@preconcurrency {% else %}{% endif %}import {{ variable.name }}
{% endfor %}
{% endfor %}
// MARK: - Equatable for structs and classes
{% for type in types.all|annotated:"equatable"|!enum|!protocol %}
extension {{ type.name }}: Equatable {
{{ type.accessLevel }} static func == (lhs: {{ type.name }}, rhs: {{ type.name }}) -> Bool {
{% for var in type.variables|!static|!computed %}
{% if var.actualTypeName.name == "Error" %}
guard "\(lhs.{{ var.name }})" == "\(rhs.{{ var.name }})" else { return false }
{% else %}
guard lhs.{{ var.name }} == rhs.{{ var.name }} else { return false }
{% endif %}
{% endfor %}
return true
}
}
{% endfor %}
// MARK: - Equatable for enums
{% for type in types.enums|annotated:"equatable" %}
extension {{ type.name }}: Equatable {
{{ type.accessLevel }} static func == (lhs: {{ type.name }}, rhs: {{ type.name }}) -> Bool {
switch (lhs,rhs) {
{% for case in type.cases %}
{% if case.hasAssociatedValue %}case (.{{ case.name }}({% if case.associatedValues.count == 1 %}let lhs{% else %}{% for associated in case.associatedValues %}let {{ associated.externalName }}1{% if not forloop.last%}, {% endif %}{% endfor %}{% endif %}), .{{ case.name }}({% if case.associatedValues.count == 1 %}let rhs{% else %}{% for associated in case.associatedValues %}let {{ associated.externalName }}2{% if not forloop.last%}, {% endif %}{% endfor %}{% endif %})):{% else %}case (.{{ case.name }}, .{{ case.name }}):{% endif %}
{% ifnot case.hasAssociatedValue %}return true{% else %}
{% if case.associatedValues.count == 1 %}
{% if case.associatedValues.first.actualTypeName.name == "Error" %}
return "\(lhs)" == "\(rhs)"
{% else %}
return lhs == rhs
{% endif %}
{% else %}
{% for associated in case.associatedValues %}
{% if associated.actualTypeName.name == "Error" %}
guard "\({{ associated.externalName }}1)" == "\(r{{ associated.externalName }}2)" else { return false }
{% else %}
guard {{ associated.externalName }}1 == {{ associated.externalName }}2 else { return false }
{% endif %}
{% endfor %}
return true
{% endif %}
{% endif %}
{% endfor %}
{% if type.cases.count > 1 %}
default: return false
{% endif %}
}
}
}
{% endfor %}