forked from alibaba/HandyJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtendCustomModelType.swift
277 lines (230 loc) · 10.1 KB
/
ExtendCustomModelType.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//
// ExtendCustomType.swift
// HandyJSON
//
// Created by zhouzhuo on 16/07/2017.
// Copyright © 2017 aliyun. All rights reserved.
//
import Foundation
public protocol _ExtendCustomModelType: _Transformable {
init()
mutating func willStartMapping()
mutating func mapping(mapper: HelpingMapper)
mutating func didFinishMapping()
}
extension _ExtendCustomModelType {
public mutating func willStartMapping() {}
public mutating func mapping(mapper: HelpingMapper) {}
public mutating func didFinishMapping() {}
}
fileprivate func convertKeyIfNeeded(dict: [String: Any]) -> [String: Any] {
if HandyJSONConfiguration.deserializeOptions.contains(.caseInsensitive) {
var newDict = [String: Any]()
dict.forEach({ (kvPair) in
let (key, value) = kvPair
newDict[key.lowercased()] = value
})
return newDict
}
return dict
}
fileprivate func getRawValueFrom(dict: [String: Any], property: PropertyInfo, mapper: HelpingMapper) -> Any? {
let address = Int(bitPattern: property.address)
if let mappingHandler = mapper.getMappingHandler(key: address) {
if let mappingPaths = mappingHandler.mappingPaths, mappingPaths.count > 0 {
for mappingPath in mappingPaths {
if let _value = dict.findValueBy(path: mappingPath) {
return _value
}
}
return nil
}
}
if HandyJSONConfiguration.deserializeOptions.contains(.caseInsensitive) {
return dict[property.key.lowercased()]
}
return dict[property.key]
}
fileprivate func convertValue(rawValue: Any, property: PropertyInfo, mapper: HelpingMapper) -> Any? {
if rawValue is NSNull { return nil }
if let mappingHandler = mapper.getMappingHandler(key: Int(bitPattern: property.address)), let transformer = mappingHandler.assignmentClosure {
return transformer(rawValue)
}
if let transformableType = property.type as? _Transformable.Type {
return transformableType.transform(from: rawValue)
} else {
return extensions(of: property.type).takeValue(from: rawValue)
}
}
fileprivate func assignProperty(convertedValue: Any, instance: _ExtendCustomModelType, property: PropertyInfo) {
if property.bridged {
(instance as! NSObject).setValue(convertedValue, forKey: property.key)
} else {
extensions(of: property.type).write(convertedValue, to: property.address)
}
}
fileprivate func readAllChildrenFrom(mirror: Mirror) -> [(String, Any)] {
var children = [(label: String?, value: Any)]()
let mirrorChildrenCollection = AnyRandomAccessCollection(mirror.children)!
children += mirrorChildrenCollection
var currentMirror = mirror
while let superclassChildren = currentMirror.superclassMirror?.children {
let randomCollection = AnyRandomAccessCollection(superclassChildren)!
children += randomCollection
currentMirror = currentMirror.superclassMirror!
}
var result = [(String, Any)]()
children.forEach { (child) in
if let _label = child.label {
result.append((_label, child.value))
}
}
return result
}
fileprivate func merge(children: [(String, Any)], propertyInfos: [PropertyInfo]) -> [String: (Any, PropertyInfo?)] {
var infoDict = [String: PropertyInfo]()
propertyInfos.forEach { (info) in
infoDict[info.key] = info
}
var result = [String: (Any, PropertyInfo?)]()
children.forEach { (child) in
result[child.0] = (child.1, infoDict[child.0])
}
return result
}
// this's a workaround before https://bugs.swift.org/browse/SR-5223 fixed
extension NSObject {
static func createInstance() -> NSObject {
return self.init()
}
}
extension _ExtendCustomModelType {
static func _transform(from object: Any) -> Self? {
if let dict = object as? [String: Any] {
// nested object, transform recursively
return self._transform(dict: dict) as? Self
}
return nil
}
static func _transform(dict: [String: Any]) -> _ExtendCustomModelType? {
var instance: Self
if let _nsType = Self.self as? NSObject.Type {
instance = _nsType.createInstance() as! Self
} else {
instance = Self.init()
}
instance.willStartMapping()
_transform(dict: dict, to: &instance)
instance.didFinishMapping()
return instance
}
static func _transform(dict: [String: Any], to instance: inout Self) {
guard let properties = getProperties(forType: Self.self) else {
InternalLogger.logDebug("Failed when try to get properties from type: \(type(of: Self.self))")
return
}
// do user-specified mapping first
let mapper = HelpingMapper()
instance.mapping(mapper: mapper)
// get head addr
let rawPointer = instance.headPointer()
InternalLogger.logVerbose("instance start at: ", Int(bitPattern: rawPointer))
// process dictionary
let _dict = convertKeyIfNeeded(dict: dict)
let instanceIsNsObject = instance.isNSObjectType()
let bridgedPropertyList = instance.getBridgedPropertyList()
for property in properties {
let isBridgedProperty = instanceIsNsObject && bridgedPropertyList.contains(property.key)
let propAddr = rawPointer.advanced(by: property.offset)
InternalLogger.logVerbose(property.key, "address at: ", Int(bitPattern: propAddr))
if mapper.propertyExcluded(key: Int(bitPattern: propAddr)) {
InternalLogger.logDebug("Exclude property: \(property.key)")
continue
}
let propertyDetail = PropertyInfo(key: property.key, type: property.type, address: propAddr, bridged: isBridgedProperty)
InternalLogger.logVerbose("field: ", property.key, " offset: ", property.offset, " isBridgeProperty: ", isBridgedProperty)
if let rawValue = getRawValueFrom(dict: _dict, property: propertyDetail, mapper: mapper) {
if let convertedValue = convertValue(rawValue: rawValue, property: propertyDetail, mapper: mapper) {
assignProperty(convertedValue: convertedValue, instance: instance, property: propertyDetail)
continue
}
}
InternalLogger.logDebug("Property: \(property.key) hasn't been written in")
}
}
}
extension _ExtendCustomModelType {
func _plainValue() -> Any? {
return Self._serializeAny(object: self)
}
static func _serializeAny(object: _Transformable) -> Any? {
let mirror = Mirror(reflecting: object)
guard let displayStyle = mirror.displayStyle else {
return object.plainValue()
}
// after filtered by protocols above, now we expect the type is pure struct/class
switch displayStyle {
case .class, .struct:
let mapper = HelpingMapper()
// do user-specified mapping first
if !(object is _ExtendCustomModelType) {
InternalLogger.logDebug("This model of type: \(type(of: object)) is not mappable but is class/struct type")
return object
}
let children = readAllChildrenFrom(mirror: mirror)
guard let properties = getProperties(forType: type(of: object)) else {
InternalLogger.logError("Can not get properties info for type: \(type(of: object))")
return nil
}
var mutableObject = object as! _ExtendCustomModelType
let instanceIsNsObject = mutableObject.isNSObjectType()
let head = mutableObject.headPointer()
let bridgedProperty = mutableObject.getBridgedPropertyList()
let propertyInfos = properties.map({ (desc) -> PropertyInfo in
return PropertyInfo(key: desc.key, type: desc.type, address: head.advanced(by: desc.offset),
bridged: instanceIsNsObject && bridgedProperty.contains(desc.key))
})
mutableObject.mapping(mapper: mapper)
let requiredInfo = merge(children: children, propertyInfos: propertyInfos)
return _serializeModelObject(instance: mutableObject, properties: requiredInfo, mapper: mapper) as Any
default:
return object.plainValue()
}
}
static func _serializeModelObject(instance: _ExtendCustomModelType, properties: [String: (Any, PropertyInfo?)], mapper: HelpingMapper) -> [String: Any] {
var dict = [String: Any]()
for (key, property) in properties {
var realKey = key
var realValue = property.0
if let info = property.1 {
if info.bridged, let _value = (instance as! NSObject).value(forKey: key) {
realValue = _value
}
if mapper.propertyExcluded(key: Int(bitPattern: info.address)) {
continue
}
if let mappingHandler = mapper.getMappingHandler(key: Int(bitPattern: info.address)) {
// if specific key is set, replace the label
if let mappingPaths = mappingHandler.mappingPaths, mappingPaths.count > 0 {
// take the first path, last segment if more than one
realKey = mappingPaths[0].segments.last!
}
if let transformer = mappingHandler.takeValueClosure {
if let _transformedValue = transformer(realValue) {
dict[realKey] = _transformedValue
}
continue
}
}
}
if let typedValue = realValue as? _Transformable {
if let result = self._serializeAny(object: typedValue) {
dict[realKey] = result
continue
}
}
InternalLogger.logDebug("The value for key: \(key) is not transformable type")
}
return dict
}
}