-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathswift-named-type.mustache
72 lines (69 loc) · 1.66 KB
/
swift-named-type.mustache
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{{#custom}}
{{#documentationLines}}
///{{{.}}}
{{/documentationLines}}
public struct {{typeName}}: Codable {
{{#members}}
{{#documentationLines}}
///{{{.}}}
{{/documentationLines}}
public var {{name}}: {{type}}
{{/members}}
{{#staticMembers}}
{{#documentationLines}}
///{{{.}}}
{{/documentationLines}}
private var {{name}}: {{type}} = {{{value}}}
{{/staticMembers}}
public init({{#members}}{{name}}: {{type}}{{#defaultValue}} = {{defaultValue}}{{/defaultValue}}{{^last}}, {{/last}}{{/members}}) {
{{#members}}
self.{{name}} = {{name}}
{{/members}}
}
}
{{/custom}}
{{#enum}}
{{#documentationLines}}
///{{{.}}}
{{/documentationLines}}
public enum {{typeName}}: {{valueType}}, Codable {
{{#members}}
{{#documentationLines}}
///{{{.}}}
{{/documentationLines}}
case {{key}} = {{{value}}}
{{/members}}
}
{{/enum}}
{{#unionType}}
public enum {{unionTypeName}}: Codable {
{{#members}}
case {{uncapitalizeName}}(_ value: {{type}})
{{/members}}
public init(from decoder: any Decoder) throws {
let container = try decoder.singleValueContainer()
{{#members}}
{{^last}}
{{^first}}else {{/first}}if let value = try? container.decode({{type}}.self) {
self = .{{uncapitalizeName}}(value)
}
{{/last}}
{{#last}}
else {
let value = try container.decode({{type}}.self)
self = .{{uncapitalizeName}}(value)
}
{{/last}}
{{/members}}
}
public func encode(to encoder: any Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
{{#members}}
case .{{uncapitalizeName}}(let value):
try container.encode(value)
{{/members}}
}
}
}
{{/unionType}}