diff --git a/Examples.swift b/Examples.swift new file mode 100644 index 0000000..5519340 --- /dev/null +++ b/Examples.swift @@ -0,0 +1,11 @@ +// +// Examples.swift +// JSONAPI +// +// Created by Mathew Polzin on 11/12/18. +// + +import Foundation +import JSONAPI + + diff --git a/Sources/JSONAPI/Document/JSONAPI_Document.swift b/Sources/JSONAPI/Document/Document.swift similarity index 99% rename from Sources/JSONAPI/Document/JSONAPI_Document.swift rename to Sources/JSONAPI/Document/Document.swift index 3fb522b..0643aa1 100644 --- a/Sources/JSONAPI/Document/JSONAPI_Document.swift +++ b/Sources/JSONAPI/Document/Document.swift @@ -1,6 +1,6 @@ // // JSONAPIDocument.swift -// ElevatedCore +// JSONAPI // // Created by Mathew Polzin on 11/5/18. // diff --git a/Sources/JSONAPI/Document/JSONAPI_Error.swift b/Sources/JSONAPI/Document/Error.swift similarity index 61% rename from Sources/JSONAPI/Document/JSONAPI_Error.swift rename to Sources/JSONAPI/Document/Error.swift index be9cd0d..6fcab7c 100644 --- a/Sources/JSONAPI/Document/JSONAPI_Error.swift +++ b/Sources/JSONAPI/Document/Error.swift @@ -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 } } diff --git a/Sources/JSONAPI/Document/JSONAPI_Includes.swift b/Sources/JSONAPI/Document/Includes.swift similarity index 99% rename from Sources/JSONAPI/Document/JSONAPI_Includes.swift rename to Sources/JSONAPI/Document/Includes.swift index c927379..15767e2 100644 --- a/Sources/JSONAPI/Document/JSONAPI_Includes.swift +++ b/Sources/JSONAPI/Document/Includes.swift @@ -1,5 +1,5 @@ // -// JSONAPI_Includes.swift +// Includes.swift // JSONAPI // // Created by Mathew Polzin on 11/10/18. diff --git a/Sources/JSONAPI/Document/JSONAPI_ResourceBody.swift b/Sources/JSONAPI/Document/ResourceBody.swift similarity index 95% rename from Sources/JSONAPI/Document/JSONAPI_ResourceBody.swift rename to Sources/JSONAPI/Document/ResourceBody.swift index f04de5d..bab1dec 100644 --- a/Sources/JSONAPI/Document/JSONAPI_ResourceBody.swift +++ b/Sources/JSONAPI/Document/ResourceBody.swift @@ -1,6 +1,6 @@ // -// JSONAPI_ResourceBody.swift -// ElevatedCore +// ResourceBody.swift +// JSONAPI // // Created by Mathew Polzin on 11/10/18. // diff --git a/Sources/JSONAPI/Resource/JSONAPI_Entity.swift b/Sources/JSONAPI/Resource/Entity.swift similarity index 72% rename from Sources/JSONAPI/Resource/JSONAPI_Entity.swift rename to Sources/JSONAPI/Resource/Entity.swift index be70dd2..38cfa7a 100644 --- a/Sources/JSONAPI/Resource/JSONAPI_Entity.swift +++ b/Sources/JSONAPI/Resource/Entity.swift @@ -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: 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) { diff --git a/Sources/JSONAPI/Resource/JSONAPI_Id.swift b/Sources/JSONAPI/Resource/Id.swift similarity index 98% rename from Sources/JSONAPI/Resource/JSONAPI_Id.swift rename to Sources/JSONAPI/Resource/Id.swift index a27cc8e..b734d4e 100644 --- a/Sources/JSONAPI/Resource/JSONAPI_Id.swift +++ b/Sources/JSONAPI/Resource/Id.swift @@ -1,6 +1,6 @@ // // Id.swift -// ElevatedCore +// JSONAPI // // Created by Mathew Polzin on 7/24/18. // diff --git a/Sources/JSONAPI/Resource/JSONAPI_Relationship.swift b/Sources/JSONAPI/Resource/Relationship.swift similarity index 99% rename from Sources/JSONAPI/Resource/JSONAPI_Relationship.swift rename to Sources/JSONAPI/Resource/Relationship.swift index 8d9de41..c697c09 100644 --- a/Sources/JSONAPI/Resource/JSONAPI_Relationship.swift +++ b/Sources/JSONAPI/Resource/Relationship.swift @@ -1,6 +1,6 @@ // // Relationship.swift -// ElevatedCore +// JSONAPI // // Created by Mathew Polzin on 8/31/18. // diff --git a/Tests/JSONAPITests/Document/DocumentTests.swift b/Tests/JSONAPITests/Document/DocumentTests.swift index 9af2d1a..2b2424f 100644 --- a/Tests/JSONAPITests/Document/DocumentTests.swift +++ b/Tests/JSONAPITests/Document/DocumentTests.swift @@ -11,7 +11,7 @@ import JSONAPI class DocumentTests: XCTestCase { func test_singleDocumentNoIncludes() { - let document = try? JSONDecoder().decode(JSONAPIDocument, Include0, TmpError>.self, from: single_document_no_includes) + let document = try? JSONDecoder().decode(JSONAPIDocument, 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, Include1, TmpError>.self, from: single_document_some_includes) + let document = try? JSONDecoder().decode(JSONAPIDocument, Include1, 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, Include0, TmpError>.self, from: many_document_no_includes) + let document = try? JSONDecoder().decode(JSONAPIDocument, 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, Include1, TmpError>.self, from: many_document_some_includes) + let document = try? JSONDecoder().decode(JSONAPIDocument, Include1, 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 } } diff --git a/Tests/JSONAPITests/Entity/EntityTests.swift b/Tests/JSONAPITests/Entity/EntityTests.swift index ce263a8..bca4b32 100644 --- a/Tests/JSONAPITests/Entity/EntityTests.swift +++ b/Tests/JSONAPITests/Entity/EntityTests.swift @@ -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 } } @@ -114,7 +114,7 @@ class EntityTests: XCTestCase { typealias RelatedType = Relationships typealias AttributeType = NoAttributes - struct Relationships: Relatives { + struct Relationships: JSONAPI.Relationships { let others: ToManyRelationship } } @@ -128,7 +128,7 @@ class EntityTests: XCTestCase { typealias RelatedType = Relationships typealias AttributeType = Atts - struct Relationships: Relatives { + struct Relationships: JSONAPI.Relationships { let other: ToOneRelationship } diff --git a/Tests/JSONAPITests/Includes/IncludeTests.swift b/Tests/JSONAPITests/Includes/IncludeTests.swift index 1c1d1fc..996b48d 100644 --- a/Tests/JSONAPITests/Includes/IncludeTests.swift +++ b/Tests/JSONAPITests/Includes/IncludeTests.swift @@ -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 } @@ -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 let entity2: ToManyRelationship } @@ -195,7 +195,7 @@ extension IncludedTests { public static var type: String { return "test_entity6" } - struct Relationships: Relatives { + struct Relationships: JSONAPI.Relationships { let entity4: ToOneRelationship } } diff --git a/Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift b/Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift index 77bec1b..068f010 100644 --- a/Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift +++ b/Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift @@ -1,6 +1,6 @@ // // IncludeStubs.swift -// ElevatedCore +// JSONAPITests // // Created by Mathew Polzin on 11/10/18. //