Skip to content

Commit fad4520

Browse files
committed
small refactor to consider ToOneRelationship with no meta or links to be even more synonymous with 'pointer'
1 parent 3468cb5 commit fad4520

4 files changed

Lines changed: 14 additions & 8 deletions

File tree

Sources/JSONAPI/Resource/Entity.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,16 @@ extension Entity where MetaType == NoMetadata, LinksType == NoLinks, EntityRawId
352352

353353
// MARK: Pointer for Relationships use.
354354
public extension Entity where EntityRawIdType: JSONAPI.RawIdType {
355+
356+
/// An Entity.Pointer is a `ToOneRelationship` with no metadata or links.
357+
/// This is just a convenient way to reference an Entity given that
358+
/// other Entities' Relationships can be built up from it.
359+
public typealias Pointer = ToOneRelationship<Entity, NoMetadata, NoLinks>
360+
355361
/// Get a pointer to this entity that can be used as a
356362
/// relationship to another entity.
357-
public var pointer: ToOneRelationship<Entity, NoMetadata, NoLinks> {
358-
return ToOneRelationship(entity: self, meta: .none, links: .none)
363+
public var pointer: Pointer {
364+
return Pointer(entity: self)
359365
}
360366

361367
public func pointer<MType: JSONAPI.Meta, LType: JSONAPI.Links>(withMeta meta: MType, links: LType) -> ToOneRelationship<Entity, MType, LType> {

Sources/JSONAPI/Resource/Relationship.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public struct ToManyRelationship<Relatable: JSONAPI.Relatable, MetaType: JSONAPI
7878
self.links = links
7979
}
8080

81-
public init<T: JSONAPI.Relatable>(relationships: [ToOneRelationship<T, NoMetadata, NoLinks>], meta: MetaType, links: LinksType) where T.WrappedIdentifier == Relatable.Identifier {
82-
ids = relationships.map { $0.id }
81+
public init<T: JSONAPI.Relatable>(pointers: [ToOneRelationship<T, NoMetadata, NoLinks>], meta: MetaType, links: LinksType) where T.WrappedIdentifier == Relatable.Identifier {
82+
ids = pointers.map { $0.id }
8383
self.meta = meta
8484
self.links = links
8585
}
@@ -103,8 +103,8 @@ extension ToManyRelationship where MetaType == NoMetadata, LinksType == NoLinks
103103
self.init(ids: ids, meta: .none, links: .none)
104104
}
105105

106-
public init<T: JSONAPI.Relatable>(relationships: [ToOneRelationship<T, NoMetadata, NoLinks>]) where T.WrappedIdentifier == Relatable.Identifier {
107-
self.init(relationships: relationships, meta: .none, links: .none)
106+
public init<T: JSONAPI.Relatable>(pointers: [ToOneRelationship<T, NoMetadata, NoLinks>]) where T.WrappedIdentifier == Relatable.Identifier {
107+
self.init(pointers: pointers, meta: .none, links: .none)
108108
}
109109

110110
public static var none: ToManyRelationship {

Tests/JSONAPITests/Entity/EntityTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class EntityTests: XCTestCase {
2929
let entity1 = TestEntity1()
3030
let entity2 = TestEntity1()
3131
let entity4 = TestEntity1()
32-
let entity3 = TestEntity3(relationships: .init(others: .init(relationships: [entity1.pointer, entity2.pointer, entity4.pointer])))
32+
let entity3 = TestEntity3(relationships: .init(others: .init(pointers: [entity1.pointer, entity2.pointer, entity4.pointer])))
3333

3434
XCTAssertEqual(entity3 ~> \.others, [entity1.id, entity2.id, entity4.id])
3535
}

Tests/JSONAPITests/Relationships/RelationshipTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class RelationshipTests: XCTestCase {
2626
let entity2 = TestEntity1()
2727
let entity3 = TestEntity1()
2828
let entity4 = TestEntity1()
29-
let relationship = ToManyRelationship<TestEntity1, NoMetadata, NoLinks>(relationships: [entity1.pointer, entity2.pointer, entity3.pointer, entity4.pointer])
29+
let relationship = ToManyRelationship<TestEntity1, NoMetadata, NoLinks>(pointers: [entity1.pointer, entity2.pointer, entity3.pointer, entity4.pointer])
3030

3131
XCTAssertEqual(relationship.ids.count, 4)
3232
XCTAssertEqual(relationship.ids, [entity1, entity2, entity3, entity4].map { $0.id })

0 commit comments

Comments
 (0)