forked from mattpolzin/JSONAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocument.swift
More file actions
733 lines (632 loc) · 26.2 KB
/
Document.swift
File metadata and controls
733 lines (632 loc) · 26.2 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
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
//
// Document.swift
// JSONAPI
//
// Created by Mathew Polzin on 11/5/18.
//
import Poly
public protocol DocumentBodyDataContext {
associatedtype PrimaryResourceBody: JSONAPI.EncodableResourceBody
associatedtype MetaType: JSONAPI.Meta
associatedtype LinksType: JSONAPI.Links
associatedtype IncludeType: JSONAPI.Include
}
public protocol DocumentBodyContext: DocumentBodyDataContext {
associatedtype Error: JSONAPIError
associatedtype BodyData: DocumentBodyData
where
BodyData.PrimaryResourceBody == PrimaryResourceBody,
BodyData.MetaType == MetaType,
BodyData.LinksType == LinksType,
BodyData.IncludeType == IncludeType
}
public protocol DocumentBodyData: DocumentBodyDataContext {
/// The document's primary resource body
/// (contains one or many resource objects)
var primary: PrimaryResourceBody { get }
/// The document's included objects
var includes: Includes<IncludeType> { get }
var meta: MetaType { get }
var links: LinksType { get }
}
public protocol DocumentBody: DocumentBodyContext {
/// `true` if the document represents one or more errors. `false` if the
/// document represents JSON:API data and/or metadata.
var isError: Bool { get }
/// Get all errors in the document, if any.
///
/// `nil` if the Document is _not_ an error response. Otherwise,
/// an array containing all errors.
var errors: [Error]? { get }
/// Get the document data
///
/// `nil` if the Document is an error response. Otherwise,
/// a structure containing the primary resource, any included
/// resources, metadata, and links.
var data: BodyData? { get }
/// Quick access to the `data`'s primary resource.
///
/// `nil` if the Document is an error document. Otherwise,
/// the primary resource body, which will contain zero/one, one/many
/// resources dependening on the `PrimaryResourceBody` type.
///
/// See `SingleResourceBody` and `ManyResourceBody`.
var primaryResource: PrimaryResourceBody? { get }
/// Quick access to the `data`'s includes.
///
/// `nil` if the Document is an error document. Otherwise,
/// zero or more includes.
var includes: Includes<IncludeType>? { get }
/// The metadata for the error or data document or `nil` if
/// no metadata is found.
var meta: MetaType? { get }
/// The links for the error or data document or `nil` if
/// no links are found.
var links: LinksType? { get }
}
/// An `EncodableJSONAPIDocument` supports encoding but not decoding.
/// It is more restrictive than `CodableJSONAPIDocument` which supports both
/// encoding and decoding.
public protocol EncodableJSONAPIDocument: Equatable, Encodable, DocumentBodyContext {
associatedtype APIDescription: APIDescriptionType
associatedtype Body: DocumentBody
where
Body.PrimaryResourceBody == PrimaryResourceBody,
Body.MetaType == MetaType,
Body.LinksType == LinksType,
Body.IncludeType == IncludeType,
Body.Error == Error,
Body.BodyData == BodyData
/// The Body of the Document. This body is either one or more errors
/// with links and metadata attempted to parse but not guaranteed or
/// it is a successful data struct containing all the primary and
/// included resources, the metadata, and the links that this
/// document type specifies.
var body: Body { get }
/// The JSON API Spec calls this the JSON:API Object. It contains version
/// and metadata information about the API itself.
var apiDescription: APIDescription { get }
}
/// A Document that can be constructed as successful (i.e. not an error document).
public protocol SucceedableJSONAPIDocument: EncodableJSONAPIDocument {
/// Create a successful JSONAPI:Document.
///
/// - Parameters:
/// - apiDescription: The description of the API (a.k.a. the "JSON:API Object").
/// - body: The primary resource body of the JSON:API Document. Generally a single resource or a batch of resources.
/// - includes: All related resources that are included in this Document.
/// - meta: Any metadata associated with the Document.
/// - links: Any links associated with the Document.
///
init(
apiDescription: APIDescription,
body: PrimaryResourceBody,
includes: Includes<IncludeType>,
meta: MetaType,
links: LinksType
)
}
/// A Document that can be constructed as failed (i.e. an error document with no primary
/// resource).
public protocol FailableJSONAPIDocument: EncodableJSONAPIDocument {
/// Create an error JSONAPI:Document.
init(
apiDescription: APIDescription,
errors: [Error],
meta: MetaType?,
links: LinksType?
)
}
/// A `CodableJSONAPIDocument` supports encoding and decoding of a JSON:API
/// compliant Document.
public protocol CodableJSONAPIDocument: EncodableJSONAPIDocument, Decodable where PrimaryResourceBody: JSONAPI.CodableResourceBody, IncludeType: Decodable {}
/// A JSON API Document represents the entire body
/// of a JSON API request or the entire body of
/// a JSON API response.
///
/// Note that this type uses Camel case. If your
/// API uses snake case, you will want to use
/// a conversion such as the one offerred by the
/// Foundation JSONEncoder/Decoder: `KeyDecodingStrategy`
public struct Document<PrimaryResourceBody: JSONAPI.EncodableResourceBody, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links, IncludeType: JSONAPI.Include, APIDescription: APIDescriptionType, Error: JSONAPIError>: EncodableJSONAPIDocument, SucceedableJSONAPIDocument, FailableJSONAPIDocument {
public typealias Include = IncludeType
public typealias BodyData = Body.Data
// See `EncodableJSONAPIDocument` for documentation.
public let apiDescription: APIDescription
// See `EncodableJSONAPIDocument` for documentation.
public let body: Body
public init(
apiDescription: APIDescription,
errors: [Error],
meta: MetaType? = nil,
links: LinksType? = nil
) {
body = .errors(errors, meta: meta, links: links)
self.apiDescription = apiDescription
}
public init(
apiDescription: APIDescription,
body: PrimaryResourceBody,
includes: Includes<Include>,
meta: MetaType,
links: LinksType
) {
self.body = .data(
.init(
primary: body,
includes: includes,
meta: meta,
links: links
)
)
self.apiDescription = apiDescription
}
}
extension Document {
public enum Body: DocumentBody, Equatable {
case errors([Error], meta: MetaType?, links: LinksType?)
case data(Data)
public typealias BodyData = Data
public struct Data: DocumentBodyData, Equatable {
/// The document's Primary Resource object(s)
public let primary: PrimaryResourceBody
/// The document's included objects
public let includes: Includes<Include>
public let meta: MetaType
public let links: LinksType
public init(primary: PrimaryResourceBody, includes: Includes<Include>, meta: MetaType, links: LinksType) {
self.primary = primary
self.includes = includes
self.meta = meta
self.links = links
}
}
/// `true` if the document represents one or more errors. `false` if the
/// document represents JSON:API data and/or metadata.
public var isError: Bool {
guard case .errors = self else { return false }
return true
}
public var errors: [Error]? {
guard case let .errors(errors, meta: _, links: _) = self else { return nil }
return errors
}
public var data: Data? {
guard case let .data(data) = self else { return nil }
return data
}
public var primaryResource: PrimaryResourceBody? {
guard case let .data(data) = self else { return nil }
return data.primary
}
public var includes: Includes<Include>? {
guard case let .data(data) = self else { return nil }
return data.includes
}
public var meta: MetaType? {
switch self {
case .data(let data):
return data.meta
case .errors(_, meta: let metadata?, links: _):
return metadata
default:
return nil
}
}
public var links: LinksType? {
switch self {
case .data(let data):
return data.links
case .errors(_, meta: _, links: let links?):
return links
default:
return nil
}
}
}
}
extension Document.Body.Data where PrimaryResourceBody: ResourceBodyAppendable {
public func merging<OtherDescription, OtherError>(_ other: Document<PrimaryResourceBody, MetaType, LinksType, IncludeType, OtherDescription, OtherError>.Body.Data,
combiningMetaWith metaMerge: (MetaType, MetaType) -> MetaType,
combiningLinksWith linksMerge: (LinksType, LinksType) -> LinksType) -> Document.Body.Data {
return Document.Body.Data(primary: primary.appending(other.primary),
includes: includes.appending(other.includes),
meta: metaMerge(meta, other.meta),
links: linksMerge(links, other.links))
}
}
extension Document.Body.Data where PrimaryResourceBody: ResourceBodyAppendable, MetaType == NoMetadata, LinksType == NoLinks {
public func merging<OtherMeta, OtherLinks, OtherDescription, OtherError>(_ other: Document<PrimaryResourceBody, OtherMeta, OtherLinks, IncludeType, OtherDescription, OtherError>.Body.Data) -> Document<PrimaryResourceBody, OtherMeta, OtherLinks, IncludeType, APIDescription, Error>.Body.Data {
return .init(primary: primary.appending(other.primary),
includes: includes.appending(other.includes),
meta: other.meta,
links: other.links)
}
}
extension Document where IncludeType == NoIncludes {
/// Create a new Document with the given includes.
public func including<I: JSONAPI.Include>(_ includes: Includes<I>) -> Document<PrimaryResourceBody, MetaType, LinksType, I, APIDescription, Error> {
// Note that if IncludeType is NoIncludes, then we allow anything
// to be included, but if IncludeType already specifies a type
// of thing to be expected then we lock that down.
// See: Document.including() where IncludeType: _Poly1
switch body {
case .data(let data):
return .init(apiDescription: apiDescription,
body: data.primary,
includes: includes,
meta: data.meta,
links: data.links)
case .errors(let errors, meta: let meta, links: let links):
return .init(apiDescription: apiDescription,
errors: errors,
meta: meta,
links: links)
}
}
}
// extending where _Poly1 means all non-zero _Poly arities are included
extension Document where IncludeType: _Poly1 {
/// Create a new Document adding the given includes. This does not
/// remove existing includes; it is additive.
public func including(_ includes: Includes<IncludeType>) -> Document {
// Note that if IncludeType is NoIncludes, then we allow anything
// to be included, but if IncludeType already specifies a type
// of thing to be expected then we lock that down.
// See: Document.including() where IncludeType == NoIncludes
switch body {
case .data(let data):
return .init(apiDescription: apiDescription,
body: data.primary,
includes: data.includes + includes,
meta: data.meta,
links: data.links)
case .errors(let errors, meta: let meta, links: let links):
return .init(apiDescription: apiDescription,
errors: errors,
meta: meta,
links: links)
}
}
}
// MARK: - Codable
extension Document {
private enum RootCodingKeys: String, CodingKey {
case data
case errors
case included
case meta
case links
case jsonapi
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: RootCodingKeys.self)
switch body {
case .errors(let errors, meta: let meta, links: let links):
var errContainer = container.nestedUnkeyedContainer(forKey: .errors)
for error in errors {
try errContainer.encode(error)
}
if MetaType.self != NoMetadata.self,
let metaVal = meta {
try container.encode(metaVal, forKey: .meta)
}
if LinksType.self != NoLinks.self,
let linksVal = links {
try container.encode(linksVal, forKey: .links)
}
case .data(let data):
try container.encode(data.primary, forKey: .data)
if Include.self != NoIncludes.self {
try container.encode(data.includes, forKey: .included)
}
if MetaType.self != NoMetadata.self {
try container.encode(data.meta, forKey: .meta)
}
if LinksType.self != NoLinks.self {
try container.encode(data.links, forKey: .links)
}
}
if APIDescription.self != NoAPIDescription.self {
try container.encode(apiDescription, forKey: .jsonapi)
}
}
}
extension Document: Decodable, CodableJSONAPIDocument where PrimaryResourceBody: CodableResourceBody, IncludeType: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: RootCodingKeys.self)
if let noAPIDescription = NoAPIDescription() as? APIDescription {
apiDescription = noAPIDescription
} else {
apiDescription = try container.decode(APIDescription.self, forKey: .jsonapi)
}
let errors = try container.decodeIfPresent([Error].self, forKey: .errors)
let meta: MetaType?
if let noMeta = NoMetadata() as? MetaType {
meta = noMeta
} else {
do {
meta = try container.decode(MetaType.self, forKey: .meta)
} catch {
meta = nil
}
}
let links: LinksType?
if let noLinks = NoLinks() as? LinksType {
links = noLinks
} else {
do {
links = try container.decode(LinksType.self, forKey: .links)
} catch {
links = nil
}
}
// If there are errors, there cannot be a body. Return errors and any metadata found.
if let errors = errors {
body = .errors(errors, meta: meta, links: links)
return
}
let data: PrimaryResourceBody
if let noData = NoResourceBody() as? PrimaryResourceBody {
data = noData
} else {
do {
data = try container.decode(PrimaryResourceBody.self, forKey: .data)
} catch let error as ResourceObjectDecodingError {
throw DocumentDecodingError(error)
} catch let error as ManyResourceBodyDecodingError {
throw DocumentDecodingError(error)
} catch let error as DecodingError {
throw DocumentDecodingError(error)
?? error
}
}
let maybeIncludes: Includes<Include>?
do {
maybeIncludes = try container.decodeIfPresent(Includes<Include>.self, forKey: .included)
} catch let error as IncludesDecodingError {
throw DocumentDecodingError(error)
}
guard let metaVal = meta else {
throw JSONAPICodingError.missingOrMalformedMetadata(path: decoder.codingPath)
}
guard let linksVal = links else {
throw JSONAPICodingError.missingOrMalformedLinks(path: decoder.codingPath)
}
body = .data(.init(primary: data, includes: maybeIncludes ?? Includes<Include>.none, meta: metaVal, links: linksVal))
}
}
// MARK: - CustomStringConvertible
extension Document: CustomStringConvertible {
public var description: String {
return "Document(\(String(describing: body)))"
}
}
extension Document.Body: CustomStringConvertible {
public var description: String {
switch self {
case .errors(let errors, meta: let meta, links: let links):
return "errors: \(String(describing: errors)), meta: \(String(describing: meta)), links: \(String(describing: links))"
case .data(let data):
return String(describing: data)
}
}
}
extension Document.Body.Data: CustomStringConvertible {
public var description: String {
return "primary: \(String(describing: primary)), includes: \(String(describing: includes)), meta: \(String(describing: meta)), links: \(String(describing: links))"
}
}
// MARK: - Error and Success Document Types
extension Document {
/// A Document that only supports error bodies. This is useful if you wish to pass around a
/// Document type but you wish to constrain it to error values.
public struct ErrorDocument: EncodableJSONAPIDocument, FailableJSONAPIDocument {
public typealias BodyData = Document.BodyData
public var body: Document.Body { return document.body }
private let document: Document
public init(
apiDescription: APIDescription,
errors: [Error],
meta: MetaType? = nil,
links: LinksType? = nil
) {
document = .init(apiDescription: apiDescription, errors: errors, meta: meta, links: links)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(document)
}
/// The JSON API Spec calls this the JSON:API Object. It contains version
/// and metadata information about the API itself.
public var apiDescription: APIDescription {
return document.apiDescription
}
/// Get all errors in the document, if any.
public var errors: [Error] {
return document.body.errors ?? []
}
/// The metadata for the error or data document or `nil` if
/// no metadata is found.
public var meta: MetaType? {
return document.body.meta
}
/// The links for the error or data document or `nil` if
/// no links are found.
public var links: LinksType? {
return document.body.links
}
public static func ==(lhs: Document, rhs: ErrorDocument) -> Bool {
return lhs == rhs.document
}
public static func ==(lhs: ErrorDocument, rhs: Document) -> Bool {
return lhs.document == rhs
}
}
/// A Document that only supports success bodies. This is useful if you wish to pass around a
/// Document type but you wish to constrain it to success values.
public struct SuccessDocument: EncodableJSONAPIDocument, SucceedableJSONAPIDocument {
public typealias BodyData = Document.BodyData
public typealias APIDescription = Document.APIDescription
public typealias Body = Document.Body
public typealias PrimaryResourceBody = Document.PrimaryResourceBody
public typealias Include = Document.Include
public typealias MetaType = Document.MetaType
public typealias LinksType = Document.LinksType
public let apiDescription: APIDescription
public let data: BodyData
public let body: Body
public var document: Document {
Document(
apiDescription: apiDescription,
body: data.primary,
includes: data.includes,
meta: data.meta,
links: data.links
)
}
public init(
apiDescription: APIDescription,
body: PrimaryResourceBody,
includes: Includes<Include>,
meta: MetaType,
links: LinksType
) {
self.apiDescription = apiDescription
data = .init(
primary: body,
includes: includes,
meta: meta,
links: links
)
self.body = .data(data)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(document)
}
/// Quick access to the `data`'s primary resource.
///
/// Guaranteed to exist for a `SuccessDocument`.
/// The primary resource body, which will contain zero/one, one/many
/// resources dependening on the `PrimaryResourceBody` type.
///
/// See `SingleResourceBody` and `ManyResourceBody`.
public var primaryResource: PrimaryResourceBody {
return data.primary
}
/// Quick access to the `data`'s includes.
///
/// Zero or more includes.
public var includes: Includes<IncludeType> {
return data.includes
}
/// The metadata for the data document.
public var meta: MetaType {
return data.meta
}
/// The links for the data document.
public var links: LinksType {
return data.links
}
public static func ==(lhs: Document, rhs: SuccessDocument) -> Bool {
return lhs == rhs.document
}
public static func ==(lhs: SuccessDocument, rhs: Document) -> Bool {
return lhs.document == rhs
}
}
}
extension Document.ErrorDocument: Decodable, CodableJSONAPIDocument
where PrimaryResourceBody: CodableResourceBody, IncludeType: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
document = try container.decode(Document.self)
guard document.body.isError else {
throw DocumentDecodingError.foundSuccessDocumentWhenExpectingError
}
}
}
extension Document.SuccessDocument: Decodable, CodableJSONAPIDocument
where PrimaryResourceBody: CodableResourceBody, IncludeType: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let document = try container.decode(Document.self)
guard case .data(let data) = document.body else {
throw DocumentDecodingError.foundErrorDocumentWhenExpectingSuccess
}
self.apiDescription = document.apiDescription
self.data = data
self.body = .data(data)
}
}
extension Document.SuccessDocument where IncludeType == NoIncludes {
/// Create a new Document with the given includes.
public func including<I: JSONAPI.Include>(_ includes: Includes<I>) -> Document<PrimaryResourceBody, MetaType, LinksType, I, APIDescription, Error>.SuccessDocument {
// Note that if IncludeType is NoIncludes, then we allow anything
// to be included, but if IncludeType already specifies a type
// of thing to be expected then we lock that down.
// See: Document.including() where IncludeType: _Poly1
switch document.body {
case .data(let data):
return .init(apiDescription: document.apiDescription,
body: data.primary,
includes: includes,
meta: data.meta,
links: data.links)
case .errors:
fatalError("SuccessDocument cannot end up in an error state")
}
}
}
// extending where _Poly1 means all non-zero _Poly arities are included
extension Document.SuccessDocument where IncludeType: _Poly1 {
/// Create a new Document adding the given includes. This does not
/// remove existing includes; it is additive.
public func including(_ includes: Includes<IncludeType>) -> Document.SuccessDocument {
// Note that if IncludeType is NoIncludes, then we allow anything
// to be included, but if IncludeType already specifies a type
// of thing to be expected then we lock that down.
// See: Document.including() where IncludeType == NoIncludes
switch document.body {
case .data(let data):
return .init(apiDescription: document.apiDescription,
body: data.primary,
includes: data.includes + includes,
meta: data.meta,
links: data.links)
case .errors:
fatalError("SuccessDocument cannot end up in an error state")
}
}
}
extension Document where MetaType == NoMetadata, LinksType == NoLinks, IncludeType == NoIncludes, APIDescription == NoAPIDescription {
public init(body: PrimaryResourceBody) {
self.init(
apiDescription: .none,
body: body,
includes: .none,
meta: .none,
links: .none
)
}
public init(errors: [Error]) {
self.init(apiDescription: .none, errors: errors)
}
}
extension Document.SuccessDocument where Document.MetaType == NoMetadata, Document.LinksType == NoLinks, Document.IncludeType == NoIncludes, Document.APIDescription == NoAPIDescription {
public init(body: PrimaryResourceBody) {
self.init(
apiDescription: .none,
body: body,
includes: .none,
meta: .none,
links: .none
)
}
}
extension Document.ErrorDocument where Document.MetaType == NoMetadata, Document.LinksType == NoLinks, Document.IncludeType == NoIncludes, Document.APIDescription == NoAPIDescription {
public init(errors: [Error]) {
self.init(apiDescription: .none, errors: errors)
}
}