From 2ae92cf33ff79691ce47e4f661a4895b71452478 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Thu, 15 Nov 2018 23:35:23 -0800 Subject: [PATCH] Add a bit more flavor to Examples.swift --- Examples.swift | 56 ++++++++++++++++++++++++- Sources/JSONAPI/Document/Document.swift | 18 ++++---- 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/Examples.swift b/Examples.swift index 957a989..e7e95db 100644 --- a/Examples.swift +++ b/Examples.swift @@ -5,8 +5,20 @@ // Created by Mathew Polzin on 11/12/18. // +import Foundation import JSONAPI + +/******* + +Please enjoy these examples, but allow me the lack of error checking and forced casting for the sake of brevity. + +********/ + + +typealias ExampleEntity = Entity> + +// MARK: - A few resource objects (entities) enum PersonDescription: EntityDescription { static var type: String { return "people" } @@ -18,7 +30,49 @@ enum PersonDescription: EntityDescription { struct Relationships: JSONAPI.Relationships { let friends: ToManyRelationship + let dogs: ToManyRelationship + let home: ToOneRelationship } } -typealias Person = Entity> +typealias Person = ExampleEntity + +enum DogDescription: EntityDescription { + + static var type: String { return "dogs" } + + struct Attributes: JSONAPI.Attributes { + let name: String + } + + struct Relationships: JSONAPI.Relationships { + let owner: ToOneRelationship + } +} + +typealias Dog = ExampleEntity + +enum HouseDescription: EntityDescription { + + static var type: String { return "houses" } + + typealias Attributes = NoAttributes + typealias Relationships = NoRelatives +} + +typealias House = ExampleEntity + +// MARK: - Parse a response body with one Dog in it +typealias SingleDogResponse = JSONAPIDocument, NoIncludes, BasicJSONAPIError> + +let dummyData = "".data(using: .utf8)! +let dogResponse = try! JSONDecoder().decode(SingleDogResponse.self, from: dummyData) +let dog = dogResponse.body.data?.primary.value + +// MARK: Parse a response body with multiple people in it and dogs and houses included +typealias BatchPeopleResponse = JSONAPIDocument, Include2, BasicJSONAPIError> + +let peopleResponse = try! JSONDecoder().decode(BatchPeopleResponse.self, from: dummyData) +let people = peopleResponse.body.data?.primary.values +let dogs = peopleResponse.body.data?.included[Dog.self] +let houses = peopleResponse.body.data?.included[House.self] diff --git a/Sources/JSONAPI/Document/Document.swift b/Sources/JSONAPI/Document/Document.swift index 3a6a62a..bbcc29b 100644 --- a/Sources/JSONAPI/Document/Document.swift +++ b/Sources/JSONAPI/Document/Document.swift @@ -12,24 +12,24 @@ /// 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 { - public let body: Data +public struct JSONAPIDocument { + public let body: Body // public let meta: Meta? // public let jsonApi: APIDescription? // public let links: Links? - public enum Data { + public enum Body { case errors([Error]) - case data(Body, included: Includes) + case data(primary: ResourceBody, included: Includes) public var isError: Bool { guard case .errors = self else { return false } return true } - public var data: (Body, included: Includes)? { - guard case let .data(body, included: includes) = self else { return nil } - return (body, included: includes) + public var data: (primary: ResourceBody, included: Includes)? { + guard case let .data(primary: body, included: includes) = self else { return nil } + return (primary: body, included: includes) } } } @@ -47,7 +47,7 @@ extension JSONAPIDocument: Decodable { public init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: RootCodingKeys.self) - let maybeData = try container.decodeIfPresent(Body.self, forKey: .data) + let maybeData = try container.decodeIfPresent(ResourceBody.self, forKey: .data) let maybeIncludes = try container.decodeIfPresent(Includes.self, forKey: .included) let errors = try container.decodeIfPresent([Error].self, forKey: .errors) @@ -66,6 +66,6 @@ extension JSONAPIDocument: Decodable { return } - body = .data(data, included: maybeIncludes ?? Includes.none) + body = .data(primary: data, included: maybeIncludes ?? Includes.none) } }