From d3763ba713005cf10e6678c2a01b9e9bd209ebf2 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Tue, 27 Nov 2018 23:59:12 -0800 Subject: [PATCH] Add array literal expressibility for ToManyRelationship --- Sources/JSONAPI/Resource/Relationship.swift | 4 ++++ Sources/JSONAPITestLib/Relationship+Literal.swift | 8 ++++++++ .../JSONAPITests/TestLib/Relationship+LiteralTests.swift | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/Sources/JSONAPI/Resource/Relationship.swift b/Sources/JSONAPI/Resource/Relationship.swift index 7079b30..4c97019 100644 --- a/Sources/JSONAPI/Resource/Relationship.swift +++ b/Sources/JSONAPI/Resource/Relationship.swift @@ -40,6 +40,10 @@ public struct ToManyRelationship: RelationshipType public let ids: [Relatable.Identifier] + public init(ids: [Relatable.Identifier]) { + self.ids = ids + } + public init(relationships: [ToOneRelationship]) where T.WrappedIdentifier == Relatable.Identifier { ids = relationships.map { $0.id } } diff --git a/Sources/JSONAPITestLib/Relationship+Literal.swift b/Sources/JSONAPITestLib/Relationship+Literal.swift index ce22903..42fdc6d 100644 --- a/Sources/JSONAPITestLib/Relationship+Literal.swift +++ b/Sources/JSONAPITestLib/Relationship+Literal.swift @@ -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) + } +} diff --git a/Tests/JSONAPITests/TestLib/Relationship+LiteralTests.swift b/Tests/JSONAPITests/TestLib/Relationship+LiteralTests.swift index 2558328..896535e 100644 --- a/Tests/JSONAPITests/TestLib/Relationship+LiteralTests.swift +++ b/Tests/JSONAPITests/TestLib/Relationship+LiteralTests.swift @@ -14,6 +14,10 @@ class Relationship_LiteralTests: XCTestCase { func test_NilLiteral() { XCTAssertEqual(ToOneRelationship(id: nil), nil) } + + func test_ArrayLiteral() { + XCTAssertEqual(ToManyRelationship(ids: ["1", "2", "3"]), ["1", "2", "3"]) + } } // MARK: - Test types