-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSwiftAirtable.stencil
More file actions
78 lines (72 loc) · 3.14 KB
/
SwiftAirtable.stencil
File metadata and controls
78 lines (72 loc) · 3.14 KB
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
73
74
75
76
77
78
import Foundation
import SwiftAirtable
{% for type in types.implementing.AutoAirtableObject %}
// MARK: {{ type.name }} AirtableObject
extension {{type.name}}: AirtableObject{
static var fieldKeys: [(fieldName: String, fieldType: AirtableTableSchemaFieldKey.AirtableTableSchemaFieldKeyType)] {
var values: [(fieldName: String, fieldType: AirtableTableSchemaFieldKey.AirtableTableSchemaFieldKeyType)] = []
{% for variable in type.storedVariables %}
{%if variable.name != "id" %}
{% if variable.typeName.name == "String" %}
values.append((fieldName: "{{variable.name}}", fieldType: .singleLineText))
{% elif variable.typeName.name == "Int" %}
values.append((fieldName: "{{variable.name}}", fieldType: .number))
{% elif variable.typeName.name == "AirtableAttachment" %}
values.append((fieldName: "{{variable.name}}", fieldType: .attachment))
{% elif variable.typeName.name == "Date" %}
values.append((fieldName: "{{variable.name}}", fieldType: .dateWithHour))
{% elif variable.typeName.name == "Bool" %}
values.append((fieldName: "{{variable.name}}", fieldType: .checkbox))
{% endif %}
{% endif %}
{% endfor %}
return values
}
func value(forKey key: AirtableTableSchemaFieldKey) -> AirtableValue? {
switch (key.fieldType, key.fieldName) {
{% for variable in type.storedVariables %}
{%if variable.name != "id" %}
{% if variable.typeName.name == "String" %}
case (.singleLineText, "{{variable.name}}"): return self.{{variable.name}}
{% elif variable.typeName.name == "Int" %}
case (.number, "{{variable.name}}"): return self.{{variable.name}}
{% elif variable.typeName.name == "AirtableAttachment" %}
case (.attachment, "{{variable.name}}"): return self.{{variable.name}}
{% elif variable.typeName.name == "Date" %}
case (.dateWithHour, "{{variable.name}}"): return self.{{variable.name}}
{% elif variable.typeName.name == "Bool" %}
case (.checkbox, "{{variable.name}}"): return self.{{variable.name}}
{% endif %}
{% endif %}
{% endfor %}
default: return nil
}
}
init(withId id: String, populatedTableSchemaKeys tableSchemaKeys: [AirtableTableSchemaFieldKey: AirtableValue]) {
self.id = id
tableSchemaKeys.forEach { key, value in
switch (key.fieldType, key.fieldName) {
{% for variable in type.storedVariables %}
{% if variable.name != "id" %}
{% if variable.typeName.name == "String" %}
case (.singleLineText, "{{variable.name}}"): self.{{variable.name}} = value.stringValue
{% elif variable.typeName.name == "Int" %}
case (.number, "{{variable.name}}"): self.{{variable.name}} = value.intValue
{% elif variable.typeName.name == "AirtableAttachment" %}
case (.attachment, "{{variable.name}}"):
if let attachment = (value as? [AirtableAttachment])?.first {
self.{{variable.name}} = attachment
}
{% elif variable.typeName.name == "Date" %}
case (.dateWithHour, "{{variable.name}}"): self.{{variable.name}} = value.dateValue
{% elif variable.typeName.name == "Bool" %}
case (.checkbox, "{{variable.name}}"): self.{{variable.name}} = value.boolValue
{% endif %}
{% endif %}
{% endfor %}
default: break
}
}
}
}
{% endfor %}