-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLens.stencil
45 lines (39 loc) · 1.46 KB
/
Lens.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
{% for type in types.all where type.name == "SourceryImports_Lens" %}
{% for variable in type.variables %}
{% if variable|annotated:"testable" %}@testable {% else %}{% endif %}{% if variable|annotated:"preconcurrency" %}@preconcurrency {% else %}{% endif %}import {{ variable.name }}
{% endfor %}
{% endfor %}
{% for type in types.structs|annotated:"lens" %}
extension {{ type.name }} {
{{ type.accessLevel }} enum lens {
{% for var in type.variables|!static|!computed %}
{{ type.accessLevel }} static var {{ var.name }}: Lens<{{ type.name }},{{ var.typeName }}> {
return Lens<{{ type.name }},{{ var.typeName }}>(
get: { $0.{{ var.name }} },
set: { part in
{ whole in
{% if var.isMutable %}
var m = whole
m.{{ var.name }} = part
return m
{% else %}
{{ type.name }}(
{% for var1 in type.variables|!static|!computed %}
{{ var1.name }}: {% if var1.name == var.name %}part{% else %}whole.{{ var1.name }}{% endif %}{% if not forloop.last %},{% endif %}{% endfor %})
{% endif %}
}
})
}
{% endfor %}
}
}
{% if type.annotations.lens == "chain" %}
{{ type.accessLevel }} extension Lens where S == T, A == B, B == {{ type.name }} {
{% for var in type.variables|!static|!computed %}
var {{ var.name }}: Lens<S,{{ var.typeName }}> {
return self.then({{ type.name }}.lens.{{ var.name }})
}
{% endfor %}
}
{% endif %}
{% endfor %}