mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
go from 3 specializations of all document related compare functions down to 2.
This commit is contained in:
@@ -79,28 +79,8 @@ public enum BodyComparison: Equatable, CustomStringConvertible {
|
||||
public var rawValue: String { description }
|
||||
}
|
||||
|
||||
extension EncodableJSONAPIDocument where Body: Equatable {
|
||||
public func compare<T>(to other: Self) -> DocumentComparison where PrimaryResourceBody == SingleResourceBody<T>, T: ResourceObjectType {
|
||||
return DocumentComparison(
|
||||
apiDescription: Comparison(
|
||||
String(describing: apiDescription),
|
||||
String(describing: other.apiDescription)
|
||||
),
|
||||
body: body.compare(to: other.body)
|
||||
)
|
||||
}
|
||||
|
||||
public func compare<T>(to other: Self) -> DocumentComparison where PrimaryResourceBody == SingleResourceBody<T?>, T: ResourceObjectType {
|
||||
return DocumentComparison(
|
||||
apiDescription: Comparison(
|
||||
String(describing: apiDescription),
|
||||
String(describing: other.apiDescription)
|
||||
),
|
||||
body: body.compare(to: other.body)
|
||||
)
|
||||
}
|
||||
|
||||
public func compare<T>(to other: Self) -> DocumentComparison where PrimaryResourceBody == ManyResourceBody<T>, T: ResourceObjectType {
|
||||
extension EncodableJSONAPIDocument where Body: Equatable, PrimaryResourceBody: _ResourceBody {
|
||||
public func compare(to other: Self) -> DocumentComparison {
|
||||
return DocumentComparison(
|
||||
apiDescription: Comparison(
|
||||
String(describing: apiDescription),
|
||||
@@ -111,36 +91,20 @@ extension EncodableJSONAPIDocument where Body: Equatable {
|
||||
}
|
||||
}
|
||||
|
||||
extension DocumentBody where Self: Equatable {
|
||||
public func compare<T>(to other: Self) -> BodyComparison where T: ResourceObjectType, PrimaryResourceBody == SingleResourceBody<T> {
|
||||
|
||||
// rule out case where they are the same
|
||||
guard self != other else {
|
||||
return .same
|
||||
}
|
||||
|
||||
// rule out case where they are both error bodies
|
||||
if let errors1 = errors, let errors2 = other.errors {
|
||||
return .differentErrors(
|
||||
BodyComparison.compare(
|
||||
errors: errors1, meta, links,
|
||||
with: errors2, meta, links
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
// rule out the case where they are both data
|
||||
if let data1 = data, let data2 = other.data {
|
||||
return .differentData(data1.compare(to: data2))
|
||||
}
|
||||
|
||||
// we are left with the case where one is data and the
|
||||
// other is an error if self.isError, then "the error
|
||||
// is on the left"
|
||||
return .dataErrorMismatch(errorOnLeft: isError)
|
||||
extension EncodableJSONAPIDocument where Body: Equatable, PrimaryResourceBody: _OptionalResourceBody {
|
||||
public func compare(to other: Self) -> DocumentComparison {
|
||||
return DocumentComparison(
|
||||
apiDescription: Comparison(
|
||||
String(describing: apiDescription),
|
||||
String(describing: other.apiDescription)
|
||||
),
|
||||
body: body.compare(to: other.body)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
public func compare<T>(to other: Self) -> BodyComparison where T: ResourceObjectType, PrimaryResourceBody == SingleResourceBody<T?> {
|
||||
extension DocumentBody where Self: Equatable, PrimaryResourceBody: _ResourceBody {
|
||||
public func compare(to other: Self) -> BodyComparison {
|
||||
|
||||
// rule out case where they are the same
|
||||
guard self != other else {
|
||||
@@ -167,8 +131,10 @@ extension DocumentBody where Self: Equatable {
|
||||
// is on the left"
|
||||
return .dataErrorMismatch(errorOnLeft: isError)
|
||||
}
|
||||
}
|
||||
|
||||
public func compare<T>(to other: Self) -> BodyComparison where T: ResourceObjectType, PrimaryResourceBody == ManyResourceBody<T> {
|
||||
extension DocumentBody where Self: Equatable, PrimaryResourceBody: _OptionalResourceBody {
|
||||
public func compare(to other: Self) -> BodyComparison {
|
||||
|
||||
// rule out case where they are the same
|
||||
guard self != other else {
|
||||
|
||||
@@ -33,8 +33,8 @@ public struct DocumentDataComparison: Equatable, PropertyComparable {
|
||||
}
|
||||
}
|
||||
|
||||
extension DocumentBodyData {
|
||||
public func compare<T>(to other: Self) -> DocumentDataComparison where T: ResourceObjectType, PrimaryResourceBody == SingleResourceBody<T> {
|
||||
extension DocumentBodyData where PrimaryResourceBody: _ResourceBody {
|
||||
public func compare(to other: Self) -> DocumentDataComparison {
|
||||
return .init(
|
||||
primary: primary.compare(to: other.primary),
|
||||
includes: includes.compare(to: other.includes),
|
||||
@@ -42,17 +42,10 @@ extension DocumentBodyData {
|
||||
links: Comparison(links, other.links)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
public func compare<T>(to other: Self) -> DocumentDataComparison where T: ResourceObjectType, PrimaryResourceBody == SingleResourceBody<T?> {
|
||||
return .init(
|
||||
primary: primary.compare(to: other.primary),
|
||||
includes: includes.compare(to: other.includes),
|
||||
meta: Comparison(meta, other.meta),
|
||||
links: Comparison(links, other.links)
|
||||
)
|
||||
}
|
||||
|
||||
public func compare<T>(to other: Self) -> DocumentDataComparison where T: ResourceObjectType, PrimaryResourceBody == ManyResourceBody<T> {
|
||||
extension DocumentBodyData where PrimaryResourceBody: _OptionalResourceBody {
|
||||
public func compare(to other: Self) -> DocumentDataComparison {
|
||||
return .init(
|
||||
primary: primary.compare(to: other.primary),
|
||||
includes: includes.compare(to: other.includes),
|
||||
@@ -109,42 +102,24 @@ public struct ManyResourceObjectComparison: Equatable, PropertyComparable {
|
||||
}
|
||||
}
|
||||
|
||||
extension SingleResourceBody where Entity: ResourceObjectType {
|
||||
extension _OptionalResourceBody where WrappedPrimaryResourceType: ResourceObjectType {
|
||||
public func compare(to other: Self) -> PrimaryResourceBodyComparison {
|
||||
return .single(.init(value, other.value))
|
||||
}
|
||||
}
|
||||
guard let one = optionalResourceObject,
|
||||
let two = other.optionalResourceObject else {
|
||||
|
||||
public protocol _OptionalResourceObjectType {
|
||||
associatedtype Wrapped: ResourceObjectType
|
||||
func nilOrName<T>(_ resObj: T?) -> String {
|
||||
resObj.map { String(describing: type(of: $0)) } ?? "nil"
|
||||
}
|
||||
|
||||
var maybeValue: Wrapped? { get }
|
||||
}
|
||||
|
||||
extension Optional: _OptionalResourceObjectType where Wrapped: ResourceObjectType {
|
||||
public var maybeValue: Wrapped? {
|
||||
switch self {
|
||||
case .none:
|
||||
return nil
|
||||
case .some(let value):
|
||||
return value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension SingleResourceBody where Entity: _OptionalResourceObjectType {
|
||||
public func compare(to other: Self) -> PrimaryResourceBodyComparison {
|
||||
guard let one = value.maybeValue,
|
||||
let two = other.value.maybeValue else {
|
||||
return .other(Comparison(value, other.value))
|
||||
return .other(Comparison(nilOrName(optionalResourceObject), nilOrName(other.optionalResourceObject)))
|
||||
}
|
||||
return .single(.init(one, two))
|
||||
}
|
||||
}
|
||||
|
||||
extension ManyResourceBody where Entity: ResourceObjectType {
|
||||
extension _ResourceBody where PrimaryResourceType: ResourceObjectType {
|
||||
public func compare(to other: Self) -> PrimaryResourceBodyComparison {
|
||||
return .many(.init(values.compare(to: other.values, using: { r1, r2 in
|
||||
return .many(.init(resourceObjects.compare(to: other.resourceObjects, using: { r1, r2 in
|
||||
let r1AsResource = r1 as? AbstractResourceObjectType
|
||||
|
||||
let maybeComparison = r1AsResource
|
||||
@@ -165,3 +140,45 @@ extension ManyResourceBody where Entity: ResourceObjectType {
|
||||
})))
|
||||
}
|
||||
}
|
||||
|
||||
public protocol _ResourceBody {
|
||||
associatedtype PrimaryResourceType: ResourceObjectType
|
||||
var resourceObjects: [PrimaryResourceType] { get }
|
||||
}
|
||||
|
||||
public protocol _OptionalResourceBody {
|
||||
associatedtype WrappedPrimaryResourceType: ResourceObjectType
|
||||
var optionalResourceObject: WrappedPrimaryResourceType? { get }
|
||||
}
|
||||
|
||||
public protocol _OptionalResourceObjectType {
|
||||
associatedtype Wrapped: ResourceObjectType
|
||||
|
||||
var maybeValue: Wrapped? { get }
|
||||
}
|
||||
|
||||
extension Optional: _OptionalResourceObjectType where Wrapped: ResourceObjectType {
|
||||
public var maybeValue: Wrapped? {
|
||||
switch self {
|
||||
case .none:
|
||||
return nil
|
||||
case .some(let value):
|
||||
return value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension ManyResourceBody: _ResourceBody where PrimaryResource: ResourceObjectType {
|
||||
public var resourceObjects: [PrimaryResource] { values }
|
||||
}
|
||||
|
||||
extension SingleResourceBody: _ResourceBody where PrimaryResource: ResourceObjectType {
|
||||
public typealias PrimaryResourceType = PrimaryResource
|
||||
public var resourceObjects: [PrimaryResource] { [value] }
|
||||
}
|
||||
|
||||
extension SingleResourceBody: _OptionalResourceBody where PrimaryResource: _OptionalResourceObjectType {
|
||||
public typealias WrappedPrimaryResourceType = PrimaryResource.Wrapped
|
||||
|
||||
public var optionalResourceObject: WrappedPrimaryResourceType? { value.maybeValue }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user