From 654d3bfd2bbf58d99f5ce38d140abe3abe1d86d5 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Wed, 2 Oct 2019 20:02:11 -0700 Subject: [PATCH] Update Poly, add Include11 type. --- Package.resolved | 4 +- Package.swift | 2 +- Sources/JSONAPI/Document/Includes.swift | 8 ++ Sources/JSONAPI/Error/BasicJSONAPIError.swift | 2 +- .../JSONAPI/Error/GenericJSONAPIError.swift | 2 +- Sources/JSONAPI/Error/JSONAPIError.swift | 2 +- .../Resource/Poly+PrimaryResource.swift | 5 ++ .../JSONAPITests/Includes/IncludeTests.swift | 33 +++++++ .../Includes/stubs/IncludeStubs.swift | 89 +++++++++++++++++++ .../Includes/stubs/one_include.json | 10 --- .../stubs/three_different_type_includes.json | 30 ------- .../stubs/two_different_type_includes.json | 26 ------ .../stubs/two_same_type_includes.json | 18 ---- Tests/JSONAPITests/Poly/stubs/PolyStubs.swift | 7 ++ 14 files changed, 148 insertions(+), 90 deletions(-) delete mode 100644 Tests/JSONAPITests/Includes/stubs/one_include.json delete mode 100644 Tests/JSONAPITests/Includes/stubs/three_different_type_includes.json delete mode 100644 Tests/JSONAPITests/Includes/stubs/two_different_type_includes.json delete mode 100644 Tests/JSONAPITests/Includes/stubs/two_same_type_includes.json diff --git a/Package.resolved b/Package.resolved index f7356ed..b1970b8 100644 --- a/Package.resolved +++ b/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/mattpolzin/Poly.git", "state": { "branch": null, - "revision": "4a08517b24f8e9f6dd8c02ec7da316aac5c00e2e", - "version": "2.1.0" + "revision": "b24fd3b41bf3126d4c6dede3708135182172af60", + "version": "2.2.0" } } ] diff --git a/Package.swift b/Package.swift index e0f38ab..2093673 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ let package = Package( targets: ["JSONAPITesting"]) ], dependencies: [ - .package(url: "https://github.com/mattpolzin/Poly.git", .upToNextMajor(from: "2.1.0")), + .package(url: "https://github.com/mattpolzin/Poly.git", .upToNextMajor(from: "2.2.0")), ], targets: [ .target( diff --git a/Sources/JSONAPI/Document/Includes.swift b/Sources/JSONAPI/Document/Includes.swift index 65ccee0..b6d93e0 100644 --- a/Sources/JSONAPI/Document/Includes.swift +++ b/Sources/JSONAPI/Document/Includes.swift @@ -169,3 +169,11 @@ extension Includes where I: _Poly10 { return values.compactMap { $0.j } } } + +// MARK: - 11 includes +public typealias Include11 = Poly11 +extension Includes where I: _Poly11 { + public subscript(_ lookup: I.K.Type) -> [I.K] { + return values.compactMap { $0.k } + } +} diff --git a/Sources/JSONAPI/Error/BasicJSONAPIError.swift b/Sources/JSONAPI/Error/BasicJSONAPIError.swift index d3859eb..28795b9 100644 --- a/Sources/JSONAPI/Error/BasicJSONAPIError.swift +++ b/Sources/JSONAPI/Error/BasicJSONAPIError.swift @@ -1,5 +1,5 @@ // -// BasicError.swift +// BasicJSONAPIError.swift // JSONAPI // // Created by Mathew Polzin on 9/29/19. diff --git a/Sources/JSONAPI/Error/GenericJSONAPIError.swift b/Sources/JSONAPI/Error/GenericJSONAPIError.swift index 91ce2b8..09383e7 100644 --- a/Sources/JSONAPI/Error/GenericJSONAPIError.swift +++ b/Sources/JSONAPI/Error/GenericJSONAPIError.swift @@ -1,5 +1,5 @@ // -// GenericError.swift +// GenericJSONAPIError.swift // JSONAPI // // Created by Mathew Polzin on 9/29/19. diff --git a/Sources/JSONAPI/Error/JSONAPIError.swift b/Sources/JSONAPI/Error/JSONAPIError.swift index f1dbfe9..b997a69 100644 --- a/Sources/JSONAPI/Error/JSONAPIError.swift +++ b/Sources/JSONAPI/Error/JSONAPIError.swift @@ -1,5 +1,5 @@ // -// Error.swift +// JSONAPIError.swift // JSONAPI // // Created by Mathew Polzin on 11/10/18. diff --git a/Sources/JSONAPI/Resource/Poly+PrimaryResource.swift b/Sources/JSONAPI/Resource/Poly+PrimaryResource.swift index d1e1e5f..284e10a 100644 --- a/Sources/JSONAPI/Resource/Poly+PrimaryResource.swift +++ b/Sources/JSONAPI/Resource/Poly+PrimaryResource.swift @@ -79,3 +79,8 @@ extension Poly9: PrimaryResource, OptionalPrimaryResource where A: PolyWrapped, extension Poly10: EncodablePrimaryResource, OptionalEncodablePrimaryResource where A: EncodablePolyWrapped, B: EncodablePolyWrapped, C: EncodablePolyWrapped, D: EncodablePolyWrapped, E: EncodablePolyWrapped, F: EncodablePolyWrapped, G: EncodablePolyWrapped, H: EncodablePolyWrapped, I: EncodablePolyWrapped, J: EncodablePolyWrapped {} extension Poly10: PrimaryResource, OptionalPrimaryResource where A: PolyWrapped, B: PolyWrapped, C: PolyWrapped, D: PolyWrapped, E: PolyWrapped, F: PolyWrapped, G: PolyWrapped, H: PolyWrapped, I: PolyWrapped, J: PolyWrapped {} + +// MARK: - 11 types +extension Poly11: 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 {} + +extension Poly11: PrimaryResource, OptionalPrimaryResource where A: PolyWrapped, B: PolyWrapped, C: PolyWrapped, D: PolyWrapped, E: PolyWrapped, F: PolyWrapped, G: PolyWrapped, H: PolyWrapped, I: PolyWrapped, J: PolyWrapped, K: PolyWrapped {} diff --git a/Tests/JSONAPITests/Includes/IncludeTests.swift b/Tests/JSONAPITests/Includes/IncludeTests.swift index ed94e89..bed7d22 100644 --- a/Tests/JSONAPITests/Includes/IncludeTests.swift +++ b/Tests/JSONAPITests/Includes/IncludeTests.swift @@ -197,6 +197,28 @@ class IncludedTests: XCTestCase { test_DecodeEncodeEquality(type: Includes>.self, data: ten_different_type_includes) } + + func test_ElevenDifferentIncludes() { + let includes = decoded(type: Includes>.self, + data: eleven_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) + } + + func test_ElevenDifferentIncludes_encode() { + test_DecodeEncodeEquality(type: Includes>.self, + data: eleven_different_type_includes) + } } // MARK: - Appending @@ -503,4 +525,15 @@ extension IncludedTests { } typealias TestEntity10 = BasicEntity + + enum TestEntityType11: ResourceObjectDescription { + + typealias Attributes = NoAttributes + + public static var jsonType: String { return "test_entity11" } + + typealias Relationships = NoRelationships + } + + typealias TestEntity11 = BasicEntity } diff --git a/Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift b/Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift index 80f1bfa..5e5f593 100644 --- a/Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift +++ b/Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift @@ -597,3 +597,92 @@ let ten_different_type_includes = """ } ] """.data(using: .utf8)! + +let eleven_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" + } +] +""".data(using: .utf8)! diff --git a/Tests/JSONAPITests/Includes/stubs/one_include.json b/Tests/JSONAPITests/Includes/stubs/one_include.json deleted file mode 100644 index 9d2a3a4..0000000 --- a/Tests/JSONAPITests/Includes/stubs/one_include.json +++ /dev/null @@ -1,10 +0,0 @@ -[ - { - "type": "test_entity1", - "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", - "attributes": { - "foo": "Hello", - "bar": 123 - } - } -] diff --git a/Tests/JSONAPITests/Includes/stubs/three_different_type_includes.json b/Tests/JSONAPITests/Includes/stubs/three_different_type_includes.json deleted file mode 100644 index edd1ba8..0000000 --- a/Tests/JSONAPITests/Includes/stubs/three_different_type_includes.json +++ /dev/null @@ -1,30 +0,0 @@ -[ - { - "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_entity4", - "id": "364B3B69-4DF1-467F-B52E-B0C9E44F666E" - } -] diff --git a/Tests/JSONAPITests/Includes/stubs/two_different_type_includes.json b/Tests/JSONAPITests/Includes/stubs/two_different_type_includes.json deleted file mode 100644 index 6ef8e14..0000000 --- a/Tests/JSONAPITests/Includes/stubs/two_different_type_includes.json +++ /dev/null @@ -1,26 +0,0 @@ -[ - { - "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" - } - } - } - } -] diff --git a/Tests/JSONAPITests/Includes/stubs/two_same_type_includes.json b/Tests/JSONAPITests/Includes/stubs/two_same_type_includes.json deleted file mode 100644 index d1dfb42..0000000 --- a/Tests/JSONAPITests/Includes/stubs/two_same_type_includes.json +++ /dev/null @@ -1,18 +0,0 @@ -[ - { - "type": "test_entity1", - "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", - "attributes": { - "foo": "Hello", - "bar": 123 - } - }, - { - "type": "test_entity1", - "id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333", - "attributes": { - "foo": "World", - "bar": 456 - } - } -] diff --git a/Tests/JSONAPITests/Poly/stubs/PolyStubs.swift b/Tests/JSONAPITests/Poly/stubs/PolyStubs.swift index fa4ac4d..b1254cf 100644 --- a/Tests/JSONAPITests/Poly/stubs/PolyStubs.swift +++ b/Tests/JSONAPITests/Poly/stubs/PolyStubs.swift @@ -114,3 +114,10 @@ let poly_entity10 = """ "id": "A24B3444-4DF1-467F-B52E-B0C9E12F436A" } """.data(using: .utf8)! + +let poly_entity11 = """ +{ + "type": "test_entity11", + "id": "A24B3444-4DF1-467F-B52E-B0C9E12F4440" +} +""".data(using: .utf8)!