generated from dankinsoid/iOSLibraryTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
TypeRevisionEncoder.swift
392 lines (322 loc) · 12.2 KB
/
TypeRevisionEncoder.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import Foundation
final class TypeRevisionEncoder: Encoder {
var path: [TypePath]
var codingPath: [CodingKey] { path.map(\.key) }
var userInfo: [CodingUserInfoKey: Any]
var result: TypeInfo
var context: TypeRevision
init(
path: [TypePath] = [],
context: TypeRevision
) {
self.path = path
self.context = context
userInfo = [:]
result = TypeInfo(type: Any.self, isOptional: false, container: .single(.null))
}
func container<Key>(keyedBy _: Key.Type) -> KeyedEncodingContainer<Key> where Key: CodingKey {
let container = TypeRevisionKeyedEncodingContainer<Key>(
path: path,
encoder: self,
result: Ref(self, \.result.container.keyed)
)
return KeyedEncodingContainer(container)
}
func unkeyedContainer() -> UnkeyedEncodingContainer {
TypeRevisionSingleValueEncodingContainer(
isSingle: false,
path: path,
encoder: self,
result: Ref { [self] in
result.container.unkeyed ?? .any
} set: { [self] newValue in
result.container = .unkeyed(newValue)
}
)
}
func singleValueContainer() -> SingleValueEncodingContainer {
TypeRevisionSingleValueEncodingContainer(
isSingle: true,
path: path,
encoder: self,
result: Ref(self, \.result)
)
}
@discardableResult
func encode(_ value: Encodable?, type: Encodable.Type) throws -> TypeInfo {
if !path.isEmpty, path.dropLast().contains(where: { path in path.type == type }) {
result.container = .recursive
} else if let container = context.customDescription(type, value) {
result = TypeInfo(type: type, isOptional: value == nil, container: container)
} else if let value {
try value.encode(to: self)
if case let .keyed(fields) = result.container, let decodable = type as? Decodable.Type {
if fields.fields.isEmpty {
let decoder = TypeRevisionDecoder(path: path, context: context)
_ = try? decodable.init(from: decoder)
result = decoder.result
} else {
let decoder = CheckAllKeysDecoder()
_ = try? decodable.init(from: decoder)
result.container.keyed.isFixed = !decoder.isAdditional
}
}
if
let collection = value as? any Collection,
collection.isEmpty,
let decodable = type as? Decodable.Type
{
let decoder = TypeRevisionDecoder(path: path, context: context)
_ = try? decodable.init(from: decoder)
result = decoder.result
}
} else if let decodable = type as? Decodable.Type {
let decoder = TypeRevisionDecoder(path: path, context: context)
_ = try? decoder.decode(decodable)
result = decoder.result
} else {
result.container = .single(.null)
}
result.type = type
return result
}
}
private struct TypeRevisionSingleValueEncodingContainer: SingleValueEncodingContainer, UnkeyedEncodingContainer {
var count: Int { 1 }
let isSingle: Bool
var path: [TypePath]
var codingPath: [CodingKey] { path.map(\.key) }
var encoder: TypeRevisionEncoder
@Ref var result: TypeInfo
mutating func encodeNil() throws {
result.isOptional = true
}
mutating func encode(_ value: Bool) throws {
result = TypeInfo(type: Bool.self, container: .single(.bool(value)))
}
mutating func encode(_ value: String) throws {
result = TypeInfo(type: String.self, container: .single(.string(value)))
}
mutating func encode(_ value: Double) throws {
result = TypeInfo(type: Double.self, container: .single(.double(value)))
}
mutating func encode(_ value: Float) throws {
result = TypeInfo(type: Float.self, container: .single(.float(value)))
}
mutating func encode(_ value: Int) throws {
result = TypeInfo(type: Int.self, container: .single(.int(value)))
}
mutating func encode(_ value: Int8) throws {
result = TypeInfo(type: Int8.self, container: .single(.int8(value)))
}
mutating func encode(_ value: Int16) throws {
result = TypeInfo(type: Int16.self, container: .single(.int16(value)))
}
mutating func encode(_ value: Int32) throws {
result = TypeInfo(type: Int32.self, container: .single(.int32(value)))
}
mutating func encode(_ value: Int64) throws {
result = TypeInfo(type: Int64.self, container: .single(.int64(value)))
}
mutating func encode(_ value: UInt) throws {
result = TypeInfo(type: UInt.self, container: .single(.uint(value)))
}
mutating func encode(_ value: UInt8) throws {
result = TypeInfo(type: UInt8.self, container: .single(.uint8(value)))
}
mutating func encode(_ value: UInt16) throws {
result = TypeInfo(type: UInt16.self, container: .single(.uint16(value)))
}
mutating func encode(_ value: UInt32) throws {
result = TypeInfo(type: UInt32.self, container: .single(.uint32(value)))
}
mutating func encode(_ value: UInt64) throws {
result = TypeInfo(type: UInt64.self, container: .single(.uint64(value)))
}
mutating func encode<T>(_ value: T) throws where T: Encodable {
result = try TypeRevisionEncoder(
path: nestedPath(for: T.self),
context: encoder.context
)
.encode(value, type: T.self)
}
mutating func nestedContainer<NestedKey>(keyedBy _: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> where NestedKey: CodingKey {
result.container = .keyed(KeyedInfo())
let container = TypeRevisionKeyedEncodingContainer<NestedKey>(
path: nestedPath(for: [String: Any].self),
encoder: encoder,
result: Ref(self, \.result.container.keyed)
)
return KeyedEncodingContainer(container)
}
mutating func nestedUnkeyedContainer() -> UnkeyedEncodingContainer {
result.container = .unkeyed(.any)
return TypeRevisionSingleValueEncodingContainer(
isSingle: false,
path: nestedPath(for: [Any].self),
encoder: encoder,
result: Ref { [$result] in
$result.wrappedValue.container.unkeyed ?? .any
} set: { [$result] newValue in
$result.wrappedValue.container.unkeyed = newValue
}
)
}
mutating func superEncoder() -> Encoder {
TypeRevisionEncoder(path: path, context: encoder.context)
}
private func nestedPath(for type: Any.Type) -> [TypePath] {
isSingle ? path : path + [TypePath(type: type, key: IntKey(intValue: count))]
}
}
private struct TypeRevisionKeyedEncodingContainer<Key: CodingKey>: KeyedEncodingContainerProtocol {
var path: [TypePath]
var codingPath: [CodingKey] { path.map(\.key) }
var encoder: TypeRevisionEncoder
@Ref var result: KeyedInfo
@inline(__always)
private func str(_ key: Key) -> String {
key.stringValue
}
mutating func encodeNil(forKey key: Key) throws {
result[str(key)].isOptional = true
}
mutating func encode(_ value: Bool, forKey key: Key) throws {
try encode(.bool(value), forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: Bool?, forKey key: Key) throws {
try encode(.bool(value), forKey: key, optional: true)
}
mutating func encode(_ value: String, forKey key: Key) throws {
try encode(.string(value), forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: String?, forKey key: Key) throws {
try encode(.string(value), forKey: key, optional: true)
}
mutating func encode(_ value: Double, forKey key: Key) throws {
try encode(.double(value), forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: Double?, forKey key: Key) throws {
try encode(.double(value), forKey: key, optional: true)
}
mutating func encode(_ value: Float, forKey key: Key) throws {
try encode(.float(value), forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: Float?, forKey key: Key) throws {
try encode(.float(value), forKey: key, optional: true)
}
mutating func encode(_ value: Int, forKey key: Key) throws {
try encode(.int(value), forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: Int?, forKey key: Key) throws {
try encode(.int(value), forKey: key, optional: true)
}
mutating func encode(_ value: Int8, forKey key: Key) throws {
try encode(.int8(value), forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: Int8?, forKey key: Key) throws {
try encode(.int8(value), forKey: key, optional: true)
}
mutating func encode(_ value: Int16, forKey key: Key) throws {
try encode(.int16(value), forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: Int16?, forKey key: Key) throws {
try encode(.int16(value), forKey: key, optional: true)
}
mutating func encode(_ value: Int32, forKey key: Key) throws {
try encode(.int32(value), forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: Int32?, forKey key: Key) throws {
try encode(.int32(value), forKey: key, optional: true)
}
mutating func encode(_ value: Int64, forKey key: Key) throws {
try encode(.int64(value), forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: Int64?, forKey key: Key) throws {
try encode(.int64(value), forKey: key, optional: true)
}
mutating func encode(_ value: UInt, forKey key: Key) throws {
try encode(.uint(value), forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: UInt?, forKey key: Key) throws {
try encode(.uint(value), forKey: key, optional: true)
}
mutating func encode(_ value: UInt8, forKey key: Key) throws {
try encode(.uint8(value), forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: UInt8?, forKey key: Key) throws {
try encode(.uint8(value), forKey: key, optional: true)
}
mutating func encode(_ value: UInt16, forKey key: Key) throws {
try encode(.uint16(value), forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: UInt16?, forKey key: Key) throws {
try encode(.uint16(value), forKey: key, optional: true)
}
mutating func encode(_ value: UInt32, forKey key: Key) throws {
try encode(.uint32(value), forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: UInt32?, forKey key: Key) throws {
try encode(.uint32(value), forKey: key, optional: true)
}
mutating func encode(_ value: UInt64, forKey key: Key) throws {
try encode(.uint64(value), forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: UInt64?, forKey key: Key) throws {
try encode(.uint64(value), forKey: key, optional: true)
}
mutating func encode(_ value: some Encodable, forKey key: Key) throws {
try encode(value, forKey: key, optional: false)
}
mutating func encodeIfPresent(_ value: (some Encodable)?, forKey key: Key) throws {
try encode(value, forKey: key, optional: true)
}
private mutating func encode<T>(_ value: T?, forKey key: Key, optional: Bool) throws where T: Encodable {
let encoder = TypeRevisionEncoder(
path: nestedPath(for: key, optional ? Optional<T>.self : T.self),
context: encoder.context
)
var info = try encoder.encode(value, type: T.self)
info.isOptional = optional
info.type = T.self
result[str(key)] = info
}
mutating func nestedContainer<NestedKey: CodingKey>(keyedBy _: NestedKey.Type, forKey key: Key) -> KeyedEncodingContainer<NestedKey> {
let strKey = str(key)
let container = TypeRevisionKeyedEncodingContainer<NestedKey>(
path: nestedPath(for: key, [String: Any].self),
encoder: encoder,
result: Ref(self, \.result[strKey].container.keyed)
)
result[strKey].container = .keyed(KeyedInfo())
return KeyedEncodingContainer(container)
}
mutating func nestedUnkeyedContainer(forKey key: Key) -> UnkeyedEncodingContainer {
let strKey = str(key)
let container = TypeRevisionSingleValueEncodingContainer(
isSingle: false,
path: nestedPath(for: key, [Any].self),
encoder: encoder,
result: Ref { [$result] in
$result.wrappedValue[strKey].container.unkeyed ?? .any
} set: { [$result] newValue in
$result.wrappedValue[strKey].container.unkeyed = newValue
}
)
result[strKey].container = .unkeyed(.any)
return container
}
mutating func superEncoder() -> Encoder {
TypeRevisionEncoder(path: path, context: encoder.context)
}
mutating func superEncoder(forKey _: Key) -> Encoder {
TypeRevisionEncoder(path: path, context: encoder.context)
}
private func nestedPath(for key: Key, _ type: Any.Type) -> [TypePath] {
path + [TypePath(type: type, key: key)]
}
@inline(__always)
private mutating func encode(_ type: CodableValues, forKey key: Key, optional: Bool) throws {
result[str(key)] = TypeInfo(type: type.type, isOptional: optional, container: .single(type))
}
}