Allow EntityType and Poly to be PrimaryResource in JSONAPIDocument

This commit is contained in:
Mathew Polzin
2018-11-23 19:33:06 -08:00
parent 16b83ddbef
commit b61a41c99b
4 changed files with 13 additions and 4 deletions
+3 -1
View File
@@ -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<Entity: JSONAPI.EntityType>: ResourceBody {
public struct SingleResourceBody<Entity: JSONAPI.PrimaryResource>: ResourceBody {
public let value: Entity?
public init(entity: Entity?) {
+1 -1
View File
@@ -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
}
+1 -1
View File
@@ -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
@@ -175,6 +175,14 @@ class DocumentTests: XCTestCase {
test_DecodeEncodeEquality(type: JSONAPIDocument<SingleResourceBody<Article>, TestPageMetadata, Include1<Author>, 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<SingleResourceBody<Poly2<Article, Author>>, 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<ManyResourceBody<Article>, NoMetadata, NoIncludes, BasicJSONAPIError>.self,
@@ -215,7 +223,6 @@ class DocumentTests: XCTestCase {
test_DecodeEncodeEquality(type: JSONAPIDocument<ManyResourceBody<Article>, NoMetadata, Include1<Author>, BasicJSONAPIError>.self,
data: many_document_some_includes)
}
}
// MARK: - Test Types