Skip to content

Commit 399ec4c

Browse files
authored
Merge pull request #111 from nekrich/chore/allow-mixed-meta-when-merging
chore: allow mixing meta while merging Body.Data
2 parents c5d1e71 + 7f326df commit 399ec4c

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

Sources/JSONAPI/Document/Document.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ extension Document {
261261
}
262262

263263
extension Document.Body.Data where PrimaryResourceBody: ResourceBodyAppendable {
264-
public func merging(_ other: Document.Body.Data,
264+
public func merging<OtherDescription, OtherError>(_ other: Document<PrimaryResourceBody, MetaType, LinksType, IncludeType, OtherDescription, OtherError>.Body.Data,
265265
combiningMetaWith metaMerge: (MetaType, MetaType) -> MetaType,
266266
combiningLinksWith linksMerge: (LinksType, LinksType) -> LinksType) -> Document.Body.Data {
267267
return Document.Body.Data(primary: primary.appending(other.primary),
@@ -272,10 +272,11 @@ extension Document.Body.Data where PrimaryResourceBody: ResourceBodyAppendable {
272272
}
273273

274274
extension Document.Body.Data where PrimaryResourceBody: ResourceBodyAppendable, MetaType == NoMetadata, LinksType == NoLinks {
275-
public func merging(_ other: Document.Body.Data) -> Document.Body.Data {
276-
return merging(other,
277-
combiningMetaWith: { _, _ in .none },
278-
combiningLinksWith: { _, _ in .none })
275+
public func merging<OtherMeta, OtherLinks, OtherDescription, OtherError>(_ other: Document<PrimaryResourceBody, OtherMeta, OtherLinks, IncludeType, OtherDescription, OtherError>.Body.Data) -> Document<PrimaryResourceBody, OtherMeta, OtherLinks, IncludeType, APIDescription, Error>.Body.Data {
276+
return .init(primary: primary.appending(other.primary),
277+
includes: includes.appending(other.includes),
278+
meta: other.meta,
279+
links: other.links)
279280
}
280281
}
281282

Tests/JSONAPITests/Document/DocumentTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,25 @@ extension DocumentTests {
15281528
XCTAssertEqual(combined.primary.values, bodyData1.primary.values + bodyData2.primary.values)
15291529
}
15301530

1531+
public func test_MergeBodyDataMixedMetaLinksErrorAndAPI(){
1532+
let entity1 = Article(attributes: .none, relationships: .init(author: "2"), meta: .none, links: .none)
1533+
let entity2 = Article(attributes: .none, relationships: .init(author: "3"), meta: .none, links: .none)
1534+
1535+
let bodyData1 = Document<ManyResourceBody<Article>, NoMetadata, NoLinks, NoIncludes, NoAPIDescription, UnknownJSONAPIError>.Body.Data(primary: .init(resourceObjects: [entity1]),
1536+
includes: .none,
1537+
meta: .none,
1538+
links: .none)
1539+
let bodyData2 = Document<ManyResourceBody<Article>, TestPageMetadata, TestLinks, NoIncludes, TestAPIDescription, GenericJSONAPIError<String>>.Body.Data(primary: .init(resourceObjects: [entity2]),
1540+
includes: .none,
1541+
meta: .init(total: 5, limit: 2, offset: 2),
1542+
links: .init(link: .init(url: "one"), link2: .init(url: .init(), meta: .init(hello: "world"))))
1543+
let combined = bodyData1.merging(bodyData2)
1544+
1545+
XCTAssertEqual(combined.primary.values, bodyData1.primary.values + bodyData2.primary.values)
1546+
XCTAssertEqual(combined.meta, bodyData2.meta)
1547+
XCTAssertEqual(combined.links, bodyData2.links)
1548+
}
1549+
15311550
public func test_MergeBodyDataWithMergeFunctions() {
15321551
let article1 = Article(attributes: .none, relationships: .init(author: "2"), meta: .none, links: .none)
15331552
let author1 = Author(id: "2", attributes: .none, relationships: .none, meta: .none, links: .none)

0 commit comments

Comments
 (0)