Skip to content

Commit

Permalink
Renamed JSONAPIDocument to Document and created a protocol describing…
Browse files Browse the repository at this point in the history
… a JSONAPI Document.
  • Loading branch information
mattpolzin committed Nov 28, 2018
1 parent a9ef71f commit 9c8b2fb
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Please enjoy these examples, but allow me the forced casting and the lack of err
// MARK: - Create a request or response body with one Dog in it
let dogFromCode = try! Dog(name: "Buddy", owner: nil)

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

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

Expand All @@ -27,7 +27,7 @@ let dogs = try! [Dog(name: "Buddy", owner: personIds[0]), Dog(name: "Joy", owner
let houses = [House(), House()]
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])]

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

let includes = dogs.map { BatchPeopleDocument.Include($0) } + houses.map { BatchPeopleDocument.Include($0) }
let batchPeopleDocument = BatchPeopleDocument(body: .init(entities: people), includes: .init(values: includes))
Expand Down
30 changes: 19 additions & 11 deletions Sources/JSONAPI/Document/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@
// Created by Mathew Polzin on 11/5/18.
//

public protocol JSONAPIDocument: Codable, Equatable {
associatedtype ResourceBody: JSONAPI.ResourceBody
associatedtype MetaType: JSONAPI.Meta
associatedtype LinksType: JSONAPI.Links
associatedtype IncludeType: JSONAPI.Include
associatedtype Error: JSONAPIError
}

/// 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 JSONAPIDocument<ResourceBody: JSONAPI.ResourceBody, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links, IncludeType: JSONAPI.Include, Error: JSONAPIError>: Equatable {
public struct Document<ResourceBody: JSONAPI.ResourceBody, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links, IncludeType: JSONAPI.Include, Error: JSONAPIError>: JSONAPIDocument {
public typealias Include = IncludeType

public let body: Body
Expand Down Expand Up @@ -81,49 +89,49 @@ public struct JSONAPIDocument<ResourceBody: JSONAPI.ResourceBody, MetaType: JSON
}
}

extension JSONAPIDocument where IncludeType == NoIncludes {
extension Document where IncludeType == NoIncludes {
public init(body: ResourceBody, meta: MetaType, links: LinksType) {
self.body = .data(.init(primary: body, includes: .none, meta: meta, links: links))
}
}

extension JSONAPIDocument where MetaType == NoMetadata {
extension Document where MetaType == NoMetadata {
public init(body: ResourceBody, includes: Includes<Include>, links: LinksType) {
self.body = .data(.init(primary: body, includes: includes, meta: .none, links: links))
}
}

extension JSONAPIDocument where LinksType == NoLinks {
extension Document where LinksType == NoLinks {
public init(body: ResourceBody, includes: Includes<Include>, meta: MetaType) {
self.body = .data(.init(primary: body, includes: includes, meta: meta, links: .none))
}
}

extension JSONAPIDocument where IncludeType == NoIncludes, LinksType == NoLinks {
extension Document where IncludeType == NoIncludes, LinksType == NoLinks {
public init(body: ResourceBody, meta: MetaType) {
self.body = .data(.init(primary: body, includes: .none, meta: meta, links: .none))
}
}

extension JSONAPIDocument where IncludeType == NoIncludes, MetaType == NoMetadata {
extension Document where IncludeType == NoIncludes, MetaType == NoMetadata {
public init(body: ResourceBody, links: LinksType) {
self.body = .data(.init(primary: body, includes: .none, meta: .none, links: links))
}
}

extension JSONAPIDocument where MetaType == NoMetadata, LinksType == NoLinks {
extension Document where MetaType == NoMetadata, LinksType == NoLinks {
public init(body: ResourceBody, includes: Includes<Include>) {
self.body = .data(.init(primary: body, includes: includes, meta: .none, links: .none))
}
}

extension JSONAPIDocument where IncludeType == NoIncludes, MetaType == NoMetadata, LinksType == NoLinks {
extension Document where IncludeType == NoIncludes, MetaType == NoMetadata, LinksType == NoLinks {
public init(body: ResourceBody) {
self.body = .data(.init(primary: body, includes: .none, meta: .none, links: .none))
}
}

extension JSONAPIDocument: Codable {
extension Document {
private enum RootCodingKeys: String, CodingKey {
case data
case errors
Expand Down Expand Up @@ -228,8 +236,8 @@ extension JSONAPIDocument: Codable {

// MARK: - CustomStringConvertible

extension JSONAPIDocument: CustomStringConvertible {
extension Document: CustomStringConvertible {
public var description: String {
return "JSONAPIDocument(body: \(String(describing: body))"
return "Document(body: \(String(describing: body))"
}
}
Loading

0 comments on commit 9c8b2fb

Please sign in to comment.