Skip to content

Commit 9c8b2fb

Browse files
committed
Renamed JSONAPIDocument to Document and created a protocol describing a JSONAPI Document.
1 parent a9ef71f commit 9c8b2fb

3 files changed

Lines changed: 65 additions & 57 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Please enjoy these examples, but allow me the forced casting and the lack of err
1111
// MARK: - Create a request or response body with one Dog in it
1212
let dogFromCode = try! Dog(name: "Buddy", owner: nil)
1313

14-
typealias SingleDogDocument = JSONAPIDocument<SingleResourceBody<Dog>, NoMetadata, NoLinks, NoIncludes, UnknownJSONAPIError>
14+
typealias SingleDogDocument = JSONAPI.Document<SingleResourceBody<Dog>, NoMetadata, NoLinks, NoIncludes, UnknownJSONAPIError>
1515

1616
let singleDogDocument = SingleDogDocument(body: SingleResourceBody(entity: dogFromCode))
1717

@@ -27,7 +27,7 @@ let dogs = try! [Dog(name: "Buddy", owner: personIds[0]), Dog(name: "Joy", owner
2727
let houses = [House(), House()]
2828
let people = try! [Person(id: personIds[0], name: ["Gary", "Doe"], favoriteColor: "Orange-Red", friends: [], dogs: [dogs[0], dogs[1]], home: houses[0]), Person(id: personIds[1], name: ["Elise", "Joy"], favoriteColor: "Red", friends: [], dogs: [dogs[2]], home: houses[1])]
2929

30-
typealias BatchPeopleDocument = JSONAPIDocument<ManyResourceBody<Person>, NoMetadata, NoLinks, Include2<Dog, House>, UnknownJSONAPIError>
30+
typealias BatchPeopleDocument = JSONAPI.Document<ManyResourceBody<Person>, NoMetadata, NoLinks, Include2<Dog, House>, UnknownJSONAPIError>
3131

3232
let includes = dogs.map { BatchPeopleDocument.Include($0) } + houses.map { BatchPeopleDocument.Include($0) }
3333
let batchPeopleDocument = BatchPeopleDocument(body: .init(entities: people), includes: .init(values: includes))

Sources/JSONAPI/Document/Document.swift

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,22 @@
55
// Created by Mathew Polzin on 11/5/18.
66
//
77

8+
public protocol JSONAPIDocument: Codable, Equatable {
9+
associatedtype ResourceBody: JSONAPI.ResourceBody
10+
associatedtype MetaType: JSONAPI.Meta
11+
associatedtype LinksType: JSONAPI.Links
12+
associatedtype IncludeType: JSONAPI.Include
13+
associatedtype Error: JSONAPIError
14+
}
15+
816
/// A JSON API Document represents the entire body
917
/// of a JSON API request or the entire body of
1018
/// a JSON API response.
1119
/// Note that this type uses Camel case. If your
1220
/// API uses snake case, you will want to use
1321
/// a conversion such as the one offerred by the
1422
/// Foundation JSONEncoder/Decoder: `KeyDecodingStrategy`
15-
public struct JSONAPIDocument<ResourceBody: JSONAPI.ResourceBody, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links, IncludeType: JSONAPI.Include, Error: JSONAPIError>: Equatable {
23+
public struct Document<ResourceBody: JSONAPI.ResourceBody, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links, IncludeType: JSONAPI.Include, Error: JSONAPIError>: JSONAPIDocument {
1624
public typealias Include = IncludeType
1725

1826
public let body: Body
@@ -81,49 +89,49 @@ public struct JSONAPIDocument<ResourceBody: JSONAPI.ResourceBody, MetaType: JSON
8189
}
8290
}
8391

84-
extension JSONAPIDocument where IncludeType == NoIncludes {
92+
extension Document where IncludeType == NoIncludes {
8593
public init(body: ResourceBody, meta: MetaType, links: LinksType) {
8694
self.body = .data(.init(primary: body, includes: .none, meta: meta, links: links))
8795
}
8896
}
8997

90-
extension JSONAPIDocument where MetaType == NoMetadata {
98+
extension Document where MetaType == NoMetadata {
9199
public init(body: ResourceBody, includes: Includes<Include>, links: LinksType) {
92100
self.body = .data(.init(primary: body, includes: includes, meta: .none, links: links))
93101
}
94102
}
95103

96-
extension JSONAPIDocument where LinksType == NoLinks {
104+
extension Document where LinksType == NoLinks {
97105
public init(body: ResourceBody, includes: Includes<Include>, meta: MetaType) {
98106
self.body = .data(.init(primary: body, includes: includes, meta: meta, links: .none))
99107
}
100108
}
101109

102-
extension JSONAPIDocument where IncludeType == NoIncludes, LinksType == NoLinks {
110+
extension Document where IncludeType == NoIncludes, LinksType == NoLinks {
103111
public init(body: ResourceBody, meta: MetaType) {
104112
self.body = .data(.init(primary: body, includes: .none, meta: meta, links: .none))
105113
}
106114
}
107115

108-
extension JSONAPIDocument where IncludeType == NoIncludes, MetaType == NoMetadata {
116+
extension Document where IncludeType == NoIncludes, MetaType == NoMetadata {
109117
public init(body: ResourceBody, links: LinksType) {
110118
self.body = .data(.init(primary: body, includes: .none, meta: .none, links: links))
111119
}
112120
}
113121

114-
extension JSONAPIDocument where MetaType == NoMetadata, LinksType == NoLinks {
122+
extension Document where MetaType == NoMetadata, LinksType == NoLinks {
115123
public init(body: ResourceBody, includes: Includes<Include>) {
116124
self.body = .data(.init(primary: body, includes: includes, meta: .none, links: .none))
117125
}
118126
}
119127

120-
extension JSONAPIDocument where IncludeType == NoIncludes, MetaType == NoMetadata, LinksType == NoLinks {
128+
extension Document where IncludeType == NoIncludes, MetaType == NoMetadata, LinksType == NoLinks {
121129
public init(body: ResourceBody) {
122130
self.body = .data(.init(primary: body, includes: .none, meta: .none, links: .none))
123131
}
124132
}
125133

126-
extension JSONAPIDocument: Codable {
134+
extension Document {
127135
private enum RootCodingKeys: String, CodingKey {
128136
case data
129137
case errors
@@ -228,8 +236,8 @@ extension JSONAPIDocument: Codable {
228236

229237
// MARK: - CustomStringConvertible
230238

231-
extension JSONAPIDocument: CustomStringConvertible {
239+
extension Document: CustomStringConvertible {
232240
public var description: String {
233-
return "JSONAPIDocument(body: \(String(describing: body))"
241+
return "Document(body: \(String(describing: body))"
234242
}
235243
}

0 commit comments

Comments
 (0)