Skip to content

Commit 661ff6e

Browse files
committed
A little renaming and easier access to important types under the JSONAPIDocument protocol
1 parent e36180c commit 661ff6e

3 files changed

Lines changed: 32 additions & 19 deletions

File tree

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,13 @@ if case let .data(bodyData) = peopleResponse.body {
4747
} else {
4848
print("no body data")
4949
}
50+
51+
// MARK: - Work in the abstract
52+
53+
func process<T: JSONAPIDocument>(document: T) {
54+
guard case let .data(body) = document.body else {
55+
return
56+
}
57+
let x: T.BodyData = body
58+
}
59+
process(document: peopleResponse)

Sources/JSONAPI/Document/Document.swift

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
//
77

88
public protocol JSONAPIDocument: Codable, Equatable {
9-
associatedtype ResourceBody: JSONAPI.ResourceBody
9+
associatedtype PrimaryResourceBody: JSONAPI.ResourceBody
1010
associatedtype MetaType: JSONAPI.Meta
1111
associatedtype LinksType: JSONAPI.Links
1212
associatedtype IncludeType: JSONAPI.Include
1313
associatedtype Error: JSONAPIError
1414

15-
var body: Document<ResourceBody, MetaType, LinksType, IncludeType, Error>.Body { get }
15+
typealias Body = Document<PrimaryResourceBody, MetaType, LinksType, IncludeType, Error>.Body
16+
typealias BodyData = Body.Data
17+
18+
var body: Body { get }
1619
}
1720

1821
/// A JSON API Document represents the entire body
@@ -22,7 +25,7 @@ public protocol JSONAPIDocument: Codable, Equatable {
2225
/// API uses snake case, you will want to use
2326
/// a conversion such as the one offerred by the
2427
/// Foundation JSONEncoder/Decoder: `KeyDecodingStrategy`
25-
public struct Document<ResourceBody: JSONAPI.ResourceBody, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links, IncludeType: JSONAPI.Include, Error: JSONAPIError>: JSONAPIDocument {
28+
public struct Document<PrimaryResourceBody: JSONAPI.ResourceBody, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links, IncludeType: JSONAPI.Include, Error: JSONAPIError>: JSONAPIDocument {
2629
public typealias Include = IncludeType
2730

2831
public let body: Body
@@ -33,7 +36,7 @@ public struct Document<ResourceBody: JSONAPI.ResourceBody, MetaType: JSONAPI.Met
3336
case data(Data)
3437

3538
public struct Data: Equatable {
36-
public let primary: ResourceBody
39+
public let primary: PrimaryResourceBody
3740
public let includes: Includes<Include>
3841
public let meta: MetaType
3942
public let links: LinksType
@@ -49,7 +52,7 @@ public struct Document<ResourceBody: JSONAPI.ResourceBody, MetaType: JSONAPI.Met
4952
return errors
5053
}
5154

52-
public var primaryData: ResourceBody? {
55+
public var primaryData: PrimaryResourceBody? {
5356
guard case let .data(data) = self else { return nil }
5457
return data.primary
5558
}
@@ -86,49 +89,49 @@ public struct Document<ResourceBody: JSONAPI.ResourceBody, MetaType: JSONAPI.Met
8689
body = .errors(errors, meta: meta, links: links)
8790
}
8891

89-
public init(body: ResourceBody, includes: Includes<Include>, meta: MetaType, links: LinksType) {
92+
public init(body: PrimaryResourceBody, includes: Includes<Include>, meta: MetaType, links: LinksType) {
9093
self.body = .data(.init(primary: body, includes: includes, meta: meta, links: links))
9194
}
9295
}
9396

9497
extension Document where IncludeType == NoIncludes {
95-
public init(body: ResourceBody, meta: MetaType, links: LinksType) {
98+
public init(body: PrimaryResourceBody, meta: MetaType, links: LinksType) {
9699
self.body = .data(.init(primary: body, includes: .none, meta: meta, links: links))
97100
}
98101
}
99102

100103
extension Document where MetaType == NoMetadata {
101-
public init(body: ResourceBody, includes: Includes<Include>, links: LinksType) {
104+
public init(body: PrimaryResourceBody, includes: Includes<Include>, links: LinksType) {
102105
self.body = .data(.init(primary: body, includes: includes, meta: .none, links: links))
103106
}
104107
}
105108

106109
extension Document where LinksType == NoLinks {
107-
public init(body: ResourceBody, includes: Includes<Include>, meta: MetaType) {
110+
public init(body: PrimaryResourceBody, includes: Includes<Include>, meta: MetaType) {
108111
self.body = .data(.init(primary: body, includes: includes, meta: meta, links: .none))
109112
}
110113
}
111114

112115
extension Document where IncludeType == NoIncludes, LinksType == NoLinks {
113-
public init(body: ResourceBody, meta: MetaType) {
116+
public init(body: PrimaryResourceBody, meta: MetaType) {
114117
self.body = .data(.init(primary: body, includes: .none, meta: meta, links: .none))
115118
}
116119
}
117120

118121
extension Document where IncludeType == NoIncludes, MetaType == NoMetadata {
119-
public init(body: ResourceBody, links: LinksType) {
122+
public init(body: PrimaryResourceBody, links: LinksType) {
120123
self.body = .data(.init(primary: body, includes: .none, meta: .none, links: links))
121124
}
122125
}
123126

124127
extension Document where MetaType == NoMetadata, LinksType == NoLinks {
125-
public init(body: ResourceBody, includes: Includes<Include>) {
128+
public init(body: PrimaryResourceBody, includes: Includes<Include>) {
126129
self.body = .data(.init(primary: body, includes: includes, meta: .none, links: .none))
127130
}
128131
}
129132

130133
extension Document where IncludeType == NoIncludes, MetaType == NoMetadata, LinksType == NoLinks {
131-
public init(body: ResourceBody) {
134+
public init(body: PrimaryResourceBody) {
132135
self.body = .data(.init(primary: body, includes: .none, meta: .none, links: .none))
133136
}
134137
}
@@ -176,11 +179,11 @@ extension Document {
176179
return
177180
}
178181

179-
let data: ResourceBody
180-
if let noData = NoResourceBody() as? ResourceBody {
182+
let data: PrimaryResourceBody
183+
if let noData = NoResourceBody() as? PrimaryResourceBody {
181184
data = noData
182185
} else {
183-
data = try container.decode(ResourceBody.self, forKey: .data)
186+
data = try container.decode(PrimaryResourceBody.self, forKey: .data)
184187
}
185188

186189
let maybeIncludes = try container.decodeIfPresent(Includes<Include>.self, forKey: .included)

Sources/JSONAPI/Document/ResourceBody.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// ResourceBody.swift
2+
// PrimaryResourceBody.swift
33
// JSONAPI
44
//
55
// Created by Mathew Polzin on 11/10/18.
@@ -80,12 +80,12 @@ extension ManyResourceBody {
8080

8181
extension SingleResourceBody: CustomStringConvertible {
8282
public var description: String {
83-
return "ResourceBody(\(String(describing: value)))"
83+
return "PrimaryResourceBody(\(String(describing: value)))"
8484
}
8585
}
8686

8787
extension ManyResourceBody: CustomStringConvertible {
8888
public var description: String {
89-
return "ResourceBody(\(String(describing: values)))"
89+
return "PrimaryResourceBody(\(String(describing: values)))"
9090
}
9191
}

0 commit comments

Comments
 (0)