From b61a41c99b0766eeefb0dff7f76c8716b3b35a63 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Fri, 23 Nov 2018 19:33:06 -0800 Subject: [PATCH] Allow EntityType and Poly to be PrimaryResource in JSONAPIDocument --- Sources/JSONAPI/Document/ResourceBody.swift | 4 +++- Sources/JSONAPI/Resource/Entity.swift | 2 +- Sources/JSONAPI/Resource/Poly.swift | 2 +- Tests/JSONAPITests/Document/DocumentTests.swift | 9 ++++++++- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/Sources/JSONAPI/Document/ResourceBody.swift b/Sources/JSONAPI/Document/ResourceBody.swift index bdcb55d..5aad51e 100644 --- a/Sources/JSONAPI/Document/ResourceBody.swift +++ b/Sources/JSONAPI/Document/ResourceBody.swift @@ -5,10 +5,12 @@ // Created by Mathew Polzin on 11/10/18. // +public protocol PrimaryResource: Equatable, Codable {} + public protocol ResourceBody: Codable, Equatable { } -public struct SingleResourceBody: ResourceBody { +public struct SingleResourceBody: ResourceBody { public let value: Entity? public init(entity: Entity?) { diff --git a/Sources/JSONAPI/Resource/Entity.swift b/Sources/JSONAPI/Resource/Entity.swift index 19954eb..4927f5f 100644 --- a/Sources/JSONAPI/Resource/Entity.swift +++ b/Sources/JSONAPI/Resource/Entity.swift @@ -37,7 +37,7 @@ public protocol EntityDescription { /// EntityType is the protocol that Entity conforms to. This /// protocol lets other types accept any Entity as a generic /// specialization. -public protocol EntityType: Codable, Equatable { +public protocol EntityType: PrimaryResource { associatedtype Description: EntityDescription associatedtype Identifier: Equatable & Codable } diff --git a/Sources/JSONAPI/Resource/Poly.swift b/Sources/JSONAPI/Resource/Poly.swift index 3b534cf..bc3f4ba 100644 --- a/Sources/JSONAPI/Resource/Poly.swift +++ b/Sources/JSONAPI/Resource/Poly.swift @@ -15,7 +15,7 @@ import Result /// disparate types under one roof for /// the purposes of JSON API compliant /// encoding or decoding. -public protocol Poly: Codable, Equatable {} +public protocol Poly: PrimaryResource {} // MARK: - Generic Decoding diff --git a/Tests/JSONAPITests/Document/DocumentTests.swift b/Tests/JSONAPITests/Document/DocumentTests.swift index e2bee72..00ab40e 100644 --- a/Tests/JSONAPITests/Document/DocumentTests.swift +++ b/Tests/JSONAPITests/Document/DocumentTests.swift @@ -175,6 +175,14 @@ class DocumentTests: XCTestCase { test_DecodeEncodeEquality(type: JSONAPIDocument, TestPageMetadata, Include1, BasicJSONAPIError>.self, data: single_document_some_includes_with_metadata) } + + func test_singleDocument_PolyPrimaryResource() { + let article = Article(id: Id(rawValue: "1"), relationships: .init(author: ToOneRelationship(id: Id(rawValue: "33")))) + let document = decoded(type: JSONAPIDocument>, NoMetadata, NoIncludes, BasicJSONAPIError>.self, data: single_document_no_includes) + + XCTAssertEqual(document.body.data?.primary.value?[Article.self], article) + XCTAssertNil(document.body.data?.primary.value?[Author.self]) + } func test_manyDocumentNoIncludes() { let document = decoded(type: JSONAPIDocument, NoMetadata, NoIncludes, BasicJSONAPIError>.self, @@ -215,7 +223,6 @@ class DocumentTests: XCTestCase { test_DecodeEncodeEquality(type: JSONAPIDocument, NoMetadata, Include1, BasicJSONAPIError>.self, data: many_document_some_includes) } - } // MARK: - Test Types