From 661ff6eca5c05f5a563e4396fcd7ebb264374f81 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Tue, 27 Nov 2018 18:20:01 -0800 Subject: [PATCH] A little renaming and easier access to important types under the JSONAPIDocument protocol --- .../Usage.xcplaygroundpage/Contents.swift | 10 ++++++ Sources/JSONAPI/Document/Document.swift | 35 ++++++++++--------- Sources/JSONAPI/Document/ResourceBody.swift | 6 ++-- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/JSONAPI.playground/Pages/Usage.xcplaygroundpage/Contents.swift b/JSONAPI.playground/Pages/Usage.xcplaygroundpage/Contents.swift index 62a8508..12542d6 100644 --- a/JSONAPI.playground/Pages/Usage.xcplaygroundpage/Contents.swift +++ b/JSONAPI.playground/Pages/Usage.xcplaygroundpage/Contents.swift @@ -47,3 +47,13 @@ if case let .data(bodyData) = peopleResponse.body { } else { print("no body data") } + +// MARK: - Work in the abstract + +func process(document: T) { + guard case let .data(body) = document.body else { + return + } + let x: T.BodyData = body +} +process(document: peopleResponse) diff --git a/Sources/JSONAPI/Document/Document.swift b/Sources/JSONAPI/Document/Document.swift index 9f02094..93aefb0 100644 --- a/Sources/JSONAPI/Document/Document.swift +++ b/Sources/JSONAPI/Document/Document.swift @@ -6,13 +6,16 @@ // public protocol JSONAPIDocument: Codable, Equatable { - associatedtype ResourceBody: JSONAPI.ResourceBody + associatedtype PrimaryResourceBody: JSONAPI.ResourceBody associatedtype MetaType: JSONAPI.Meta associatedtype LinksType: JSONAPI.Links associatedtype IncludeType: JSONAPI.Include associatedtype Error: JSONAPIError - var body: Document.Body { get } + typealias Body = Document.Body + typealias BodyData = Body.Data + + var body: Body { get } } /// A JSON API Document represents the entire body @@ -22,7 +25,7 @@ public protocol JSONAPIDocument: Codable, Equatable { /// API uses snake case, you will want to use /// a conversion such as the one offerred by the /// Foundation JSONEncoder/Decoder: `KeyDecodingStrategy` -public struct Document: JSONAPIDocument { +public struct Document: JSONAPIDocument { public typealias Include = IncludeType public let body: Body @@ -33,7 +36,7 @@ public struct Document public let meta: MetaType public let links: LinksType @@ -49,7 +52,7 @@ public struct Document, meta: MetaType, links: LinksType) { + public init(body: PrimaryResourceBody, includes: Includes, meta: MetaType, links: LinksType) { self.body = .data(.init(primary: body, includes: includes, meta: meta, links: links)) } } extension Document where IncludeType == NoIncludes { - public init(body: ResourceBody, meta: MetaType, links: LinksType) { + public init(body: PrimaryResourceBody, meta: MetaType, links: LinksType) { self.body = .data(.init(primary: body, includes: .none, meta: meta, links: links)) } } extension Document where MetaType == NoMetadata { - public init(body: ResourceBody, includes: Includes, links: LinksType) { + public init(body: PrimaryResourceBody, includes: Includes, links: LinksType) { self.body = .data(.init(primary: body, includes: includes, meta: .none, links: links)) } } extension Document where LinksType == NoLinks { - public init(body: ResourceBody, includes: Includes, meta: MetaType) { + public init(body: PrimaryResourceBody, includes: Includes, meta: MetaType) { self.body = .data(.init(primary: body, includes: includes, meta: meta, links: .none)) } } extension Document where IncludeType == NoIncludes, LinksType == NoLinks { - public init(body: ResourceBody, meta: MetaType) { + public init(body: PrimaryResourceBody, meta: MetaType) { self.body = .data(.init(primary: body, includes: .none, meta: meta, links: .none)) } } extension Document where IncludeType == NoIncludes, MetaType == NoMetadata { - public init(body: ResourceBody, links: LinksType) { + public init(body: PrimaryResourceBody, links: LinksType) { self.body = .data(.init(primary: body, includes: .none, meta: .none, links: links)) } } extension Document where MetaType == NoMetadata, LinksType == NoLinks { - public init(body: ResourceBody, includes: Includes) { + public init(body: PrimaryResourceBody, includes: Includes) { self.body = .data(.init(primary: body, includes: includes, meta: .none, links: .none)) } } extension Document where IncludeType == NoIncludes, MetaType == NoMetadata, LinksType == NoLinks { - public init(body: ResourceBody) { + public init(body: PrimaryResourceBody) { self.body = .data(.init(primary: body, includes: .none, meta: .none, links: .none)) } } @@ -176,11 +179,11 @@ extension Document { return } - let data: ResourceBody - if let noData = NoResourceBody() as? ResourceBody { + let data: PrimaryResourceBody + if let noData = NoResourceBody() as? PrimaryResourceBody { data = noData } else { - data = try container.decode(ResourceBody.self, forKey: .data) + data = try container.decode(PrimaryResourceBody.self, forKey: .data) } let maybeIncludes = try container.decodeIfPresent(Includes.self, forKey: .included) diff --git a/Sources/JSONAPI/Document/ResourceBody.swift b/Sources/JSONAPI/Document/ResourceBody.swift index b6c79d7..cace1c0 100644 --- a/Sources/JSONAPI/Document/ResourceBody.swift +++ b/Sources/JSONAPI/Document/ResourceBody.swift @@ -1,5 +1,5 @@ // -// ResourceBody.swift +// PrimaryResourceBody.swift // JSONAPI // // Created by Mathew Polzin on 11/10/18. @@ -80,12 +80,12 @@ extension ManyResourceBody { extension SingleResourceBody: CustomStringConvertible { public var description: String { - return "ResourceBody(\(String(describing: value)))" + return "PrimaryResourceBody(\(String(describing: value)))" } } extension ManyResourceBody: CustomStringConvertible { public var description: String { - return "ResourceBody(\(String(describing: values)))" + return "PrimaryResourceBody(\(String(describing: values)))" } }