From 9df9efc2dc4e9d8b15d86233c5b1fe462d55212c Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Tue, 27 Nov 2018 11:08:53 -0800 Subject: [PATCH] rename NoRelatives to NoRelationships to better pair with NoAttributes and NoLinks --- JSONAPI.playground/Sources/Entities.swift | 2 +- README.md | 4 +- Sources/JSONAPI/Resource/Entity.swift | 22 +++++----- Sources/JSONAPITestLib/Id+Literal.swift | 40 +++++++++++++++++++ .../JSONAPITests/Document/DocumentTests.swift | 2 +- Tests/JSONAPITests/Entity/EntityTests.swift | 18 ++++----- .../JSONAPITests/Includes/IncludeTests.swift | 6 +-- Tests/JSONAPITests/Poly/PolyTests.swift | 6 +-- .../Relationships/RelationshipTests.swift | 2 +- .../ResourceBody/ResourceBodyTests.swift | 2 +- .../TestLib/Id+LiteralTests.swift | 35 ++++++++++++++++ 11 files changed, 107 insertions(+), 32 deletions(-) create mode 100644 Sources/JSONAPITestLib/Id+Literal.swift create mode 100644 Tests/JSONAPITests/TestLib/Id+LiteralTests.swift diff --git a/JSONAPI.playground/Sources/Entities.swift b/JSONAPI.playground/Sources/Entities.swift index 8714611..e466b35 100644 --- a/JSONAPI.playground/Sources/Entities.swift +++ b/JSONAPI.playground/Sources/Entities.swift @@ -81,7 +81,7 @@ public enum HouseDescription: EntityDescription { public static var type: String { return "houses" } public typealias Attributes = NoAttributes - public typealias Relationships = NoRelatives + public typealias Relationships = NoRelationships } public typealias House = ExampleEntity diff --git a/README.md b/README.md index a88f1d0..0d5d90e 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ enum PersonDescription: IdentifiedEntityDescription { The requirements of an `EntityDescription` are: 1. A static `var` "type" that matches the JSON type; The JSON spec requires every *Resource Object* to have a "type". 2. A `struct` of `Attributes` **- OR -** `typealias Attributes = NoAttributes` -3. A `struct` of `Relationships` **- OR -** `typealias Relationships = NoRelatives` +3. A `struct` of `Relationships` **- OR -** `typealias Relationships = NoRelationships` Note that an `enum` type is used here for the `EntityDescription`; it could have been a `struct`, but `EntityDescription`s do not ever need to be created so an `enum` with no `case`s is a nice fit for the job. @@ -189,7 +189,7 @@ let nullableRelative: ToOneRelationship An entity that does not have relationships can be described by adding the following to an `EntityDescription`: ``` -typealias Relationships = NoRelatives +typealias Relationships = NoRelationships ``` `Relationship`s boil down to Ids of other entities. To access the Id of a related entity, you can use the shorthand `~>` operator with the `KeyPath` of the `Relationship` from which you want the Id. The friends of the above `Person` entity could be accessed as follows (type annotations for clarity): diff --git a/Sources/JSONAPI/Resource/Entity.swift b/Sources/JSONAPI/Resource/Entity.swift index 6a54635..2bb58cc 100644 --- a/Sources/JSONAPI/Resource/Entity.swift +++ b/Sources/JSONAPI/Resource/Entity.swift @@ -16,7 +16,7 @@ public typealias Attributes = Codable & Equatable /// Can be used as `Relationships` Type for Entities that do not /// have any Relationships. -public struct NoRelatives: Relationships {} +public struct NoRelationships: Relationships {} /// Can be used as `Attributes` Type for Entities that do not /// have any Attributes. @@ -100,27 +100,27 @@ extension Entity where Description.Attributes == NoAttributes, Identifier: Creat } } -extension Entity where Description.Relationships == NoRelatives { +extension Entity where Description.Relationships == NoRelationships { public init(id: Identifier, attributes: Description.Attributes) { - self.init(id: id, attributes: attributes, relationships: NoRelatives()) + self.init(id: id, attributes: attributes, relationships: NoRelationships()) } } -extension Entity where Description.Relationships == NoRelatives, Identifier: CreatableIdType { +extension Entity where Description.Relationships == NoRelationships, Identifier: CreatableIdType { public init(attributes: Description.Attributes) { - self.init(attributes: attributes, relationships: NoRelatives()) + self.init(attributes: attributes, relationships: NoRelationships()) } } -extension Entity where Description.Attributes == NoAttributes, Description.Relationships == NoRelatives { +extension Entity where Description.Attributes == NoAttributes, Description.Relationships == NoRelationships { public init(id: Identifier) { - self.init(id: id, attributes: NoAttributes(), relationships: NoRelatives()) + self.init(id: id, attributes: NoAttributes(), relationships: NoRelationships()) } } -extension Entity where Description.Attributes == NoAttributes, Description.Relationships == NoRelatives, Identifier: CreatableIdType { +extension Entity where Description.Attributes == NoAttributes, Description.Relationships == NoRelationships, Identifier: CreatableIdType { public init() { - self.init(attributes: NoAttributes(), relationships: NoRelatives()) + self.init(attributes: NoAttributes(), relationships: NoRelationships()) } } @@ -198,7 +198,7 @@ public extension Entity { try container.encode(attributes, forKey: .attributes) } - if Description.Relationships.self != NoRelatives.self { + if Description.Relationships.self != NoRelationships.self { try container.encode(relationships, forKey: .relationships) } } @@ -217,6 +217,6 @@ public extension Entity { attributes = try (NoAttributes() as? Description.Attributes) ?? container.decode(Description.Attributes.self, forKey: .attributes) - relationships = try (NoRelatives() as? Description.Relationships) ?? container.decode(Description.Relationships.self, forKey: .relationships) + relationships = try (NoRelationships() as? Description.Relationships) ?? container.decode(Description.Relationships.self, forKey: .relationships) } } diff --git a/Sources/JSONAPITestLib/Id+Literal.swift b/Sources/JSONAPITestLib/Id+Literal.swift new file mode 100644 index 0000000..b05b8b1 --- /dev/null +++ b/Sources/JSONAPITestLib/Id+Literal.swift @@ -0,0 +1,40 @@ +// +// Id+Literal.swift +// JSONAPI +// +// Created by Mathew Polzin on 11/27/18. +// + +import JSONAPI + +extension Id: ExpressibleByUnicodeScalarLiteral where RawType: ExpressibleByUnicodeScalarLiteral { + public typealias UnicodeScalarLiteralType = RawType.UnicodeScalarLiteralType + + public init(unicodeScalarLiteral value: RawType.UnicodeScalarLiteralType) { + self.init(rawValue: RawType(unicodeScalarLiteral: value)) + } +} + +extension Id: ExpressibleByExtendedGraphemeClusterLiteral where RawType: ExpressibleByExtendedGraphemeClusterLiteral { + public typealias ExtendedGraphemeClusterLiteralType = RawType.ExtendedGraphemeClusterLiteralType + + public init(extendedGraphemeClusterLiteral value: RawType.ExtendedGraphemeClusterLiteralType) { + self.init(rawValue: RawType(extendedGraphemeClusterLiteral: value)) + } +} + +extension Id: ExpressibleByStringLiteral where RawType: ExpressibleByStringLiteral { + public typealias StringLiteralType = RawType.StringLiteralType + + public init(stringLiteral value: RawType.StringLiteralType) { + self.init(rawValue: RawType(stringLiteral: value)) + } +} + +extension Id: ExpressibleByIntegerLiteral where RawType: ExpressibleByIntegerLiteral { + public typealias IntegerLiteralType = RawType.IntegerLiteralType + + public init(integerLiteral value: IntegerLiteralType) { + self.init(rawValue: RawType(integerLiteral: value)) + } +} diff --git a/Tests/JSONAPITests/Document/DocumentTests.swift b/Tests/JSONAPITests/Document/DocumentTests.swift index c69c2ed..a947541 100644 --- a/Tests/JSONAPITests/Document/DocumentTests.swift +++ b/Tests/JSONAPITests/Document/DocumentTests.swift @@ -450,7 +450,7 @@ extension DocumentTests { static var type: String { return "authors" } typealias Attributes = NoAttributes - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships } typealias Author = Entity diff --git a/Tests/JSONAPITests/Entity/EntityTests.swift b/Tests/JSONAPITests/Entity/EntityTests.swift index 8ca0e1d..d69ae03 100644 --- a/Tests/JSONAPITests/Entity/EntityTests.swift +++ b/Tests/JSONAPITests/Entity/EntityTests.swift @@ -48,7 +48,7 @@ extension EntityTests { let entity = decoded(type: TestEntity1.self, data: entity_no_relationships_no_attributes) - XCTAssert(type(of: entity.relationships) == NoRelatives.self) + XCTAssert(type(of: entity.relationships) == NoRelationships.self) XCTAssert(type(of: entity.attributes) == NoAttributes.self) } @@ -61,7 +61,7 @@ extension EntityTests { let entity = decoded(type: TestEntity5.self, data: entity_no_relationships_some_attributes) - XCTAssert(type(of: entity.relationships) == NoRelatives.self) + XCTAssert(type(of: entity.relationships) == NoRelationships.self) XCTAssertEqual(entity[\.floater], 123.321) } @@ -310,7 +310,7 @@ extension EntityTests { static var type: String { return "test_entities"} typealias Attributes = NoAttributes - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships } typealias TestEntity1 = Entity @@ -358,7 +358,7 @@ extension EntityTests { enum TestEntityType5: EntityDescription { static var type: String { return "fifth_test_entities"} - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships struct Attributes: JSONAPI.Attributes { let floater: Attribute @@ -370,7 +370,7 @@ extension EntityTests { enum TestEntityType6: EntityDescription { static var type: String { return "sixth_test_entities" } - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships struct Attributes: JSONAPI.Attributes { let here: Attribute @@ -384,7 +384,7 @@ extension EntityTests { enum TestEntityType7: EntityDescription { static var type: String { return "seventh_test_entities" } - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships struct Attributes: JSONAPI.Attributes { let here: Attribute @@ -397,7 +397,7 @@ extension EntityTests { enum TestEntityType8: EntityDescription { static var type: String { return "eighth_test_entities" } - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships struct Attributes: JSONAPI.Attributes { let string: Attribute @@ -454,7 +454,7 @@ extension EntityTests { let number: ValidatedAttribute } - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships } typealias TestEntity11 = Entity @@ -466,7 +466,7 @@ extension EntityTests { let me: Attribute? } - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships } typealias UnidentifiedTestEntity = NewEntity diff --git a/Tests/JSONAPITests/Includes/IncludeTests.swift b/Tests/JSONAPITests/Includes/IncludeTests.swift index 6036e66..112c7d7 100644 --- a/Tests/JSONAPITests/Includes/IncludeTests.swift +++ b/Tests/JSONAPITests/Includes/IncludeTests.swift @@ -127,7 +127,7 @@ class IncludedTests: XCTestCase { extension IncludedTests { enum TestEntityType: EntityDescription { - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships public static var type: String { return "test_entity1" } @@ -173,7 +173,7 @@ extension IncludedTests { typealias Attributes = NoAttributes - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships public static var type: String { return "test_entity4" } } @@ -184,7 +184,7 @@ extension IncludedTests { typealias Attributes = NoAttributes - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships public static var type: String { return "test_entity5" } } diff --git a/Tests/JSONAPITests/Poly/PolyTests.swift b/Tests/JSONAPITests/Poly/PolyTests.swift index 515ac03..10f901e 100644 --- a/Tests/JSONAPITests/Poly/PolyTests.swift +++ b/Tests/JSONAPITests/Poly/PolyTests.swift @@ -265,7 +265,7 @@ extension PolyTests { extension PolyTests { enum TestEntityType: EntityDescription { - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships public static var type: String { return "test_entity1" } @@ -311,7 +311,7 @@ extension PolyTests { typealias Attributes = NoAttributes - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships public static var type: String { return "test_entity4" } } @@ -322,7 +322,7 @@ extension PolyTests { typealias Attributes = NoAttributes - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships public static var type: String { return "test_entity5" } } diff --git a/Tests/JSONAPITests/Relationships/RelationshipTests.swift b/Tests/JSONAPITests/Relationships/RelationshipTests.swift index 459dd3b..3c36d3a 100644 --- a/Tests/JSONAPITests/Relationships/RelationshipTests.swift +++ b/Tests/JSONAPITests/Relationships/RelationshipTests.swift @@ -77,7 +77,7 @@ extension RelationshipTests { enum TestEntityType1: EntityDescription { typealias Attributes = NoAttributes - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships public static var type: String { return "test_entity1" } } diff --git a/Tests/JSONAPITests/ResourceBody/ResourceBodyTests.swift b/Tests/JSONAPITests/ResourceBody/ResourceBodyTests.swift index c7feb87..3360877 100644 --- a/Tests/JSONAPITests/ResourceBody/ResourceBodyTests.swift +++ b/Tests/JSONAPITests/ResourceBody/ResourceBodyTests.swift @@ -54,7 +54,7 @@ class ResourceBodyTests: XCTestCase { enum ArticleType: EntityDescription { public static var type: String { return "articles" } - typealias Relationships = NoRelatives + typealias Relationships = NoRelationships struct Attributes: JSONAPI.Attributes { let title: Attribute diff --git a/Tests/JSONAPITests/TestLib/Id+LiteralTests.swift b/Tests/JSONAPITests/TestLib/Id+LiteralTests.swift new file mode 100644 index 0000000..70fac44 --- /dev/null +++ b/Tests/JSONAPITests/TestLib/Id+LiteralTests.swift @@ -0,0 +1,35 @@ +// +// Id+LiteralTests.swift +// JSONAPITests +// +// Created by Mathew Polzin on 11/27/18. +// + +import XCTest +import JSONAPI +import JSONAPITestLib + +extension Int: RawIdType {} + +class Id_LiteralTests: XCTestCase { + + func test_StringLiteral() { + XCTAssertEqual(Id(rawValue: "hello"), "hello") + } + + func test_IntegerLiteral() { + XCTAssertEqual(Id(rawValue: 121), 121) + } +} + +// MARK: - Test types +extension Id_LiteralTests { + enum TestDescription: EntityDescription { + public static var type: String { return "test" } + + public typealias Attributes = NoAttributes + public typealias Relationships = NoRelationships + } + + typealias TestEntity = Entity +}