Skip to content

Commit

Permalink
Merge pull request #114 from mattpolzin/include15-support
Browse files Browse the repository at this point in the history
support for Include15 type
  • Loading branch information
mattpolzin authored Mar 27, 2024
2 parents aaa014a + 9b919b6 commit 3856a88
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/mattpolzin/Poly.git",
"state": {
"branch": null,
"revision": "fb90ab22a09fe32e8cb152f7dea344df82a59d53",
"version": "2.7.0"
"revision": "99e2e8b575620369be52fe348c0dd72028e3674c",
"version": "2.8.0"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
targets: ["JSONAPITesting"])
],
dependencies: [
.package(url: "https://github.com/mattpolzin/Poly.git", .upToNextMajor(from: "2.7.0")),
.package(url: "https://github.com/mattpolzin/Poly.git", .upToNextMajor(from: "2.8.0")),
],
targets: [
.target(
Expand Down
8 changes: 8 additions & 0 deletions Sources/JSONAPI/Document/Includes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ extension Includes where I: _Poly14 {
}
}

// MARK: - 15 includes
public typealias Include15 = Poly15
extension Includes where I: _Poly15 {
public subscript(_ lookup: I.O.Type) -> [I.O] {
return values.compactMap(\.o)
}
}

// MARK: - DecodingError
public struct IncludesDecodingError: Swift.Error, Equatable {
public let error: Swift.Error
Expand Down
37 changes: 37 additions & 0 deletions Sources/JSONAPI/Resource/Poly+PrimaryResource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,40 @@ K: CodablePolyWrapped,
L: CodablePolyWrapped,
M: CodablePolyWrapped,
N: CodablePolyWrapped {}

// MARK: - 15 types
extension Poly15: 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,
O: EncodablePolyWrapped {}

extension Poly15: 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,
O: CodablePolyWrapped {}
37 changes: 37 additions & 0 deletions Tests/JSONAPITests/Includes/IncludeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,32 @@ class IncludedTests: XCTestCase {
test_DecodeEncodeEquality(type: Includes<Include14<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11, TestEntity12, TestEntity13, TestEntity14>>.self,
data: fourteen_different_type_includes)
}

func test_FifteenDifferentIncludes() {
let includes = decoded(type: Includes<Include15<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11, TestEntity12, TestEntity13, TestEntity14, TestEntity15>>.self,
data: fifteen_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)
XCTAssertEqual(includes[TestEntity15.self].count, 1)
}

func test_FifteenDifferentIncludes_encode() {
test_DecodeEncodeEquality(type: Includes<Include15<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11, TestEntity12, TestEntity13, TestEntity14, TestEntity15>>.self,
data: fifteen_different_type_includes)
}
}

// MARK: - Appending
Expand Down Expand Up @@ -641,4 +667,15 @@ extension IncludedTests {
}

typealias TestEntity14 = BasicEntity<TestEntityType14>

enum TestEntityType15: ResourceObjectDescription {

typealias Attributes = NoAttributes

public static var jsonType: String { return "test_entity15" }

typealias Relationships = NoRelationships
}

typealias TestEntity15 = BasicEntity<TestEntityType15>
}
105 changes: 105 additions & 0 deletions Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,111 @@ let fourteen_different_type_includes = """
]
""".data(using: .utf8)!

let fifteen_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"
},
{
"type": "test_entity15",
"id": "264B3B69-4DF3-312A-B32E-A0C9E44F26C01"
}
]
""".data(using: .utf8)!


let three_includes_one_missing_attributes = """
[
Expand Down

0 comments on commit 3856a88

Please sign in to comment.