-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathObjectDefinition+Builder.swift
119 lines (99 loc) · 4.93 KB
/
ObjectDefinition+Builder.swift
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// ObjectDefinition+Builder.swift
// Eject
//
// Created by Brian King on 10/19/16.
// Copyright © 2016 Brian King. All rights reserved.
//
import Foundation
extension ObjectDefinition: Builder {
func buildElement(attributes: inout [String: String], document: XIBDocument, parent: Reference?) throws -> Reference? {
// if a key is specified, the ID can be nil, so just generate a UUID in that case.
let identifier = attributes.removeValue(forKey: "id") ?? UUID().uuidString
let customClass = attributes.removeValue(forKey: "customClass")
// If this is a top level object with a custom class, select this identifier as self.
if customClass != nil && customClass != "UIResponder" && parent == nil && document.configuration.selfIdentifier == nil {
document.configuration.selfIdentifier = identifier
}
for key in ["customModule", "placeholderIdentifier", "customModuleProvider", "misplaced"] {
attributes.removeValue(forKey: key)
}
let object = document.addObject(
for: identifier,
definition: self,
customSubclass: customClass,
userLabel: attributes.removeValue(forKey: "userLabel")
)
let initializer = Initializer(objectIdentifier: identifier, className: object.className)
try document.addStatement(for: identifier, generator: initializer, phase: .initialization)
// Specify default values for properties that are injected in case they are not supplied.
for property in properties where property.injected {
object.values[property.key.propertyName] = BasicValue(value: property.defaultValue, format: property.format)
}
// If a key is specified, add a configuration to the parent
if let parentKey = attributes.removeValue(forKey: "key") {
if document.isPlaceholder(for: identifier) {
// If this is a placeholder (IE: an object that the parent will initialize internally) set the variable name to the property.
document.variableNameOverrides[identifier] = { document in
if let parent = parent {
return [document.variable(for: parent), parentKey].joined(separator: ".")
}
else {
return parentKey
}
}
}
else {
guard let parent = parent else { throw XIBParser.Error.needParent }
// Otherwise create a create an assignment
let value = VariableValue(objectIdentifier: object.identifier)
try document.addVariableConfiguration(for: parent.identifier, attribute: parentKey, value: value)
}
}
try buildElementProperties(attributes: &attributes, document: document, object: object)
return object
}
}
struct PropertyBuilder: Builder {
var keysToRemove: [String]
var properties: [ObjectDefinition.Property]
func buildElement(attributes: inout [String: String], document: XIBDocument, parent: Reference?) throws -> Reference? {
for key in keysToRemove {
attributes.removeValue(forKey: key)
}
return try buildElementProperties(attributes: &attributes, document: document, object: parent)
}
}
extension PropertyBuilder: PropertyContainer {}
extension ObjectDefinition: PropertyContainer {}
private protocol PropertyContainer: Builder {
var properties: [ObjectDefinition.Property] { get }
}
extension PropertyContainer {
@discardableResult func buildElementProperties(attributes: inout [String: String], document: XIBDocument, object: Reference?) throws -> Reference? {
guard let object = object else { throw XIBParser.Error.needParent }
let identifier = object.identifier
for property in properties {
if let value = attributes.removeValue(forKey: property.key.attribute) {
if property.injected {
// If the property is injected, just add the value
try document.lookupReference(for: identifier).values[property.key.propertyName] = BasicValue(value: value, format: property.format)
}
else if case .placeholder(let property) = property.context {
document.placeholders.append(value)
document.variableNameOverrides[value] = { document in
return [document.variable(for: object), property].joined(separator: ".")
}
}
else if value != property.defaultValue && !property.ignored {
try document.addVariableConfiguration(
for: identifier,
attribute: property.key.attribute,
value: BasicValue(value: value, format: property.format)
)
}
}
}
return object
}
}