mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
Add array literal expressibility for ToManyRelationship
This commit is contained in:
@@ -40,6 +40,10 @@ public struct ToManyRelationship<Relatable: JSONAPI.Relatable>: RelationshipType
|
||||
|
||||
public let ids: [Relatable.Identifier]
|
||||
|
||||
public init(ids: [Relatable.Identifier]) {
|
||||
self.ids = ids
|
||||
}
|
||||
|
||||
public init<T: JSONAPI.Relatable>(relationships: [ToOneRelationship<T>]) where T.WrappedIdentifier == Relatable.Identifier {
|
||||
ids = relationships.map { $0.id }
|
||||
}
|
||||
|
||||
@@ -12,3 +12,11 @@ extension ToOneRelationship: ExpressibleByNilLiteral where Relatable.WrappedIden
|
||||
self.init(id: Relatable.WrappedIdentifier(nilLiteral: ()))
|
||||
}
|
||||
}
|
||||
|
||||
extension ToManyRelationship: ExpressibleByArrayLiteral {
|
||||
public typealias ArrayLiteralElement = Relatable.Identifier
|
||||
|
||||
public init(arrayLiteral elements: ArrayLiteralElement...) {
|
||||
self.init(ids: elements)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,10 @@ class Relationship_LiteralTests: XCTestCase {
|
||||
func test_NilLiteral() {
|
||||
XCTAssertEqual(ToOneRelationship<TestEntity?>(id: nil), nil)
|
||||
}
|
||||
|
||||
func test_ArrayLiteral() {
|
||||
XCTAssertEqual(ToManyRelationship<TestEntity>(ids: ["1", "2", "3"]), ["1", "2", "3"])
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Test types
|
||||
|
||||
Reference in New Issue
Block a user