-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCodable.swift
1227 lines (1043 loc) · 48.2 KB
/
Codable.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Codable.swift
// LucidCodeGenCore
//
// Created by Théophane Rupin on 3/27/19.
//
import Foundation
import PathKit
// MARK: - Defaults
public enum DescriptionDefaults {
public static let identifier = EntityIdentifier(
identifierType: .void,
equivalentIdentifierName: nil,
objc: DescriptionDefaults.objc,
atomic: nil
)
public static let remote = true
public static let persist = false
public static let useForEquality = true
public static let idOnly = false
public static let idKey = "id"
public static let failableItems = true
public static let isTarget = false
public static let nullable = false
public static let mutable = false
public static let objc = false
public static let objcNoneCase = false
public static let unused = false
public static let logError = true
public static let lazy = false
public static let matchExactKey = false
public static let platforms = Set<Platform>()
public static let lastRemoteRead = false
public static let queryContext = false
public static let clientQueueName = Entity.mainClientQueueName
public static let ignoreMigrationChecks = false
public static let ignorePropertyMigrationChecksOn = [String]()
public static let httpMethod: EndpointPayloadTest.HTTPMethod = .get
public static let cacheSize: EntityCacheSize = .group(.medium)
public static let sendable = false
}
public extension Entity {
static let mainClientQueueName = "main"
}
// MARK: - Payloads
extension EndpointPayload: Codable {
private enum Keys: String, CodingKey {
case name
case read
case write
case readWrite
case tests
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
name = try container.decode(String.self, forKey: .name)
if let readWriteValue = try container.decodeIfPresent(ReadWriteEndpointPayload.self, forKey: .readWrite) {
readPayload = readWriteValue
writePayload = readWriteValue
} else {
readPayload = try container.decodeIfPresent(ReadWriteEndpointPayload.self, forKey: .read)
writePayload = try container.decodeIfPresent(ReadWriteEndpointPayload.self, forKey: .write)
}
tests = try container.decodeIfPresent(EndpointPayloadTests.self, forKey: .tests)
if readPayload == nil && writePayload == nil {
throw CodeGenError.endpointRequiresAtLeastOnePayload(name)
}
if try container.decodeIfPresent(ReadWriteEndpointPayload.self, forKey: .readWrite) != nil,
(readPayload?.httpMethod != nil || writePayload?.httpMethod != nil) {
throw CodeGenError.endpointRequiresSeparateReadAndWritePayloads(name)
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(name, forKey: .name)
try container.encodeIfPresent(readPayload, forKey: .read)
try container.encodeIfPresent(writePayload, forKey: .write)
try container.encodeIfPresent(tests, forKey: .tests)
}
}
extension ReadWriteEndpointPayload: Codable {
private enum Keys: String, CodingKey {
case baseKey
case entity
case entityVariations
case excludedPaths
case metadata
case httpMethod
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
baseKey = try container.decodeIfPresent(BaseKey.self, forKey: .baseKey)
entity = try container.decode(EndpointPayloadEntity.self, forKey: .entity)
entityVariations = try container.decodeIfPresent([EndpointPayloadEntityVariation].self, forKey: .entityVariations)
excludedPaths = try container.decodeIfPresent([String].self, forKey: .excludedPaths) ?? []
metadata = try container.decodeIfPresent([MetadataProperty].self, forKey: .metadata)
httpMethod = try container.decodeIfPresent(HTTPMethod.self, forKey: .httpMethod)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encodeIfPresent(baseKey, forKey: .baseKey)
try container.encode(entity, forKey: .entity)
try container.encodeIfPresent(entityVariations, forKey: .entityVariations)
try container.encodeIfPresent(excludedPaths.isEmpty ? nil : excludedPaths, forKey: .excludedPaths)
try container.encodeIfPresent(metadata, forKey: .metadata)
try container.encodeIfPresent(httpMethod, forKey: .httpMethod)
}
}
extension ReadWriteEndpointPayload.BaseKey: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let arrayBaseKey = try? container.decode([String].self) {
self = .array(arrayBaseKey)
} else {
self = .single(try container.decode(String.self))
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .single(let key):
try container.encode(key)
case .array(let keys):
try container.encode(keys)
}
}
}
extension EndpointPayloadTests: Codable {
private enum Keys: String, CodingKey {
case read
case write
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
readTests = try container.decodeIfPresent([EndpointPayloadTest].self, forKey: .read) ?? []
writeTests = try container.decodeIfPresent([EndpointPayloadTest].self, forKey: .write) ?? []
if readTests.isEmpty && writeTests.isEmpty {
throw CodeGenError.endpointTestsRequiresAtLeastOneType
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encodeIfPresent(readTests.isEmpty ? nil : readTests, forKey: .read)
try container.encodeIfPresent(writeTests.isEmpty ? nil : writeTests, forKey: .write)
}
}
extension EndpointPayloadTest: Codable {
private enum Keys: String, CodingKey {
case name
case url
case httpMethod
case body
case contexts
case endpoints
case entities
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
name = try container.decode(String.self, forKey: .name)
url = try container.decode(URL.self, forKey: .url)
httpMethod = try container.decodeIfPresent(HTTPMethod.self, forKey: .httpMethod) ?? .get
body = try container.decodeIfPresent(String.self, forKey: .body)
entities = try container.decode([Entity].self, forKey: .entities)
// for parsing from previous versions
do {
contexts = try container.decode([String].self, forKey: .contexts)
endpoints = []
} catch {
endpoints = try container.decode([String].self, forKey: .endpoints)
contexts = []
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(name, forKey: .name)
try container.encode(url, forKey: .url)
try container.encodeIfPresent(httpMethod == DescriptionDefaults.httpMethod ? nil : httpMethod, forKey: .httpMethod)
try container.encodeIfPresent(body, forKey: .body)
try container.encode(entities, forKey: .entities)
try container.encodeIfPresent(contexts, forKey: .contexts)
try container.encodeIfPresent(endpoints, forKey: .endpoints)
}
}
extension EndpointPayloadTest.HTTPMethod: Codable { }
extension EndpointPayloadTest.Entity: Codable {
private enum Keys: String, CodingKey {
case name
case count
case isTarget
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
name = try container.decode(String.self, forKey: .name)
count = try container.decodeIfPresent(Int.self, forKey: .count)
isTarget = try container.decodeIfPresent(Bool.self, forKey: .isTarget) ?? DescriptionDefaults.isTarget
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(name, forKey: .name)
try container.encodeIfPresent(count, forKey: .count)
try container.encodeIfPresent(isTarget == DescriptionDefaults.isTarget ? nil : isTarget, forKey: .isTarget)
}
}
extension EndpointPayloadEntity: Codable {
private enum Keys: String, CodingKey {
case entityKey
case entityName
case structure
case nullable
case legacyOptional = "optional"
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
entityKey = try container.decodeIfPresent(String.self, forKey: .entityKey)
entityName = try container.decode(String.self, forKey: .entityName)
structure = try container.decode(Structure.self, forKey: .structure)
nullable = try container.decodeIfPresent(Bool.self, forKey: .nullable) ?? container.decodeIfPresent(Bool.self, forKey: .legacyOptional) ?? DescriptionDefaults.nullable
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encodeIfPresent(entityKey, forKey: .entityKey)
try container.encode(entityName, forKey: .entityName)
try container.encode(structure, forKey: .structure)
try container.encodeIfPresent(nullable == DescriptionDefaults.nullable ? nil : nullable, forKey: .nullable)
}
}
extension EndpointPayloadEntity.Structure: Codable {}
// MARK: - EntityCacheSize
extension EntityCacheSize: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let groupValue = try? container.decode(Group.self) {
self = .group(groupValue)
} else {
let intValue = try container.decode(Int.self)
self = .fixed(intValue)
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .group(let groupName):
try container.encode(groupName)
case .fixed(let value):
try container.encode(value)
}
}
}
// MARK: - Entity
extension Entity: Codable {
private enum Keys: String, CodingKey {
case name
case remote
case persist
case identifier
case metadata
case properties
case systemProperties
case uid
case legacyPreviousName
case previousName
case addedAtVersion
case versionHistory
case persistedName
case platforms
case lastRemoteRead
case queryContext
case clientQueueName
case cacheSize
case sendable
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
let name = try container.decode(String.self, forKey: .name)
self.name = name
remote = try container.decodeIfPresent(Bool.self, forKey: .remote) ?? DescriptionDefaults.remote
persist = try container.decodeIfPresent(Bool.self, forKey: .persist) ?? DescriptionDefaults.persist
identifier = try container.decodeIfPresent(EntityIdentifier.self, forKey: .identifier) ?? DescriptionDefaults.identifier
metadata = try container.decodeIfPresent([MetadataProperty].self, forKey: .metadata)
properties = try container.decode([EntityProperty].self, forKey: .properties).sorted(by: { $0.name < $1.name })
systemProperties = try container.decodeIfPresent([SystemProperty].self, forKey: .systemProperties)?.sorted(by: { $0.name.rawValue < $1.name.rawValue }) ?? []
identifierTypeID = try container.decodeIfPresent(String.self, forKey: .uid)
legacyPreviousName = try container.decodeIfPresent(String.self, forKey: .legacyPreviousName) ?? container.decodeIfPresent(String.self, forKey: .previousName)
versionHistory = try container.decodeIfPresent([VersionHistoryItem].self, forKey: .versionHistory) ?? []
if versionHistory.isEmpty {
legacyAddedAtVersion = try container.decodeIfPresent(Version.self, forKey: .addedAtVersion)
} else {
legacyAddedAtVersion = nil
}
persistedName = try container.decodeIfPresent(String.self, forKey: .persistedName)
platforms = try container.decodeIfPresent(Set<Platform>.self, forKey: .platforms) ?? DescriptionDefaults.platforms
queryContext = try container.decodeIfPresent(Bool.self, forKey: .queryContext) ?? DescriptionDefaults.queryContext
clientQueueName = try container.decodeIfPresent(String.self, forKey: .clientQueueName) ?? DescriptionDefaults.clientQueueName
cacheSize = try container.decodeIfPresent(EntityCacheSize.self, forKey: .cacheSize) ?? DescriptionDefaults.cacheSize
senable = try container.decodeIfPresent(Bool.self, forKey: .sendable) ?? DescriptionDefaults.sendable
let systemPropertiesSet = Set(SystemPropertyName.allCases.map { $0.rawValue })
for property in properties where systemPropertiesSet.contains(property.name) {
throw CodeGenError.systemPropertyNameCollision(property.name)
}
if let legacyLastRemoteRead = try container.decodeIfPresent(Bool.self, forKey: .lastRemoteRead) {
let systemPropertyNames = systemProperties.map { $0.name }
guard systemPropertyNames.contains(.lastRemoteRead) == false else {
throw CodeGenError.incompatiblePropertyKey("last_remote_read")
}
if legacyLastRemoteRead {
systemProperties.append(SystemProperty(name: .lastRemoteRead, useCoreDataLegacyNaming: true, addedAtVersion: nil))
}
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(name, forKey: .name)
try container.encode(remote, forKey: .remote)
try container.encode(persist, forKey: .persist)
try container.encode(identifier, forKey: .identifier)
try container.encodeIfPresent(metadata, forKey: .metadata)
try container.encode(properties, forKey: .properties)
try container.encodeIfPresent(systemProperties.isEmpty ? nil : systemProperties, forKey: .systemProperties)
try container.encodeIfPresent(identifierTypeID, forKey: .uid)
try container.encodeIfPresent(versionHistory.isEmpty ? nil : versionHistory, forKey: .versionHistory)
try container.encodeIfPresent(legacyPreviousName, forKey: .legacyPreviousName)
try container.encodeIfPresent(persistedName, forKey: .persistedName)
try container.encodeIfPresent(platforms == DescriptionDefaults.platforms ? nil : platforms, forKey: .platforms)
try container.encodeIfPresent(queryContext == DescriptionDefaults.queryContext ? nil : queryContext, forKey: .queryContext)
try container.encodeIfPresent(clientQueueName == DescriptionDefaults.clientQueueName ? nil : clientQueueName, forKey: .clientQueueName)
}
}
extension VersionHistoryItem: Codable {
private enum Keys: String, CodingKey {
case version
case previousName
case ignoreMigrationChecks
case ignorePropertyMigrationChecksOn
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
version = try container.decode(Version.self, forKey: .version)
previousName = try container.decodeIfPresent(String.self, forKey: .previousName)
ignoreMigrationChecks = try container.decodeIfPresent(Bool.self, forKey: .ignoreMigrationChecks) ?? DescriptionDefaults.ignoreMigrationChecks
ignorePropertyMigrationChecksOn = try container.decodeIfPresent([String].self, forKey: .ignorePropertyMigrationChecksOn) ?? []
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(version, forKey: .version)
try container.encodeIfPresent(previousName, forKey: .previousName)
try container.encodeIfPresent(ignoreMigrationChecks == DescriptionDefaults.ignoreMigrationChecks ? nil : ignoreMigrationChecks, forKey: .ignoreMigrationChecks)
try container.encodeIfPresent(ignorePropertyMigrationChecksOn == DescriptionDefaults.ignorePropertyMigrationChecksOn ? nil : ignorePropertyMigrationChecksOn, forKey: .ignorePropertyMigrationChecksOn)
}
}
extension EndpointPayloadEntityVariation: Codable {
private enum Keys: String, CodingKey {
case entityName
case propertyRenames
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
self.entityName = try container.decode(String.self, forKey: .entityName)
self.propertyRenames = try container.decodeIfPresent([Rename].self, forKey: .propertyRenames)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(entityName, forKey: .entityName)
try container.encodeIfPresent(propertyRenames, forKey: .propertyRenames)
}
}
extension EntityIdentifier: Codable {
private enum Keys: String, CodingKey {
case key
case type
case derivedFromRelationships
case equivalentToIdentifierOf
case propertyName
case objc
case atomic
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
key = try container.decodeIfPresent(String.self, forKey: .key) ?? DescriptionDefaults.idKey
objc = try container.decodeIfPresent(Bool.self, forKey: .objc) ?? DescriptionDefaults.objc
let lowerCaseType = try container.decodeIfPresent(String.self, forKey: .type)
switch lowerCaseType {
case .some("property"):
identifierType = .property(try container.decode(String.self, forKey: .propertyName))
case .none:
identifierType = .void
case .some(let lowerCaseType):
do {
let relationshipIDs: [EntityIdentifierType.RelationshipID] = try container
.decode([String].self, forKey: .derivedFromRelationships)
.map { entityName in
EntityIdentifierType.RelationshipID(variableName: entityName, entityName: entityName)
}
guard let scalarType = PropertyScalarType(lowerCaseType) else {
throw DecodingError.dataCorruptedError(forKey: Keys.type, in: container, debugDescription: "Unknown value type \(lowerCaseType.capitalized).")
}
identifierType = .relationships(scalarType, relationshipIDs)
} catch {
guard let scalarType = PropertyScalarType(lowerCaseType) else {
throw DecodingError.dataCorruptedError(forKey: Keys.type, in: container, debugDescription: "Unknown value type \(lowerCaseType.capitalized).")
}
identifierType = .scalarType(scalarType)
}
}
equivalentIdentifierName = try container.decodeIfPresent(String.self, forKey: .equivalentToIdentifierOf)
atomic = try container.decodeIfPresent(Bool.self, forKey: .atomic)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(key, forKey: .key)
try container.encodeIfPresent(objc == DescriptionDefaults.objc ? nil : objc, forKey: .objc)
try container.encodeIfPresent(equivalentIdentifierName, forKey: .equivalentToIdentifierOf)
switch identifierType {
case .property(let name):
try container.encode("property", forKey: .type)
try container.encode(name, forKey: .propertyName)
case .relationships(let scalarType, let relationshipIDs):
try container.encode(scalarType.stringValue, forKey: .type)
try container.encode(relationshipIDs.map { $0.entityName }, forKey: .derivedFromRelationships)
case .scalarType(let scalarType):
try container.encode(scalarType.rawValue.lowercased(), forKey: .type)
case .void:
break
}
}
}
extension DefaultValue: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let value = try? container.decode(Bool.self) {
self = .bool(value)
} else if let value = try? container.decode(Int.self) {
self = .int(value)
} else if let value = try? container.decode(Float.self) {
self = .float(value)
} else if let value = try? container.decode(Date.self) {
self = .date(value)
} else if let value = try? container.decode(String.self) {
if value == "current_date" {
self = .currentDate
} else if value == "nil" {
self = .nil
} else if value.reversed().starts(with: "s") {
var value = value
value.removeLast()
self = .seconds(Float(value) ?? 0)
} else if value.reversed().starts(with: "ms".reversed()) {
var value = value
value.removeLast(2)
self = .milliseconds(Float(value) ?? 0)
} else if value.starts(with: ".") {
var value = value
value.removeFirst()
self = .enumCase(value)
} else {
self = .string(value)
}
} else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Unknown default value type.")
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(description)
}
}
extension MetadataProperty: Codable {
private enum Keys: String, CodingKey {
case name
case key
case propertyType
case nullable
case legacyOptional = "optional"
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
name = try container.decode(String.self, forKey: .name)
let value = try container.decode(String.self, forKey: .propertyType)
let propertyType: PropertyType = PropertyScalarType(value.arrayElementType()).flatMap {
.scalar($0)
} ?? .subtype(value.arrayElementType())
if value.isArray {
self.propertyType = .array(propertyType)
} else {
self.propertyType = propertyType
}
nullable = try container.decodeIfPresent(Bool.self, forKey: .nullable) ?? container.decodeIfPresent(Bool.self, forKey: .legacyOptional) ?? DescriptionDefaults.nullable
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(name, forKey: .name)
func propertyTypeString(_ propertyType: PropertyType) -> String {
switch propertyType {
case .array(let propertyType):
return "[\(propertyTypeString(propertyType))]"
case .scalar(let scalarType):
return scalarType.stringValue
case .subtype(let subtype):
return subtype
}
}
try container.encode(propertyTypeString(propertyType), forKey: .propertyType)
try container.encodeIfPresent(nullable == DescriptionDefaults.nullable ? nil : nullable, forKey: .nullable)
}
}
extension EntityProperty: Codable {
private enum Keys: String, CodingKey {
case name
case previousName
case addedAtVersion
case key
case propertyType
case nullable
case legacyOptional = "optional"
case defaultValue
case logError
case useForEquality
case mutable
case objc
case unused
case lazy
case legacyExtra = "extra"
case matchExactKey
case platforms
case persistedName
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
name = try container.decode(String.self, forKey: .name)
previousName = try container.decodeIfPresent(String.self, forKey: .previousName)
addedAtVersion = try container.decodeIfPresent(Version.self, forKey: .addedAtVersion)
do {
let relationship = try container.decode(EntityRelationship.self, forKey: .propertyType)
propertyType = .relationship(relationship)
} catch {
let value = try container.decode(String.self, forKey: .propertyType)
let propertyType: PropertyType
if let scalarType = PropertyScalarType(value.arrayElementType()) {
propertyType = .scalar(scalarType)
} else {
propertyType = .subtype(value.arrayElementType())
}
if value.isArray {
self.propertyType = .array(propertyType)
} else {
self.propertyType = propertyType
}
}
key = try container.decodeIfPresent(String.self, forKey: .key) ?? container.decode(String.self, forKey: .name)
matchExactKey = try container.decodeIfPresent(Bool.self, forKey: .matchExactKey) ?? DescriptionDefaults.matchExactKey
nullable = try container.decodeIfPresent(Bool.self, forKey: .nullable) ?? container.decodeIfPresent(Bool.self, forKey: .legacyOptional) ?? DescriptionDefaults.nullable
defaultValue = try container.decodeIfPresent(DefaultValue.self, forKey: .defaultValue)
logError = try container.decodeIfPresent(Bool.self, forKey: .logError) ?? DescriptionDefaults.logError
useForEquality = try container.decodeIfPresent(Bool.self, forKey: .useForEquality) ?? DescriptionDefaults.useForEquality
mutable = try container.decodeIfPresent(Bool.self, forKey: .mutable) ?? DescriptionDefaults.mutable
objc = try container.decodeIfPresent(Bool.self, forKey: .objc) ?? DescriptionDefaults.objc
unused = try container.decodeIfPresent(Bool.self, forKey: .unused) ?? DescriptionDefaults.unused
lazy = try container.decodeIfPresent(Bool.self, forKey: .lazy) ?? container.decodeIfPresent(Bool.self, forKey: .legacyExtra) ?? DescriptionDefaults.lazy
platforms = try container.decodeIfPresent(Set<Platform>.self, forKey: .platforms) ?? DescriptionDefaults.platforms
persistedName = try container.decodeIfPresent(String.self, forKey: .persistedName)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(name, forKey: .name)
try container.encodeIfPresent(previousName, forKey: .previousName)
try container.encodeIfPresent(addedAtVersion, forKey: .addedAtVersion)
func propertyTypeString(_ propertyType: PropertyType) -> String {
switch propertyType {
case .array(let propertyType):
return "[\(propertyTypeString(propertyType))]"
case .scalar(let scalarType):
return scalarType.stringValue
case .subtype(let subType):
return subType
case .relationship:
return String()
}
}
switch propertyType {
case .relationship(let relationship):
try container.encode(relationship, forKey: .propertyType)
default:
try container.encode(propertyTypeString(propertyType), forKey: .propertyType)
}
try container.encodeIfPresent(key == name ? nil : key, forKey: .key)
try container.encodeIfPresent(matchExactKey == DescriptionDefaults.matchExactKey ? nil : matchExactKey, forKey: .matchExactKey)
try container.encodeIfPresent(nullable == DescriptionDefaults.nullable ? nil : nullable, forKey: .nullable)
try container.encodeIfPresent(defaultValue, forKey: .defaultValue)
try container.encodeIfPresent(logError == DescriptionDefaults.logError ? nil : logError, forKey: .logError)
try container.encodeIfPresent(useForEquality == DescriptionDefaults.useForEquality ? nil : useForEquality, forKey: .useForEquality)
try container.encodeIfPresent(mutable == DescriptionDefaults.mutable ? nil : mutable, forKey: .mutable)
try container.encodeIfPresent(objc == DescriptionDefaults.objc ? nil : objc, forKey: .objc)
try container.encodeIfPresent(unused == DescriptionDefaults.unused ? nil : unused, forKey: .unused)
try container.encodeIfPresent(lazy == DescriptionDefaults.lazy ? nil : lazy, forKey: .lazy)
try container.encodeIfPresent(platforms == DescriptionDefaults.platforms ? nil : platforms.sorted(), forKey: .platforms)
try container.encodeIfPresent(persistedName, forKey: .persistedName)
}
}
extension EntityRelationship: Codable {
private enum Keys: String, CodingKey {
case entityName
case association
case idOnly
case failableItems
case platforms
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
entityName = try container.decode(String.self, forKey: .entityName)
association = try container.decode(Association.self, forKey: .association)
idOnly = try container.decodeIfPresent(Bool.self, forKey: .idOnly) ?? DescriptionDefaults.idOnly
failableItems = try container.decodeIfPresent(Bool.self, forKey: .failableItems) ?? DescriptionDefaults.failableItems
platforms = try container.decodeIfPresent([Platform].self, forKey: .platforms)?.sorted() ?? Array(DescriptionDefaults.platforms)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(entityName, forKey: .entityName)
try container.encode(association, forKey: .association)
try container.encodeIfPresent(idOnly == DescriptionDefaults.idOnly ? nil : idOnly, forKey: .idOnly)
try container.encodeIfPresent(failableItems == DescriptionDefaults.failableItems ? nil : failableItems, forKey: .failableItems)
try container.encodeIfPresent(platforms == Array(DescriptionDefaults.platforms) ? nil : platforms.sorted(), forKey: .platforms)
}
}
extension EntityRelationship.Association: Codable {}
// MARK: - Subtype
extension Subtype: Codable {
private enum Keys: String, CodingKey {
case name
case customDecoder
case cases
case unusedCases
case options
case unusedOptions
case properties
case manualImplementations
case objc
case objcNoneCase
case platforms
case sendable
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
name = try container.decode(String.self, forKey: .name)
manualImplementations = Set(try container.decodeIfPresent([`Protocol`].self, forKey: .manualImplementations) ?? [])
platforms = try container.decodeIfPresent(Set<Platform>.self, forKey: .platforms) ?? DescriptionDefaults.platforms
sendable = try container.decodeIfPresent(Bool.self, forKey: .sendable) ?? DescriptionDefaults.sendable
if let usedCases = try container.decodeIfPresent([String].self, forKey: .cases) {
let unusedCases = try container.decodeIfPresent([String].self, forKey: .unusedCases) ?? []
objc = try container.decodeIfPresent(Bool.self, forKey: .objc) ?? DescriptionDefaults.objc
let objcNoneCase = try container.decodeIfPresent(Bool.self, forKey: .objcNoneCase) ?? DescriptionDefaults.objcNoneCase
items = .cases(
used: usedCases.sorted(),
unused: unusedCases.sorted(),
objcNoneCase: objcNoneCase
)
} else if let options = try container.decodeIfPresent([String].self, forKey: .options) {
let unusedOptions = try container.decodeIfPresent([String].self, forKey: .unusedOptions) ?? []
objc = try container.decodeIfPresent(Bool.self, forKey: .objc) ?? DescriptionDefaults.objc
items = .options(
used: options,
unused: unusedOptions
)
} else if let properties = try container.decodeIfPresent([Property].self, forKey: .properties) {
items = .properties(properties.filter { !$0.unused }.sorted { $0.name < $1.name })
objc = try container.decodeIfPresent(Bool.self, forKey: .objc) ?? properties.contains { $0.objc }
} else {
throw DecodingError.dataCorrupted(.init(
codingPath: [Keys.cases, Keys.options, Keys.properties],
debugDescription: "No items key was found."
))
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(name, forKey: .name)
try container.encode(manualImplementations, forKey: .manualImplementations)
try container.encode(platforms, forKey: .platforms)
try container.encode(objc, forKey: .objc)
switch items {
case .cases(let used, let unused, let objcNoneCase):
try container.encode(used, forKey: .cases)
try container.encode(unused, forKey: .unusedCases)
try container.encode(objcNoneCase, forKey: .objcNoneCase)
case .options(let used, let unused):
try container.encode(used, forKey: .options)
try container.encode(unused, forKey: .unusedOptions)
case .properties(let properties):
try container.encode(properties, forKey: .properties)
}
}
}
extension Subtype.Property: Codable {
private enum Keys: String, CodingKey {
case name
case key
case propertyType
case nullable
case legacyOptional = "optional"
case objc
case unused
case defaultValue
case logError
case platforms
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
name = try container.decode(String.self, forKey: .name)
key = try container.decodeIfPresent(String.self, forKey: .key)
propertyType = try container.decode(PropertyType.self, forKey: .propertyType)
objc = try container.decodeIfPresent(Bool.self, forKey: .objc) ?? DescriptionDefaults.objc
unused = try container.decodeIfPresent(Bool.self, forKey: .unused) ?? DescriptionDefaults.unused
nullable = try container.decodeIfPresent(Bool.self, forKey: .nullable) ?? container.decodeIfPresent(Bool.self, forKey: .legacyOptional) ?? DescriptionDefaults.nullable
let defaultValue = try container.decodeIfPresent(DefaultValue.self, forKey: .defaultValue)
let logError = try container.decodeIfPresent(Bool.self, forKey: .logError) ?? DescriptionDefaults.logError
guard logError == true || defaultValue != nil else {
throw DecodingError.dataCorruptedError(forKey: Keys.logError,
in: container,
debugDescription: "log_error can only be true if default value is set.")
}
self.defaultValue = defaultValue
self.logError = logError
self.platforms = try container.decodeIfPresent(Set<Platform>.self, forKey: .platforms) ?? DescriptionDefaults.platforms
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(name, forKey: .name)
try container.encode(key, forKey: .key)
try container.encode(propertyType, forKey: .propertyType)
try container.encode(objc, forKey: .objc)
try container.encode(unused, forKey: .unused)
try container.encode(nullable, forKey: .nullable)
try container.encode(defaultValue, forKey: .defaultValue)
try container.encode(logError, forKey: .logError)
try container.encode(platforms, forKey: .platforms)
}
}
extension Subtype.Property.PropertyType: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let propertyType: (String) -> Subtype.Property.PropertyType = { string in
if let scalarType = PropertyScalarType(string) {
return .scalar(scalarType)
} else {
return .custom(string)
}
}
let typeString = try container.decode(String.self)
if let (key, value) = typeString.dictionaryElementTypes() {
self = .dictionary(key: propertyType(key), value: propertyType(value))
} else if typeString.isArray {
self = .array(propertyType(typeString.arrayElementType()))
} else {
self = propertyType(typeString)
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(stringValue)
}
private var stringValue: String {
switch self {
case .scalar(let value):
return value.stringValue
case .custom(let value):
return value
case .dictionary(let key, let value):
return "{\(key.stringValue):\(value.stringValue)}"
case .array(let type):
return "[\(type.stringValue)]"
}
}
}
extension SystemProperty: Codable {
private enum Keys: String, CodingKey {
case name
case addedAtVersion
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
name = try container.decode(SystemPropertyName.self, forKey: .name)
addedAtVersion = try container.decodeIfPresent(Version.self, forKey: .addedAtVersion)
useCoreDataLegacyNaming = false
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(name, forKey: .name)
try container.encodeIfPresent(addedAtVersion, forKey: .addedAtVersion)
}
}
extension Version: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
try self.init(try container.decode(String.self), source: .description)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(description)
}
}
extension Descriptions: Codable {
private enum Keys: String, CodingKey {
case subtypes
case entities
case endpoints
case targets
case version
}
public convenience init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
self.init(
subtypes: try container.decode([Subtype].self, forKey: .subtypes),
entities: try container.decode([Entity].self, forKey: .entities),
endpoints: try container.decode([EndpointPayload].self, forKey: .endpoints),
targets: try container.decode(Targets.self, forKey: .targets),
version: try container.decode(Version.self, forKey: .version)
)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: Keys.self)
try container.encode(subtypes, forKey: .subtypes)
try container.encode(entities, forKey: .entities)
try container.encode(endpoints, forKey: .endpoints)
try container.encode(targets, forKey: .targets)
try container.encode(version, forKey: .version)
}
}
extension Targets: Codable {
private enum Keys: String, CodingKey {
case app
case appTests
case appTestSupport
}
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: Keys.self)
app = try container.decode(Target.self, forKey: .app)
appTests = try container.decode(Target.self, forKey: .appTests)
appTestSupport = try container.decode(Target.self, forKey: .appTestSupport)
}
public func encode(to encoder: Encoder) throws {