From fad45203ddcd0b2faf473b9704cb7ab0926d2541 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Wed, 5 Dec 2018 22:42:34 -0800 Subject: [PATCH] small refactor to consider ToOneRelationship with no meta or links to be even more synonymous with 'pointer' --- Sources/JSONAPI/Resource/Entity.swift | 10 ++++++++-- Sources/JSONAPI/Resource/Relationship.swift | 8 ++++---- Tests/JSONAPITests/Entity/EntityTests.swift | 2 +- .../JSONAPITests/Relationships/RelationshipTests.swift | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Sources/JSONAPI/Resource/Entity.swift b/Sources/JSONAPI/Resource/Entity.swift index 4adf193..fd290e5 100644 --- a/Sources/JSONAPI/Resource/Entity.swift +++ b/Sources/JSONAPI/Resource/Entity.swift @@ -352,10 +352,16 @@ extension Entity where MetaType == NoMetadata, LinksType == NoLinks, EntityRawId // MARK: Pointer for Relationships use. public extension Entity where EntityRawIdType: JSONAPI.RawIdType { + + /// An Entity.Pointer is a `ToOneRelationship` with no metadata or links. + /// This is just a convenient way to reference an Entity given that + /// other Entities' Relationships can be built up from it. + public typealias Pointer = ToOneRelationship + /// Get a pointer to this entity that can be used as a /// relationship to another entity. - public var pointer: ToOneRelationship { - return ToOneRelationship(entity: self, meta: .none, links: .none) + public var pointer: Pointer { + return Pointer(entity: self) } public func pointer(withMeta meta: MType, links: LType) -> ToOneRelationship { diff --git a/Sources/JSONAPI/Resource/Relationship.swift b/Sources/JSONAPI/Resource/Relationship.swift index 921a36b..85b5a7f 100644 --- a/Sources/JSONAPI/Resource/Relationship.swift +++ b/Sources/JSONAPI/Resource/Relationship.swift @@ -78,8 +78,8 @@ public struct ToManyRelationship(relationships: [ToOneRelationship], meta: MetaType, links: LinksType) where T.WrappedIdentifier == Relatable.Identifier { - ids = relationships.map { $0.id } + public init(pointers: [ToOneRelationship], meta: MetaType, links: LinksType) where T.WrappedIdentifier == Relatable.Identifier { + ids = pointers.map { $0.id } self.meta = meta self.links = links } @@ -103,8 +103,8 @@ extension ToManyRelationship where MetaType == NoMetadata, LinksType == NoLinks self.init(ids: ids, meta: .none, links: .none) } - public init(relationships: [ToOneRelationship]) where T.WrappedIdentifier == Relatable.Identifier { - self.init(relationships: relationships, meta: .none, links: .none) + public init(pointers: [ToOneRelationship]) where T.WrappedIdentifier == Relatable.Identifier { + self.init(pointers: pointers, meta: .none, links: .none) } public static var none: ToManyRelationship { diff --git a/Tests/JSONAPITests/Entity/EntityTests.swift b/Tests/JSONAPITests/Entity/EntityTests.swift index c6df47e..0ae1680 100644 --- a/Tests/JSONAPITests/Entity/EntityTests.swift +++ b/Tests/JSONAPITests/Entity/EntityTests.swift @@ -29,7 +29,7 @@ class EntityTests: XCTestCase { let entity1 = TestEntity1() let entity2 = TestEntity1() let entity4 = TestEntity1() - let entity3 = TestEntity3(relationships: .init(others: .init(relationships: [entity1.pointer, entity2.pointer, entity4.pointer]))) + let entity3 = TestEntity3(relationships: .init(others: .init(pointers: [entity1.pointer, entity2.pointer, entity4.pointer]))) XCTAssertEqual(entity3 ~> \.others, [entity1.id, entity2.id, entity4.id]) } diff --git a/Tests/JSONAPITests/Relationships/RelationshipTests.swift b/Tests/JSONAPITests/Relationships/RelationshipTests.swift index 6d3f176..09a7d00 100644 --- a/Tests/JSONAPITests/Relationships/RelationshipTests.swift +++ b/Tests/JSONAPITests/Relationships/RelationshipTests.swift @@ -26,7 +26,7 @@ class RelationshipTests: XCTestCase { let entity2 = TestEntity1() let entity3 = TestEntity1() let entity4 = TestEntity1() - let relationship = ToManyRelationship(relationships: [entity1.pointer, entity2.pointer, entity3.pointer, entity4.pointer]) + let relationship = ToManyRelationship(pointers: [entity1.pointer, entity2.pointer, entity3.pointer, entity4.pointer]) XCTAssertEqual(relationship.ids.count, 4) XCTAssertEqual(relationship.ids, [entity1, entity2, entity3, entity4].map { $0.id })