Skip to content

Commit 232554e

Browse files
committed
Wrap JSONAPIDocument body up a bit nicer for use when the JSONAPIDocument does not represent errors.
1 parent 8f36bb4 commit 232554e

2 files changed

Lines changed: 40 additions & 23 deletions

File tree

JSONAPI.playground/Pages/Usage.xcplaygroundpage/Contents.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,11 @@ let peopleResponse = try! JSONDecoder().decode(BatchPeopleDocument.self, from: b
3939
let peopleFromData = peopleResponse.body.primaryData?.values
4040
let dogsFromData = peopleResponse.body.includes?[Dog.self]
4141
let housesFromData = peopleResponse.body.includes?[House.self]
42+
43+
// MARK: - Pass successfully parsed body to other parts of the code
44+
45+
if case let .data(bodyData) = peopleResponse.body {
46+
print(bodyData)
47+
} else {
48+
print("no body data")
49+
}

Sources/JSONAPI/Document/Document.swift

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ public struct JSONAPIDocument<ResourceBody: JSONAPI.ResourceBody, MetaType: JSON
2020

2121
public enum Body: Equatable {
2222
case errors([Error], meta: MetaType?, links: LinksType?)
23-
case data(primary: ResourceBody, included: Includes<Include>, meta: MetaType, links: LinksType)
23+
case data(Data)
24+
25+
public struct Data: Equatable {
26+
let primary: ResourceBody
27+
let includes: Includes<Include>
28+
let meta: MetaType
29+
let links: LinksType
30+
}
2431

2532
public var isError: Bool {
2633
guard case .errors = self else { return false }
@@ -33,19 +40,20 @@ public struct JSONAPIDocument<ResourceBody: JSONAPI.ResourceBody, MetaType: JSON
3340
}
3441

3542
public var primaryData: ResourceBody? {
36-
guard case let .data(primary: body, included: _, meta: _, links: _) = self else { return nil }
37-
return body
43+
guard case let .data(data) = self else { return nil }
44+
return data.primary
3845
}
3946

4047
public var includes: Includes<Include>? {
41-
guard case let .data(primary: _, included: includes, meta: _, links: _) = self else { return nil }
42-
return includes
48+
guard case let .data(data) = self else { return nil }
49+
return data.includes
4350
}
4451

4552
public var meta: MetaType? {
4653
switch self {
47-
case .data(primary: _, included: _, meta: let metadata, links: _),
48-
.errors(_, meta: let metadata?, links: _):
54+
case .data(let data):
55+
return data.meta
56+
case .errors(_, meta: let metadata?, links: _):
4957
return metadata
5058
default:
5159
return nil
@@ -54,8 +62,9 @@ public struct JSONAPIDocument<ResourceBody: JSONAPI.ResourceBody, MetaType: JSON
5462

5563
public var links: LinksType? {
5664
switch self {
57-
case .data(primary: _, included: _, meta: _, links: let links),
58-
.errors(_, meta: _, links: let links?):
65+
case .data(let data):
66+
return data.links
67+
case .errors(_, meta: _, links: let links?):
5968
return links
6069
default:
6170
return nil
@@ -68,49 +77,49 @@ public struct JSONAPIDocument<ResourceBody: JSONAPI.ResourceBody, MetaType: JSON
6877
}
6978

7079
public init(body: ResourceBody, includes: Includes<Include>, meta: MetaType, links: LinksType) {
71-
self.body = .data(primary: body, included: includes, meta: meta, links: links)
80+
self.body = .data(.init(primary: body, includes: includes, meta: meta, links: links))
7281
}
7382
}
7483

7584
extension JSONAPIDocument where IncludeType == NoIncludes {
7685
public init(body: ResourceBody, meta: MetaType, links: LinksType) {
77-
self.body = .data(primary: body, included: .none, meta: meta, links: links)
86+
self.body = .data(.init(primary: body, includes: .none, meta: meta, links: links))
7887
}
7988
}
8089

8190
extension JSONAPIDocument where MetaType == NoMetadata {
8291
public init(body: ResourceBody, includes: Includes<Include>, links: LinksType) {
83-
self.body = .data(primary: body, included: includes, meta: .none, links: links)
92+
self.body = .data(.init(primary: body, includes: includes, meta: .none, links: links))
8493
}
8594
}
8695

8796
extension JSONAPIDocument where LinksType == NoLinks {
8897
public init(body: ResourceBody, includes: Includes<Include>, meta: MetaType) {
89-
self.body = .data(primary: body, included: includes, meta: meta, links: .none)
98+
self.body = .data(.init(primary: body, includes: includes, meta: meta, links: .none))
9099
}
91100
}
92101

93102
extension JSONAPIDocument where IncludeType == NoIncludes, LinksType == NoLinks {
94103
public init(body: ResourceBody, meta: MetaType) {
95-
self.body = .data(primary: body, included: .none, meta: meta, links: .none)
104+
self.body = .data(.init(primary: body, includes: .none, meta: meta, links: .none))
96105
}
97106
}
98107

99108
extension JSONAPIDocument where IncludeType == NoIncludes, MetaType == NoMetadata {
100109
public init(body: ResourceBody, links: LinksType) {
101-
self.body = .data(primary: body, included: .none, meta: .none, links: links)
110+
self.body = .data(.init(primary: body, includes: .none, meta: .none, links: links))
102111
}
103112
}
104113

105114
extension JSONAPIDocument where MetaType == NoMetadata, LinksType == NoLinks {
106115
public init(body: ResourceBody, includes: Includes<Include>) {
107-
self.body = .data(primary: body, included: includes, meta: .none, links: .none)
116+
self.body = .data(.init(primary: body, includes: includes, meta: .none, links: .none))
108117
}
109118
}
110119

111120
extension JSONAPIDocument where IncludeType == NoIncludes, MetaType == NoMetadata, LinksType == NoLinks {
112121
public init(body: ResourceBody) {
113-
self.body = .data(primary: body, included: .none, meta: .none, links: .none)
122+
self.body = .data(.init(primary: body, includes: .none, meta: .none, links: .none))
114123
}
115124
}
116125

@@ -175,7 +184,7 @@ extension JSONAPIDocument: Codable {
175184
throw JSONAPIEncodingError.missingOrMalformedLinks
176185
}
177186

178-
body = .data(primary: data, included: maybeIncludes ?? Includes<Include>.none, meta: metaVal, links: linksVal)
187+
body = .data(.init(primary: data, includes: maybeIncludes ?? Includes<Include>.none, meta: metaVal, links: linksVal))
179188
}
180189

181190
public func encode(to encoder: Encoder) throws {
@@ -199,19 +208,19 @@ extension JSONAPIDocument: Codable {
199208
try container.encode(linksVal, forKey: .links)
200209
}
201210

202-
case .data(primary: let resourceBody, included: let includes, let meta, links: let links):
203-
try container.encode(resourceBody, forKey: .data)
211+
case .data(let data):
212+
try container.encode(data.primary, forKey: .data)
204213

205214
if Include.self != NoIncludes.self {
206-
try container.encode(includes, forKey: .included)
215+
try container.encode(data.includes, forKey: .included)
207216
}
208217

209218
if MetaType.self != NoMetadata.self {
210-
try container.encode(meta, forKey: .meta)
219+
try container.encode(data.meta, forKey: .meta)
211220
}
212221

213222
if LinksType.self != NoLinks.self {
214-
try container.encode(links, forKey: .links)
223+
try container.encode(data.links, forKey: .links)
215224
}
216225
}
217226
}

0 commit comments

Comments
 (0)