rename NoRelatives to NoRelationships to better pair with NoAttributes and NoLinks

This commit is contained in:
Mathew Polzin
2018-11-27 11:08:53 -08:00
parent a55a4cbed0
commit 9df9efc2dc
11 changed files with 107 additions and 32 deletions
+1 -1
View File
@@ -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<HouseDescription>
+2 -2
View File
@@ -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<Person?>
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):
+11 -11
View File
@@ -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)
}
}
+40
View File
@@ -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))
}
}
@@ -450,7 +450,7 @@ extension DocumentTests {
static var type: String { return "authors" }
typealias Attributes = NoAttributes
typealias Relationships = NoRelatives
typealias Relationships = NoRelationships
}
typealias Author = Entity<AuthorType>
+9 -9
View File
@@ -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<TestEntityType1>
@@ -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<Double>
@@ -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<String>
@@ -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<String>
@@ -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<String>
@@ -454,7 +454,7 @@ extension EntityTests {
let number: ValidatedAttribute<Int, IntOver10>
}
typealias Relationships = NoRelatives
typealias Relationships = NoRelationships
}
typealias TestEntity11 = Entity<TestEntityType11>
@@ -466,7 +466,7 @@ extension EntityTests {
let me: Attribute<String>?
}
typealias Relationships = NoRelatives
typealias Relationships = NoRelationships
}
typealias UnidentifiedTestEntity = NewEntity<UnidentifiedTestEntityType>
@@ -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" }
}
+3 -3
View File
@@ -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" }
}
@@ -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" }
}
@@ -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<String>
@@ -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<String, TestDescription>(rawValue: "hello"), "hello")
}
func test_IntegerLiteral() {
XCTAssertEqual(Id<Int, TestDescription>(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<TestDescription>
}