Skip to content

Commit d3763ba

Browse files
committed
Add array literal expressibility for ToManyRelationship
1 parent fcc1796 commit d3763ba

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

Sources/JSONAPI/Resource/Relationship.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public struct ToManyRelationship<Relatable: JSONAPI.Relatable>: RelationshipType
4040

4141
public let ids: [Relatable.Identifier]
4242

43+
public init(ids: [Relatable.Identifier]) {
44+
self.ids = ids
45+
}
46+
4347
public init<T: JSONAPI.Relatable>(relationships: [ToOneRelationship<T>]) where T.WrappedIdentifier == Relatable.Identifier {
4448
ids = relationships.map { $0.id }
4549
}

Sources/JSONAPITestLib/Relationship+Literal.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ extension ToOneRelationship: ExpressibleByNilLiteral where Relatable.WrappedIden
1212
self.init(id: Relatable.WrappedIdentifier(nilLiteral: ()))
1313
}
1414
}
15+
16+
extension ToManyRelationship: ExpressibleByArrayLiteral {
17+
public typealias ArrayLiteralElement = Relatable.Identifier
18+
19+
public init(arrayLiteral elements: ArrayLiteralElement...) {
20+
self.init(ids: elements)
21+
}
22+
}

Tests/JSONAPITests/TestLib/Relationship+LiteralTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class Relationship_LiteralTests: XCTestCase {
1414
func test_NilLiteral() {
1515
XCTAssertEqual(ToOneRelationship<TestEntity?>(id: nil), nil)
1616
}
17+
18+
func test_ArrayLiteral() {
19+
XCTAssertEqual(ToManyRelationship<TestEntity>(ids: ["1", "2", "3"]), ["1", "2", "3"])
20+
}
1721
}
1822

1923
// MARK: - Test types

0 commit comments

Comments
 (0)