Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: allow mixing meta while merging Body.Data #111

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Sources/JSONAPI/Document/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ extension Document {
}

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

extension Document.Body.Data where PrimaryResourceBody: ResourceBodyAppendable, MetaType == NoMetadata, LinksType == NoLinks {
public func merging(_ other: Document.Body.Data) -> Document.Body.Data {
return merging(other,
combiningMetaWith: { _, _ in .none },
combiningLinksWith: { _, _ in .none })
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 {
return .init(primary: primary.appending(other.primary),
includes: includes.appending(other.includes),
meta: other.meta,
links: other.links)
}
}

Expand Down
19 changes: 19 additions & 0 deletions Tests/JSONAPITests/Document/DocumentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,25 @@ extension DocumentTests {
XCTAssertEqual(combined.primary.values, bodyData1.primary.values + bodyData2.primary.values)
}

public func test_MergeBodyDataMixedMetaLinksErrorAndAPI(){
let entity1 = Article(attributes: .none, relationships: .init(author: "2"), meta: .none, links: .none)
let entity2 = Article(attributes: .none, relationships: .init(author: "3"), meta: .none, links: .none)

let bodyData1 = Document<ManyResourceBody<Article>, NoMetadata, NoLinks, NoIncludes, NoAPIDescription, UnknownJSONAPIError>.Body.Data(primary: .init(resourceObjects: [entity1]),
includes: .none,
meta: .none,
links: .none)
let bodyData2 = Document<ManyResourceBody<Article>, TestPageMetadata, TestLinks, NoIncludes, TestAPIDescription, GenericJSONAPIError<String>>.Body.Data(primary: .init(resourceObjects: [entity2]),
includes: .none,
meta: .init(total: 5, limit: 2, offset: 2),
links: .init(link: .init(url: "one"), link2: .init(url: .init(), meta: .init(hello: "world"))))
let combined = bodyData1.merging(bodyData2)

XCTAssertEqual(combined.primary.values, bodyData1.primary.values + bodyData2.primary.values)
mattpolzin marked this conversation as resolved.
Show resolved Hide resolved
XCTAssertEqual(combined.meta, bodyData2.meta)
XCTAssertEqual(combined.links, bodyData2.links)
}

public func test_MergeBodyDataWithMergeFunctions() {
let article1 = Article(attributes: .none, relationships: .init(author: "2"), meta: .none, links: .none)
let author1 = Author(id: "2", attributes: .none, relationships: .none, meta: .none, links: .none)
Expand Down
Loading