From f1945d29663c828f5916756b857321ddd3c2727b Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Wed, 27 Mar 2024 13:59:18 -0500 Subject: [PATCH] add support for 14 includes --- Package.resolved | 4 +- Package.swift | 2 +- Sources/JSONAPI/Document/Includes.swift | 8 ++ .../Resource/Poly+PrimaryResource.swift | 35 ++++++ .../JSONAPITests/Includes/IncludeTests.swift | 36 +++++++ .../Includes/stubs/IncludeStubs.swift | 102 ++++++++++++++++++ 6 files changed, 184 insertions(+), 3 deletions(-) diff --git a/Package.resolved b/Package.resolved index 8a582ff..6540547 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/mattpolzin/Poly.git", "state": { "branch": null, - "revision": "c108e9e0a2904134719b082f6c18d64406afc6db", - "version": "2.6.0" + "revision": "fb90ab22a09fe32e8cb152f7dea344df82a59d53", + "version": "2.7.0" } } ] diff --git a/Package.swift b/Package.swift index e7a0e51..f6a29f7 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,7 @@ let package = Package( targets: ["JSONAPITesting"]) ], dependencies: [ - .package(url: "https://github.com/mattpolzin/Poly.git", .upToNextMajor(from: "2.6.0")), + .package(url: "https://github.com/mattpolzin/Poly.git", .upToNextMajor(from: "2.7.0")), ], targets: [ .target( diff --git a/Sources/JSONAPI/Document/Includes.swift b/Sources/JSONAPI/Document/Includes.swift index 30634ae..9f5b1bd 100644 --- a/Sources/JSONAPI/Document/Includes.swift +++ b/Sources/JSONAPI/Document/Includes.swift @@ -222,6 +222,14 @@ extension Includes where I: _Poly13 { } } +// MARK: - 14 includes +public typealias Include14 = Poly14 +extension Includes where I: _Poly14 { + public subscript(_ lookup: I.N.Type) -> [I.N] { + return values.compactMap(\.n) + } +} + // MARK: - DecodingError public struct IncludesDecodingError: Swift.Error, Equatable { public let error: Swift.Error diff --git a/Sources/JSONAPI/Resource/Poly+PrimaryResource.swift b/Sources/JSONAPI/Resource/Poly+PrimaryResource.swift index 0dab925..c341302 100644 --- a/Sources/JSONAPI/Resource/Poly+PrimaryResource.swift +++ b/Sources/JSONAPI/Resource/Poly+PrimaryResource.swift @@ -226,3 +226,38 @@ J: CodablePolyWrapped, K: CodablePolyWrapped, L: CodablePolyWrapped, M: CodablePolyWrapped {} + +// MARK: - 14 types +extension Poly14: EncodablePrimaryResource, OptionalEncodablePrimaryResource +where +A: EncodablePolyWrapped, +B: EncodablePolyWrapped, +C: EncodablePolyWrapped, +D: EncodablePolyWrapped, +E: EncodablePolyWrapped, +F: EncodablePolyWrapped, +G: EncodablePolyWrapped, +H: EncodablePolyWrapped, +I: EncodablePolyWrapped, +J: EncodablePolyWrapped, +K: EncodablePolyWrapped, +L: EncodablePolyWrapped, +M: EncodablePolyWrapped, +N: EncodablePolyWrapped {} + +extension Poly14: CodablePrimaryResource, OptionalCodablePrimaryResource +where +A: CodablePolyWrapped, +B: CodablePolyWrapped, +C: CodablePolyWrapped, +D: CodablePolyWrapped, +E: CodablePolyWrapped, +F: CodablePolyWrapped, +G: CodablePolyWrapped, +H: CodablePolyWrapped, +I: CodablePolyWrapped, +J: CodablePolyWrapped, +K: CodablePolyWrapped, +L: CodablePolyWrapped, +M: CodablePolyWrapped, +N: CodablePolyWrapped {} diff --git a/Tests/JSONAPITests/Includes/IncludeTests.swift b/Tests/JSONAPITests/Includes/IncludeTests.swift index e0a44a4..73b1922 100644 --- a/Tests/JSONAPITests/Includes/IncludeTests.swift +++ b/Tests/JSONAPITests/Includes/IncludeTests.swift @@ -266,6 +266,31 @@ class IncludedTests: XCTestCase { test_DecodeEncodeEquality(type: Includes>.self, data: thirteen_different_type_includes) } + + func test_FourteenDifferentIncludes() { + let includes = decoded(type: Includes>.self, + data: fourteen_different_type_includes) + + XCTAssertEqual(includes[TestEntity.self].count, 1) + XCTAssertEqual(includes[TestEntity2.self].count, 1) + XCTAssertEqual(includes[TestEntity3.self].count, 1) + XCTAssertEqual(includes[TestEntity4.self].count, 1) + XCTAssertEqual(includes[TestEntity5.self].count, 1) + XCTAssertEqual(includes[TestEntity6.self].count, 1) + XCTAssertEqual(includes[TestEntity7.self].count, 1) + XCTAssertEqual(includes[TestEntity8.self].count, 1) + XCTAssertEqual(includes[TestEntity9.self].count, 1) + XCTAssertEqual(includes[TestEntity10.self].count, 1) + XCTAssertEqual(includes[TestEntity11.self].count, 1) + XCTAssertEqual(includes[TestEntity12.self].count, 1) + XCTAssertEqual(includes[TestEntity13.self].count, 1) + XCTAssertEqual(includes[TestEntity14.self].count, 1) + } + + func test_FourteenDifferentIncludes_encode() { + test_DecodeEncodeEquality(type: Includes>.self, + data: fourteen_different_type_includes) + } } // MARK: - Appending @@ -605,4 +630,15 @@ extension IncludedTests { } typealias TestEntity13 = BasicEntity + + enum TestEntityType14: ResourceObjectDescription { + + typealias Attributes = NoAttributes + + public static var jsonType: String { return "test_entity14" } + + typealias Relationships = NoRelationships + } + + typealias TestEntity14 = BasicEntity } diff --git a/Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift b/Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift index 2cfd3fa..7d23d22 100644 --- a/Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift +++ b/Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift @@ -877,6 +877,108 @@ let thirteen_different_type_includes = """ ] """.data(using: .utf8)! +let fourteen_different_type_includes = """ +[ + { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", + "attributes": { + "foo": "Hello", + "bar": 123 + } + }, + { + "type": "test_entity2", + "id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333", + "attributes": { + "foo": "World", + "bar": 456 + }, + "relationships": { + "entity1": { + "data": { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + } + } + } + }, + { + "type": "test_entity3", + "id": "11223B69-4DF1-467F-B52E-B0C9E44FC443", + "relationships": { + "entity1": { + "data": { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + } + }, + "entity2": { + "data": [ + { + "type": "test_entity2", + "id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333" + } + ] + } + } + }, + { + "type": "test_entity6", + "id": "11113B69-4DF1-467F-B52E-B0C9E44FC444", + "relationships": { + "entity4": { + "data": { + "type": "test_entity4", + "id": "364B3B69-4DF1-467F-B52E-B0C9E44F666E" + } + } + } + }, + { + "type": "test_entity5", + "id": "A24B3B69-4DF1-467F-B52E-B0C9E44F436A" + }, + { + "type": "test_entity4", + "id": "364B3B69-4DF1-467F-B52E-B0C9E44F666E" + }, + { + "type": "test_entity7", + "id": "364B3B69-4DF1-222F-B52E-B0C9E44F666E" + }, + { + "type": "test_entity8", + "id": "364B3B69-4DF1-222F-B52E-B0C9E44F266F" + }, + { + "type": "test_entity9", + "id": "364B3B69-4DF1-218F-B52E-B0C9E44F2661" + }, + { + "type": "test_entity10", + "id": "264B3B69-4DF1-212F-B52E-B0C9E44F2660" + }, + { + "type": "test_entity11", + "id": "264B3B69-4DF3-212F-B32E-A0C9E44F26C0B" + }, + { + "type": "test_entity12", + "id": "264B3B69-4DF3-212F-B32E-A0C9E44F26C00" + }, + { + "type": "test_entity13", + "id": "264B3B69-4DF3-212F-B32E-A0C9E44F26C01" + }, + { + "type": "test_entity14", + "id": "264B3B69-4DF3-312F-B32E-A0C9E44F26C01" + } +] +""".data(using: .utf8)! + + let three_includes_one_missing_attributes = """ [ {