mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
Some cleanup and added documentation.
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
//
|
||||
// Examples.swift
|
||||
// JSONAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 11/12/18.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import JSONAPI
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
//
|
||||
// JSONAPIDocument.swift
|
||||
// ElevatedCore
|
||||
// JSONAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 11/5/18.
|
||||
//
|
||||
@@ -1,6 +1,6 @@
|
||||
//
|
||||
// JSONAPI_Error.swift
|
||||
// ElevatedCore
|
||||
// Error.swift
|
||||
// JSONAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 11/10/18.
|
||||
//
|
||||
@@ -11,15 +11,14 @@ public protocol JSONAPIError: Swift.Error {
|
||||
static var unknown: Self { get }
|
||||
}
|
||||
|
||||
// TODO: remove temp error stuff below
|
||||
public enum TmpError: JSONAPIError & Decodable {
|
||||
public enum BasicJSONAPIError: JSONAPIError & Decodable {
|
||||
case unknownError
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
self = .unknown
|
||||
}
|
||||
|
||||
public static var unknown: TmpError {
|
||||
public static var unknown: BasicJSONAPIError {
|
||||
return .unknownError
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// JSONAPI_Includes.swift
|
||||
// Includes.swift
|
||||
// JSONAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 11/10/18.
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
//
|
||||
// JSONAPI_ResourceBody.swift
|
||||
// ElevatedCore
|
||||
// ResourceBody.swift
|
||||
// JSONAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 11/10/18.
|
||||
//
|
||||
+39
-8
@@ -1,43 +1,74 @@
|
||||
//
|
||||
// Entity.swift
|
||||
// ElevatedCore
|
||||
// JSONAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 7/24/18.
|
||||
//
|
||||
|
||||
public typealias Relatives = Codable & Equatable
|
||||
/// A JSON API structure within an Entity that contains
|
||||
/// named properties of types `ToOneRelationship` and
|
||||
/// `ToManyRelationship`.
|
||||
public typealias Relationships = Codable & Equatable
|
||||
|
||||
/// A JSON API structure within an Entity that contains
|
||||
/// properties of any types that are JSON encodable.
|
||||
public typealias Attributes = Codable & Equatable
|
||||
|
||||
/// Can be used as Relationships Type for Entities that do not
|
||||
/// Can be used as `Relationships` Type for Entities that do not
|
||||
/// have any Relationships.
|
||||
public struct NoRelatives: Relatives {}
|
||||
public struct NoRelatives: Relationships {}
|
||||
|
||||
/// Can be used as Attributes Type for Entities that do not
|
||||
/// Can be used as `Attributes` Type for Entities that do not
|
||||
/// have any Attributes.
|
||||
public struct NoAttributes: Attributes {}
|
||||
|
||||
/// An `EntityType` describes a JSON API
|
||||
/// Resource Object. The Resource Object
|
||||
/// itself is encoded and decoded as an
|
||||
/// `Entity`, which gets specialized on an
|
||||
/// `EntityType`.
|
||||
public protocol EntityType {
|
||||
associatedtype Identifier: JSONAPI.Identifier
|
||||
associatedtype AttributeType: Attributes
|
||||
associatedtype RelatedType: Relatives
|
||||
associatedtype RelatedType: Relationships
|
||||
|
||||
static var type: String { get }
|
||||
}
|
||||
|
||||
/// Shorthand for an `EntityType` that has an Id.
|
||||
/// The only times you would not want an `EntityType`
|
||||
/// to not be an `IdentifiedEntityType` are when you
|
||||
/// are a client making a request to create a new `Entity`
|
||||
/// or you are a server receiving a request to create a
|
||||
/// new `Entity`.
|
||||
public protocol IdentifiedEntityType: EntityType where Identifier: IdType {}
|
||||
|
||||
/// An Entity is a single model type that can be
|
||||
/// Shorthand for an `EntityType` that does not have an Id.
|
||||
/// The only times you would not want an `EntityType`
|
||||
/// to not be an `IdentifiedEntityType` are when you
|
||||
/// are a client making a request to create a new `Entity`
|
||||
/// or you are a server receiving a request to create a
|
||||
/// new `Entity`.
|
||||
public protocol UnidentifiedEntityType: EntityType where Identifier == Unidentified {}
|
||||
|
||||
/// An `Entity` is a single model type that can be
|
||||
/// encoded to or decoded from a JSON API
|
||||
/// "Resource Object."
|
||||
/// See https://jsonapi.org/format/#document-resource-objects
|
||||
/// Easiest to use with `protocol MyEntity: Entity, Identified, Related, Attributed where ID = UUID`.
|
||||
public struct Entity<EntityType: JSONAPI.EntityType>: Codable, Equatable {
|
||||
/// The JSON API compliant "type" of this `Entity`.
|
||||
public static var type: String { return EntityType.type }
|
||||
|
||||
/// The `Entity`'s Id. This can be of type `Unidentified` if
|
||||
/// the entity is being created clientside and the
|
||||
/// server is being asked to create a unique Id. Otherwise,
|
||||
/// this should be of a type conforming to `IdType`.
|
||||
public let id: EntityType.Identifier
|
||||
|
||||
/// The JSON API compliant attributes of this `Entity`.
|
||||
public let attributes: EntityType.AttributeType
|
||||
|
||||
/// The JSON API compliant relationships of this `Entity`.
|
||||
public let relationships: EntityType.RelatedType
|
||||
|
||||
public init(id: EntityType.Identifier, attributes: EntityType.AttributeType, relationships: EntityType.RelatedType) {
|
||||
@@ -1,6 +1,6 @@
|
||||
//
|
||||
// Id.swift
|
||||
// ElevatedCore
|
||||
// JSONAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 7/24/18.
|
||||
//
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
//
|
||||
// Relationship.swift
|
||||
// ElevatedCore
|
||||
// JSONAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 8/31/18.
|
||||
//
|
||||
@@ -11,7 +11,7 @@ import JSONAPI
|
||||
class DocumentTests: XCTestCase {
|
||||
|
||||
func test_singleDocumentNoIncludes() {
|
||||
let document = try? JSONDecoder().decode(JSONAPIDocument<SingleResourceBody<ArticleType>, Include0, TmpError>.self, from: single_document_no_includes)
|
||||
let document = try? JSONDecoder().decode(JSONAPIDocument<SingleResourceBody<ArticleType>, Include0, BasicJSONAPIError>.self, from: single_document_no_includes)
|
||||
|
||||
XCTAssertNotNil(document)
|
||||
|
||||
@@ -24,7 +24,7 @@ class DocumentTests: XCTestCase {
|
||||
}
|
||||
|
||||
func test_singleDocumentSomeIncludes() {
|
||||
let document = try? JSONDecoder().decode(JSONAPIDocument<SingleResourceBody<ArticleType>, Include1<AuthorType>, TmpError>.self, from: single_document_some_includes)
|
||||
let document = try? JSONDecoder().decode(JSONAPIDocument<SingleResourceBody<ArticleType>, Include1<AuthorType>, BasicJSONAPIError>.self, from: single_document_some_includes)
|
||||
|
||||
XCTAssertNotNil(document)
|
||||
|
||||
@@ -39,7 +39,7 @@ class DocumentTests: XCTestCase {
|
||||
}
|
||||
|
||||
func test_manyDocumentNoIncludes() {
|
||||
let document = try? JSONDecoder().decode(JSONAPIDocument<ManyResourceBody<ArticleType>, Include0, TmpError>.self, from: many_document_no_includes)
|
||||
let document = try? JSONDecoder().decode(JSONAPIDocument<ManyResourceBody<ArticleType>, Include0, BasicJSONAPIError>.self, from: many_document_no_includes)
|
||||
|
||||
XCTAssertNotNil(document)
|
||||
|
||||
@@ -55,7 +55,7 @@ class DocumentTests: XCTestCase {
|
||||
}
|
||||
|
||||
func test_manyDocumentSomeIncludes() {
|
||||
let document = try? JSONDecoder().decode(JSONAPIDocument<ManyResourceBody<ArticleType>, Include1<AuthorType>, TmpError>.self, from: many_document_some_includes)
|
||||
let document = try? JSONDecoder().decode(JSONAPIDocument<ManyResourceBody<ArticleType>, Include1<AuthorType>, BasicJSONAPIError>.self, from: many_document_some_includes)
|
||||
|
||||
XCTAssertNotNil(document)
|
||||
|
||||
@@ -91,7 +91,7 @@ class DocumentTests: XCTestCase {
|
||||
typealias AttributeType = NoAttributes
|
||||
typealias RelatedType = Relationships
|
||||
|
||||
struct Relationships: Relatives {
|
||||
struct Relationships: JSONAPI.Relationships {
|
||||
let author: ToOneRelationship<AuthorType>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//
|
||||
// EntityTests.swift
|
||||
// ElevatedCoreTests
|
||||
// JSONAPITests
|
||||
//
|
||||
// Created by Mathew Polzin on 7/25/18.
|
||||
//
|
||||
@@ -100,7 +100,7 @@ class EntityTests: XCTestCase {
|
||||
typealias RelatedType = Relationships
|
||||
typealias AttributeType = NoAttributes
|
||||
|
||||
struct Relationships: Relatives {
|
||||
struct Relationships: JSONAPI.Relationships {
|
||||
let other: ToOneRelationship<TestEntityType1>
|
||||
}
|
||||
}
|
||||
@@ -114,7 +114,7 @@ class EntityTests: XCTestCase {
|
||||
typealias RelatedType = Relationships
|
||||
typealias AttributeType = NoAttributes
|
||||
|
||||
struct Relationships: Relatives {
|
||||
struct Relationships: JSONAPI.Relationships {
|
||||
let others: ToManyRelationship<TestEntityType1>
|
||||
}
|
||||
}
|
||||
@@ -128,7 +128,7 @@ class EntityTests: XCTestCase {
|
||||
typealias RelatedType = Relationships
|
||||
typealias AttributeType = Atts
|
||||
|
||||
struct Relationships: Relatives {
|
||||
struct Relationships: JSONAPI.Relationships {
|
||||
let other: ToOneRelationship<TestEntityType2>
|
||||
}
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ extension IncludedTests {
|
||||
|
||||
public static var type: String { return "test_entity2" }
|
||||
|
||||
public struct Relationships: Relatives {
|
||||
public struct Relationships: JSONAPI.Relationships {
|
||||
let entity1: ToOneRelationship<TestEntityType>
|
||||
}
|
||||
|
||||
@@ -154,7 +154,7 @@ extension IncludedTests {
|
||||
|
||||
public static var type: String { return "test_entity3" }
|
||||
|
||||
public struct Relationships: Relatives {
|
||||
public struct Relationships: JSONAPI.Relationships {
|
||||
let entity1: ToOneRelationship<TestEntityType>
|
||||
let entity2: ToManyRelationship<TestEntityType2>
|
||||
}
|
||||
@@ -195,7 +195,7 @@ extension IncludedTests {
|
||||
|
||||
public static var type: String { return "test_entity6" }
|
||||
|
||||
struct Relationships: Relatives {
|
||||
struct Relationships: JSONAPI.Relationships {
|
||||
let entity4: ToOneRelationship<TestEntityType4>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//
|
||||
// IncludeStubs.swift
|
||||
// ElevatedCore
|
||||
// JSONAPITests
|
||||
//
|
||||
// Created by Mathew Polzin on 11/10/18.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user