mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
67 lines
2.0 KiB
Swift
67 lines
2.0 KiB
Swift
//
|
|
// IncludesCompare.swift
|
|
//
|
|
//
|
|
// Created by Mathew Polzin on 11/4/19.
|
|
//
|
|
|
|
import JSONAPI
|
|
import Poly
|
|
|
|
public struct IncludesComparison: Equatable, PropertyComparison {
|
|
public let comparisons: [ArrayElementComparison]
|
|
|
|
public init(_ comparisons: [ArrayElementComparison]) {
|
|
self.comparisons = comparisons
|
|
}
|
|
|
|
public var differences: NamedDifferences {
|
|
return comparisons
|
|
.enumerated()
|
|
.filter { $0.element != .same }
|
|
.reduce(into: [String: String]()) { hash, next in
|
|
hash["include \(next.offset + 1)"] = next.element.rawValue
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Includes {
|
|
public func compare(to other: Self) -> IncludesComparison {
|
|
|
|
return IncludesComparison(
|
|
values.compare(to: other.values) { thisInclude, otherInclude in
|
|
guard thisInclude != otherInclude else {
|
|
return .same
|
|
}
|
|
|
|
let thisWrappedValue = thisInclude.value
|
|
let otherWrappedValue = otherInclude.value
|
|
guard type(of: thisWrappedValue) == type(of: otherWrappedValue) else {
|
|
return .differentTypes(
|
|
String(describing: type(of: thisWrappedValue)),
|
|
String(describing: type(of: otherWrappedValue))
|
|
)
|
|
}
|
|
|
|
let thisAsAResource = thisWrappedValue as? AbstractResourceObjectType
|
|
|
|
let maybeComparison = thisAsAResource
|
|
.flatMap { resource in
|
|
try? ArrayElementComparison(
|
|
resourceObjectComparison: resource.abstractCompare(to: otherWrappedValue)
|
|
)
|
|
}
|
|
|
|
guard let comparison = maybeComparison else {
|
|
return .differentValues(
|
|
String(describing: thisWrappedValue),
|
|
String(describing: otherWrappedValue)
|
|
)
|
|
}
|
|
|
|
return comparison
|
|
}
|
|
)
|
|
}
|
|
}
|