mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
Allow EntityType and Poly to be PrimaryResource in JSONAPIDocument
This commit is contained in:
@@ -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?) {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user