-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathResourceObjectSwiftGen.swift
More file actions
548 lines (465 loc) · 19.8 KB
/
Copy pathResourceObjectSwiftGen.swift
File metadata and controls
548 lines (465 loc) · 19.8 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
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
//
// ResourceObjectSwiftGen.swift
// JSONAPISwiftGen
//
// Created by Mathew Polzin on 9/7/19.
//
import Foundation
import OpenAPIKit30
import JSONAPI
public protocol ResourceTypeSwiftGenerator: SwiftTypeGenerator {
var resourceTypeName: String { get }
}
extension ResourceTypeSwiftGenerator {
public func defines(typeName: String) -> Bool {
return exportedSwiftTypeNames.contains(typeName)
}
}
/// A Swift generator that produces code for the types needed
/// by a JSONAPI ResourceObject.
public struct ResourceObjectSwiftGen: JSONSchemaSwiftGenerator, ResourceTypeSwiftGenerator {
public let structure: DereferencedJSONSchema
public let decls: [Decl]
public let resourceTypeName: String
public let jsonTypeName: String
public let exportedSwiftTypeNames: Set<String>
public let relatives: Set<Relative>
public let relationshipStubGenerators: Set<ResourceObjectStubSwiftGen>
/// A Generator that produces Swift code for a JSONAPI Resource Object type.
/// - parameters:
/// - structure: a JSONSchema describing the entire JSON:API Resource Object.
/// - allowPlaceholders: If true (default) then placeholders will be used for the Swift
/// types of things the generator cannot determine the type or structure of. If false, the
/// generator will throw an error in those situations.
public init(
structure: DereferencedJSONSchema,
allowPlaceholders: Bool = true
) throws {
self.structure = structure
(decls, jsonTypeName, relatives, relationshipStubGenerators) = try ResourceObjectSwiftGen.swiftDecls(
from: structure,
allowPlaceholders: allowPlaceholders
)
let typealiases = decls.compactMap { $0 as? Typealias }
resourceTypeName = typealiases.first.map { $0.alias.swiftCode }!
exportedSwiftTypeNames = Set(typealiases.map { $0.alias.swiftCode })
}
static func swiftDecls(
from structure: DereferencedJSONSchema,
allowPlaceholders: Bool
) throws -> (decls: [Decl], jsonTypeName: String, relatives: Set<Relative>, relationshipStubs: Set<ResourceObjectStubSwiftGen>) {
guard case let .object(_, resourceObjectContextB) = structure else {
throw Error.rootNotJSONObject
}
let (jsonTypeName, typeName, typeNameDecl) = try jsonAPITypeNameSnippet(
contextB: resourceObjectContextB,
allowPlaceholders: allowPlaceholders
)
let identified = resourceObjectContextB.properties[Key.id.rawValue] != nil
let attributesDecl = try attributesSnippet(
contextB: resourceObjectContextB,
allowPlaceholders: allowPlaceholders
)
let relationships = try relationshipsSnippet(
contextB: resourceObjectContextB,
allowPlaceholders: allowPlaceholders
)
let descriptionTypeName = "\(typeName)Description"
let identifiedTypealias = Typealias(
alias: .init(typeName),
existingType: .init(
SwiftTypeDef(
name: "JSONAPI.ResourceObject",
specializationReps: [
.init(descriptionTypeName),
.init(NoMetadata.self),
.init(NoLinks.self),
.init(String.self)
]
)
)
)
let unidenfitiedTypealias = Typealias(
alias: .init("Unidentified" + typeName),
existingType: .init(
SwiftTypeDef(
name: "JSONAPI.ResourceObject",
specializationReps: [
.init(descriptionTypeName),
.init(NoMetadata.self),
.init(NoLinks.self),
.init(Unidentified.self)
]
)
)
)
let decls = [
BlockTypeDecl.enum(
typeName: descriptionTypeName,
conformances: ["JSONAPI.ResourceObjectDescription"],
[
typeNameDecl,
attributesDecl.attributes,
relationships.relationshipsDecl
] + attributesDecl.dependencies
),
(identified ? nil : unidenfitiedTypealias) as Decl?, // only include unidentified typealias if identified is false
identifiedTypealias
].compactMap { $0 }
let relationshipStubs = try Set(
relationships.relatives.map {
try ResourceObjectStubSwiftGen(jsonAPITypeName: $0.jsonTypeName)
}
)
return (decls: decls, jsonTypeName: jsonTypeName, relatives: Set(relationships.relatives), relationshipStubs: relationshipStubs)
}
/// Creates a snippet of code that declares the static Swift property
/// containing the JSON:API type name.
///
/// Takes the second context of the root of the JSON Schema for a Resource Object.
private static func jsonAPITypeNameSnippet(
contextB: DereferencedJSONSchema.ObjectContext,
allowPlaceholders: Bool
) throws -> (jsonTypeName: String, typeName: String, typeNameDeclCode: Decl) {
let typeNameString = try jsonAPITypeName(
from: contextB,
allowPlaceholders: allowPlaceholders
)
return (
jsonTypeName: typeNameString,
typeName: typeCased(typeNameString),
typeNameDeclCode: StaticDecl(.let(propName: "jsonType", swiftType: .init(String.self), .init(value: "\"\(typeNameString)\"")))
)
}
/// Get the JSON:API type name generated for this JSON:API type.
///
/// If the JSON:API resource is documented as having
///
/// type:
/// type: string
/// enum:
/// - widget
///
/// then the JSON:API type name will be "widget".
///
/// If the JSON:API resource is documented as having
///
/// type:
/// type: string
/// enum:
/// - widget
/// - cog
///
/// then the JSON:API type is polymorphic. This is not currently supported so it will
/// throw an error.
///
/// Similarly, if the JSON:API resource is documented as having
///
/// type:
/// type: string
///
/// then the JSON:API type is unspecified. This is not currently supported so it will
/// throw an error.
private static func jsonAPITypeName(
from resourceIdentifierContext: DereferencedJSONSchema.ObjectContext,
allowPlaceholders: Bool
) throws -> String {
guard case let .string(typeNameContextA, _)? = resourceIdentifierContext.properties[Key.type.rawValue] else {
throw Error.jsonAPITypeNotFound
}
guard let possibleTypeNames = typeNameContextA.allowedValues else {
let placeholder = swiftPlaceholder(name: "JSON:API type", type: .init(String.self))
guard allowPlaceholders else {
throw Error.jsonAPITypeUnspecified(placeholder: placeholder)
}
return placeholder
}
guard possibleTypeNames.count == 1,
let typeNameString = possibleTypeNames.first.flatMap({ $0.value as? String }) else {
let typeNames = possibleTypeNames
.compactMap { $0.value as? String }
.map(typeCased)
.joined(separator: ", ")
let placeholder = swiftPlaceholder(name: typeNames, type: .def(.init(name: "Either<\(typeNames)>")))
guard allowPlaceholders else {
throw Error.jsonAPITypePolymorphismUnsupported(placeholder: placeholder)
}
return placeholder
}
return typeNameString
}
/// Takes the second context of the root of the JSON Schema for a Resource Object.
private static func attributesSnippet(
contextB: DereferencedJSONSchema.ObjectContext,
allowPlaceholders: Bool
) throws -> (attributes: Decl, dependencies: [Decl]) {
let attributeTypeName = "Attributes"
guard case let .object(_, attributesContextB)? = contextB.properties[Key.attributes.rawValue] else {
let noAttributesDecl = Typealias(alias: .init(attributeTypeName), existingType: .init(NoAttributes.self))
return (attributes: noAttributesDecl, dependencies: [])
}
let attributeDecls: [(Decl, dependencies: [Decl])] = try attributesContextB
.properties
.sorted { $0.key < $1.key }
.map { keyValue in
return try attributeSnippet(
name: propertyCased(keyValue.key),
schema: keyValue.value,
allowPlaceholders: allowPlaceholders
)
}
let codingKeyCaseDecls = attributesContextB
.properties
.keys
.map{
BlockTypeDecl.enumCase(propertyCased($0), stringValue: $0)
}
let hasAttributes = codingKeyCaseDecls.count > 0
let codingKeyDecl = BlockTypeDecl.enum(
typeName: "CodingKeys",
conformances: ["String", "CodingKey, Equatable"],
codingKeyCaseDecls
)
let attributesAndCodingKeys = attributeDecls
.map { $0.0 }
+ (hasAttributes ? [codingKeyDecl] : []) // only include CodingKeys if non-zero count of attributes
let conformances = hasAttributes
? ["JSONAPI.SparsableAttributes"]
: ["JSONAPI.Attributes"] // only make sparsable if non-zero count of attributes
let attributesDecl = BlockTypeDecl.struct(
typeName: attributeTypeName,
conformances: conformances,
attributesAndCodingKeys
)
return (attributes: attributesDecl, dependencies: attributeDecls.flatMap { $0.1 })
}
/// Takes a JSONSchema and attempts to create a single attribute's
/// code snippet (Decl).
private static func attributeSnippet(
name: String,
schema: DereferencedJSONSchema,
allowPlaceholders: Bool
) throws -> (property: Decl, dependencies: [Decl]) {
let isOmittable = !schema.required
let isNullable = schema.nullable
let attributeRawTypeRep: SwiftTypeRep
let dependencies: [Decl]
switch schema {
case .object, .one, .any:
let structureGen = try StructureSwiftGen(
swiftTypeName: typeCased(name),
structure: schema,
cascadingConformances: ["Codable", "Equatable"]
)
attributeRawTypeRep = .def(.init(name: structureGen.swiftTypeName))
dependencies = structureGen.decls
default:
attributeRawTypeRep = try swiftType(
from: schema,
allowPlaceholders: allowPlaceholders,
handleOptionality: false
)
dependencies = []
}
let finalAttributeRawTypeRep = isNullable
? attributeRawTypeRep.optional
: attributeRawTypeRep
let attributePropertyDecl = PropDecl.let(
propName: name,
swiftType: .init(
SwiftTypeDef(
name: "Attribute",
specializationReps: [finalAttributeRawTypeRep],
optional: isOmittable
)
),
nil
)
return (property: attributePropertyDecl, dependencies: dependencies)
}
/// Takes the second context of the root of the JSON Schema for a Resource Object.
private static func relationshipsSnippet(
contextB: DereferencedJSONSchema.ObjectContext,
allowPlaceholders: Bool
) throws -> (relatives: [Relative], relationshipsDecl: Decl) {
let relationshipTypeName = "Relationships"
guard case let .object(_, relationshipsContextB)? = contextB.properties[Key.relationships.rawValue] else {
return (relatives: [], relationshipsDecl: Typealias(alias: .init(relationshipTypeName), existingType: .init(NoRelationships.self)))
}
let relationshipDecls: [(relative: Relative, typeNameDeclCode: Decl)] = try relationshipsContextB
.properties
.sorted { $0.key < $1.key }
.map { keyValue in
return try relationshipSnippet(
name: propertyCased(keyValue.key),
schema: keyValue.value,
allowPlaceholders: allowPlaceholders
)
}
let codingKeyCaseDecls = relationshipsContextB
.properties
.keys
.map{
BlockTypeDecl.enumCase(propertyCased($0), stringValue: $0)
}
let hasRelationships = codingKeyCaseDecls.count > 0
let codingKeyDecl = BlockTypeDecl.enum(
typeName: "CodingKeys",
conformances: ["String", "CodingKey, Equatable"],
codingKeyCaseDecls
)
let relationshipsAndCodingKeys = relationshipDecls
.map { $0.typeNameDeclCode }
+ (hasRelationships ? [codingKeyDecl] : []) // only include CodingKeys if non-zero count of relationships
let relatives = relationshipDecls.map { $0.relative }
let decl = BlockTypeDecl.struct(
typeName: relationshipTypeName,
conformances: ["JSONAPI.\(relationshipTypeName)"],
relationshipsAndCodingKeys
)
return (relatives: relatives,
relationshipsDecl: decl)
}
private static func relationshipSnippet(
name: String,
schema: DereferencedJSONSchema,
allowPlaceholders: Bool
) throws -> (relative: Relative, typeNameDeclCode: Decl) {
guard case let .object(_, relationshipContextB) = schema,
let dataSchema = relationshipContextB.properties[Key.data.rawValue] else {
throw Error.relationshipMalformed
}
let isOmittable = !schema.required
let isNullable = dataSchema.nullable
let relationship: Relative.Relationship
let oneOrManyName: String
let relatedJSONTypeName: String
let relatedSwiftTypeName: String
let relationshipTypeRep: SwiftTypeRep
switch dataSchema {
case .boolean,
.number,
.integer,
.string:
throw
Error.relationshipMissingDataObject
case .object(_, let contextB):
oneOrManyName = "ToOneRelationship"
relatedJSONTypeName = try jsonAPITypeName(
from: contextB,
allowPlaceholders: allowPlaceholders
)
relatedSwiftTypeName = typeCased(relatedJSONTypeName)
let tmpRelTypeRep = SwiftTypeRep(relatedSwiftTypeName)
relationshipTypeRep = isNullable
? tmpRelTypeRep.optional
: tmpRelTypeRep
relationship = .toOne(isNullable || isOmittable ? .optional : .required)
case .array(_, let contextB):
guard isNullable == false else {
throw Error.toManyRelationshipCannotBeNullable
}
oneOrManyName = "ToManyRelationship"
guard let relationshipEntry = contextB.items,
case let .object(_, relationshipObjectContext) = relationshipEntry else {
throw Error.toManyRelationshipNotDefined
}
relatedJSONTypeName = try jsonAPITypeName(
from: relationshipObjectContext,
allowPlaceholders: allowPlaceholders
)
relatedSwiftTypeName = typeCased(relatedJSONTypeName)
relationshipTypeRep = SwiftTypeRep(relatedSwiftTypeName)
relationship = .toMany(isOmittable ? .optional : .required)
default:
throw Error.relationshipMalformed
}
let typeDecl = PropDecl.let(
propName: name,
swiftType: .init(
SwiftTypeDef(
name: oneOrManyName,
specializationReps: [
relationshipTypeRep,
.init(NoIdMetadata.self),
.init(NoMetadata.self),
.init(NoLinks.self)
], optional: isOmittable
)
),
nil
)
return (
relative: .init(
propertyName: name,
swiftTypeName: relatedSwiftTypeName,
jsonTypeName: relatedJSONTypeName,
relationshipType: relationship
),
typeNameDeclCode: typeDecl
)
}
}
private extension ResourceObjectSwiftGen {
enum Key: String {
case type
case id
case attributes
case relationships
case data
}
}
public extension ResourceObjectSwiftGen {
enum Error: Swift.Error, CustomDebugStringConvertible {
case rootNotJSONObject
case jsonAPITypeNotFound
case jsonAPITypeUnspecified(placeholder: String)
case jsonAPITypePolymorphismUnsupported(placeholder: String)
case relationshipMalformed
case relationshipMissingDataObject
case toManyRelationshipCannotBeNullable
case toManyRelationshipNotDefined
public var debugDescription: String {
switch self {
case .rootNotJSONObject:
return "Tried to parse a JSON:API Resource Object schema that did not have a JSON Schema 'object' type at its root."
case .jsonAPITypeNotFound:
return "Tried to parse a JSON:API Resource Object schema that did not have a JSON:API 'type' property."
case .jsonAPITypeUnspecified(placeholder: let placeholder):
return "Encountered an JSON:API type that was not specified. Make sure that your resource `type` properties have `enum` declarations that define the allowed values for the JSON:API type of the given resource. With `allowPlaceholders: true`, this type would have been: \(placeholder)."
case .jsonAPITypePolymorphismUnsupported(placeholder: let placeholder):
return "Encountered a resource with a polymorphic type. Generation on polymorphic types is not supported. Make sure that your resource `type` properties have `enum` declarations with only one allowed JSON:API type value. With `allowPlaceholders: true`, this type would have been: \(placeholder)."
case .relationshipMalformed:
return "Encountered an unexpected schema when parsing a JSON:API Resource Object's Relationships Object."
case .relationshipMissingDataObject:
return "Tried to parse JSON:API a Relationship schema that did not have either a 'data' object or 'data' array."
case .toManyRelationshipCannotBeNullable:
return "Encountered a nullable to-many JSON:API Relationship in schema. This is not allowed by the spec."
case .toManyRelationshipNotDefined:
return "Tried to parse a to-many JSON:API Relationship schema and did not find an 'items' property defining the Relationship type/id."
}
}
}
}
extension ResourceObjectSwiftGen: Hashable {
public static func == (lhs: ResourceObjectSwiftGen, rhs: ResourceObjectSwiftGen) -> Bool {
return lhs.resourceTypeName == rhs.resourceTypeName
}
public func hash(into hasher: inout Hasher) {
hasher.combine(resourceTypeName)
}
}
public struct Relative: Hashable {
public let propertyName: String
public let swiftTypeName: String
public let jsonTypeName: String
public let relationshipType: Relationship
public enum Relationship: Hashable {
case toOne(Optionality)
case toMany(Optionality)
public enum Optionality: Hashable {
case required
case optional
}
}
}