Skip to content

Commit

Permalink
add support for 14 includes
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpolzin committed Mar 27, 2024
1 parent 399ec4c commit f1945d2
Show file tree
Hide file tree
Showing 6 changed files with 184 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": "c108e9e0a2904134719b082f6c18d64406afc6db",
"version": "2.6.0"
"revision": "fb90ab22a09fe32e8cb152f7dea344df82a59d53",
"version": "2.7.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.6.0")),
.package(url: "https://github.com/mattpolzin/Poly.git", .upToNextMajor(from: "2.7.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 @@ -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
Expand Down
35 changes: 35 additions & 0 deletions Sources/JSONAPI/Resource/Poly+PrimaryResource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
36 changes: 36 additions & 0 deletions Tests/JSONAPITests/Includes/IncludeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,31 @@ class IncludedTests: XCTestCase {
test_DecodeEncodeEquality(type: Includes<Include13<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11, TestEntity12, TestEntity13>>.self,
data: thirteen_different_type_includes)
}

func test_FourteenDifferentIncludes() {
let includes = decoded(type: Includes<Include14<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11, TestEntity12, TestEntity13, TestEntity14>>.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<Include14<TestEntity, TestEntity2, TestEntity3, TestEntity4, TestEntity5, TestEntity6, TestEntity7, TestEntity8, TestEntity9, TestEntity10, TestEntity11, TestEntity12, TestEntity13, TestEntity14>>.self,
data: fourteen_different_type_includes)
}
}

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

typealias TestEntity13 = BasicEntity<TestEntityType13>

enum TestEntityType14: ResourceObjectDescription {

typealias Attributes = NoAttributes

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

typealias Relationships = NoRelationships
}

typealias TestEntity14 = BasicEntity<TestEntityType14>
}
102 changes: 102 additions & 0 deletions Tests/JSONAPITests/Includes/stubs/IncludeStubs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """
[
{
Expand Down

0 comments on commit f1945d2

Please sign in to comment.