mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
merge and fix conflicts
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Mathew Polzin on 11/5/19.
|
||||
//
|
||||
|
||||
import JSONAPI
|
||||
|
||||
public enum ArrayElementComparison: Equatable, CustomStringConvertible {
|
||||
case same
|
||||
case missing
|
||||
case differentTypes(String, String)
|
||||
case differentValues(String, String)
|
||||
case prebuilt(String)
|
||||
|
||||
public init(sameTypeComparison: Comparison) {
|
||||
switch sameTypeComparison {
|
||||
case .same:
|
||||
self = .same
|
||||
case .different(let one, let two):
|
||||
self = .differentValues(one, two)
|
||||
case .prebuilt(let str):
|
||||
self = .prebuilt(str)
|
||||
}
|
||||
}
|
||||
|
||||
public init(resourceObjectComparison: ResourceObjectComparison) {
|
||||
guard !resourceObjectComparison.isSame else {
|
||||
self = .same
|
||||
return
|
||||
}
|
||||
|
||||
self = .prebuilt(
|
||||
resourceObjectComparison
|
||||
.differences
|
||||
.sorted { $0.key < $1.key }
|
||||
.map { "\($0.key): \($0.value)" }
|
||||
.joined(separator: ", ")
|
||||
)
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
switch self {
|
||||
case .same:
|
||||
return "same"
|
||||
case .missing:
|
||||
return "missing"
|
||||
case .differentTypes(let one, let two),
|
||||
.differentValues(let one, let two):
|
||||
return "\(one) ≠ \(two)"
|
||||
case .prebuilt(let description):
|
||||
return description
|
||||
}
|
||||
}
|
||||
|
||||
public var rawValue: String { description }
|
||||
}
|
||||
|
||||
extension Array {
|
||||
func compare(to other: Self, using compare: (Element, Element) -> ArrayElementComparison) -> [ArrayElementComparison] {
|
||||
let isSelfLonger = count >= other.count
|
||||
|
||||
let longer = isSelfLonger ? self : other
|
||||
let shorter = isSelfLonger ? other : self
|
||||
|
||||
return longer.indices.map { idx in
|
||||
guard shorter.indices.contains(idx) else {
|
||||
return .missing
|
||||
}
|
||||
|
||||
let this = longer[idx]
|
||||
let other = shorter[idx]
|
||||
|
||||
return compare(this, other)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Mathew Polzin on 11/3/19.
|
||||
//
|
||||
|
||||
import JSONAPI
|
||||
|
||||
extension Attributes {
|
||||
public func compare(to other: Self) -> [String: Comparison] {
|
||||
let mirror1 = Mirror(reflecting: self)
|
||||
let mirror2 = Mirror(reflecting: other)
|
||||
|
||||
var comparisons = [String: Comparison]()
|
||||
|
||||
for child in mirror1.children {
|
||||
guard let childLabel = child.label else { continue }
|
||||
|
||||
let childDescription = attributeDescription(of: child.value)
|
||||
|
||||
guard let otherChild = mirror2.children.first(where: { $0.label == childLabel }) else {
|
||||
comparisons[childLabel] = .different(childDescription, "missing")
|
||||
continue
|
||||
}
|
||||
|
||||
if (attributesEqual(child.value, otherChild.value)) {
|
||||
comparisons[childLabel] = .same
|
||||
} else {
|
||||
let otherChildDescription = attributeDescription(of: otherChild.value)
|
||||
|
||||
comparisons[childLabel] = .different(childDescription, otherChildDescription)
|
||||
}
|
||||
}
|
||||
|
||||
return comparisons
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func attributesEqual(_ one: Any, _ two: Any) -> Bool {
|
||||
guard let attr = one as? AbstractAttribute else {
|
||||
return false
|
||||
}
|
||||
|
||||
return attr.equals(two)
|
||||
}
|
||||
|
||||
fileprivate func attributeDescription(of thing: Any) -> String {
|
||||
return (thing as? AbstractAttribute)?.abstractDescription ?? String(describing: thing)
|
||||
}
|
||||
|
||||
protocol AbstractAttribute {
|
||||
var abstractDescription: String { get }
|
||||
|
||||
func equals(_ other: Any) -> Bool
|
||||
}
|
||||
|
||||
extension Attribute: AbstractAttribute {
|
||||
var abstractDescription: String { String(describing: value) }
|
||||
|
||||
func equals(_ other: Any) -> Bool {
|
||||
guard let attributeB = other as? Self else {
|
||||
return false
|
||||
}
|
||||
return abstractDescription == attributeB.abstractDescription
|
||||
}
|
||||
}
|
||||
|
||||
extension TransformedAttribute: AbstractAttribute {
|
||||
var abstractDescription: String { String(describing: value) }
|
||||
|
||||
func equals(_ other: Any) -> Bool {
|
||||
guard let attributeB = other as? Self else {
|
||||
return false
|
||||
}
|
||||
return abstractDescription == attributeB.abstractDescription
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// Comparison.swift
|
||||
//
|
||||
//
|
||||
// Created by Mathew Polzin on 11/3/19.
|
||||
//
|
||||
|
||||
public enum Comparison: Equatable, CustomStringConvertible {
|
||||
case same
|
||||
case different(String, String)
|
||||
case prebuilt(String)
|
||||
|
||||
init<T: Equatable>(_ one: T, _ two: T) {
|
||||
guard one == two else {
|
||||
self = .different(String(describing: one), String(describing: two))
|
||||
return
|
||||
}
|
||||
self = .same
|
||||
}
|
||||
|
||||
init(reducing other: ArrayElementComparison) {
|
||||
switch other {
|
||||
case .same:
|
||||
self = .same
|
||||
case .differentTypes(let one, let two),
|
||||
.differentValues(let one, let two):
|
||||
self = .different(one, two)
|
||||
case .missing:
|
||||
self = .different("array length 1", "array length 2")
|
||||
case .prebuilt(let str):
|
||||
self = .prebuilt(str)
|
||||
}
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
switch self {
|
||||
case .same:
|
||||
return "same"
|
||||
case .different(let one, let two):
|
||||
return "\(one) ≠ \(two)"
|
||||
case .prebuilt(let str):
|
||||
return str
|
||||
}
|
||||
}
|
||||
|
||||
public var rawValue: String { description }
|
||||
|
||||
public var isSame: Bool { self == .same }
|
||||
}
|
||||
|
||||
public typealias NamedDifferences = [String: String]
|
||||
|
||||
public protocol PropertyComparable: CustomStringConvertible {
|
||||
var differences: NamedDifferences { get }
|
||||
}
|
||||
|
||||
extension PropertyComparable {
|
||||
public var description: String {
|
||||
return differences
|
||||
.map { "(\($0): \($1))" }
|
||||
.sorted()
|
||||
.joined(separator: ", ")
|
||||
}
|
||||
|
||||
public var rawValue: String { description }
|
||||
|
||||
public var isSame: Bool { differences.isEmpty }
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
//
|
||||
// DocumentCompare.swift
|
||||
// JSONAPITesting
|
||||
//
|
||||
// Created by Mathew Polzin on 11/4/19.
|
||||
//
|
||||
|
||||
import JSONAPI
|
||||
|
||||
public struct DocumentComparison: Equatable, PropertyComparable {
|
||||
public let apiDescription: Comparison
|
||||
public let body: BodyComparison
|
||||
|
||||
init(apiDescription: Comparison, body: BodyComparison) {
|
||||
self.apiDescription = apiDescription
|
||||
self.body = body
|
||||
}
|
||||
|
||||
public var differences: NamedDifferences {
|
||||
return Dictionary(
|
||||
[
|
||||
apiDescription != .same ? ("API Description", apiDescription.rawValue) : nil,
|
||||
body != .same ? ("Body", body.rawValue) : nil
|
||||
].compactMap { $0 },
|
||||
uniquingKeysWith: { $1 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
public enum BodyComparison: Equatable, CustomStringConvertible {
|
||||
case same
|
||||
case dataErrorMismatch(errorOnLeft: Bool)
|
||||
case differentErrors(ErrorComparison)
|
||||
case differentData(DocumentDataComparison)
|
||||
|
||||
public typealias ErrorComparison = [Comparison]
|
||||
|
||||
static func compare<E: JSONAPIError, M: JSONAPI.Meta, L: JSONAPI.Links>(errors errors1: [E], _ meta1: M?, _ links1: L?, with errors2: [E], _ meta2: M?, _ links2: L?) -> ErrorComparison {
|
||||
return errors1.compare(
|
||||
to: errors2,
|
||||
using: { error1, error2 in
|
||||
guard error1 != error2 else {
|
||||
return .same
|
||||
}
|
||||
|
||||
return .differentValues(
|
||||
String(describing: error1),
|
||||
String(describing: error2)
|
||||
)
|
||||
}
|
||||
).map(Comparison.init) + [
|
||||
Comparison(meta1, meta2),
|
||||
Comparison(links1, links2)
|
||||
]
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
switch self {
|
||||
case .same:
|
||||
return "same"
|
||||
case .dataErrorMismatch(errorOnLeft: let errorOnLeft):
|
||||
let errorString = "error response"
|
||||
let dataString = "data response"
|
||||
let left = errorOnLeft ? errorString : dataString
|
||||
let right = errorOnLeft ? dataString : errorString
|
||||
|
||||
return "\(left) ≠ \(right)"
|
||||
case .differentErrors(let comparisons):
|
||||
return comparisons
|
||||
.filter { !$0.isSame }
|
||||
.map { $0.rawValue }
|
||||
.sorted()
|
||||
.joined(separator: ", ")
|
||||
case .differentData(let comparison):
|
||||
return comparison.rawValue
|
||||
}
|
||||
}
|
||||
|
||||
public var rawValue: String { description }
|
||||
}
|
||||
|
||||
extension Document {
|
||||
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 {
|
||||
return DocumentComparison(
|
||||
apiDescription: Comparison(
|
||||
String(describing: apiDescription),
|
||||
String(describing: other.apiDescription)
|
||||
),
|
||||
body: body.compare(to: other.body)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension Document.Body {
|
||||
public func compare<T>(to other: Self) -> BodyComparison where T: ResourceObjectType, PrimaryResourceBody == SingleResourceBody<T> {
|
||||
guard self != other else {
|
||||
return .same
|
||||
}
|
||||
|
||||
switch (self, other) {
|
||||
case (.errors(let errors1), .errors(let errors2)):
|
||||
return .differentErrors(BodyComparison.compare(errors: errors1.0,
|
||||
errors1.meta,
|
||||
errors1.links,
|
||||
with: errors2.0,
|
||||
errors2.meta,
|
||||
errors2.links))
|
||||
case (.errors, .data):
|
||||
return .dataErrorMismatch(errorOnLeft: true)
|
||||
case (.data, .errors):
|
||||
return .dataErrorMismatch(errorOnLeft: false)
|
||||
case (.data(let data1), .data(let data2)):
|
||||
return .differentData(data1.compare(to: data2))
|
||||
}
|
||||
}
|
||||
|
||||
public func compare<T>(to other: Self) -> BodyComparison where T: ResourceObjectType, PrimaryResourceBody == SingleResourceBody<T?> {
|
||||
guard self != other else {
|
||||
return .same
|
||||
}
|
||||
|
||||
switch (self, other) {
|
||||
case (.errors(let errors1), .errors(let errors2)):
|
||||
return .differentErrors(BodyComparison.compare(errors: errors1.0,
|
||||
errors1.meta,
|
||||
errors1.links,
|
||||
with: errors2.0,
|
||||
errors2.meta,
|
||||
errors2.links))
|
||||
case (.errors, .data):
|
||||
return .dataErrorMismatch(errorOnLeft: true)
|
||||
case (.data, .errors):
|
||||
return .dataErrorMismatch(errorOnLeft: false)
|
||||
case (.data(let data1), .data(let data2)):
|
||||
return .differentData(data1.compare(to: data2))
|
||||
}
|
||||
}
|
||||
|
||||
public func compare<T>(to other: Self) -> BodyComparison where T: ResourceObjectType, PrimaryResourceBody == ManyResourceBody<T> {
|
||||
guard self != other else {
|
||||
return .same
|
||||
}
|
||||
|
||||
switch (self, other) {
|
||||
case (.errors(let errors1), .errors(let errors2)):
|
||||
return .differentErrors(BodyComparison.compare(errors: errors1.0,
|
||||
errors1.meta,
|
||||
errors1.links,
|
||||
with: errors2.0,
|
||||
errors2.meta,
|
||||
errors2.links))
|
||||
case (.errors, .data):
|
||||
return .dataErrorMismatch(errorOnLeft: true)
|
||||
case (.data, .errors):
|
||||
return .dataErrorMismatch(errorOnLeft: false)
|
||||
case (.data(let data1), .data(let data2)):
|
||||
return .differentData(data1.compare(to: data2))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
//
|
||||
// DocumentDataCompare.swift
|
||||
//
|
||||
//
|
||||
// Created by Mathew Polzin on 11/5/19.
|
||||
//
|
||||
|
||||
import JSONAPI
|
||||
|
||||
public struct DocumentDataComparison: Equatable, PropertyComparable {
|
||||
public let primary: PrimaryResourceBodyComparison
|
||||
public let includes: IncludesComparison
|
||||
public let meta: Comparison
|
||||
public let links: Comparison
|
||||
|
||||
init(primary: PrimaryResourceBodyComparison, includes: IncludesComparison, meta: Comparison, links: Comparison) {
|
||||
self.primary = primary
|
||||
self.includes = includes
|
||||
self.meta = meta
|
||||
self.links = links
|
||||
}
|
||||
|
||||
public var differences: NamedDifferences {
|
||||
return Dictionary(
|
||||
[
|
||||
!primary.isSame ? ("Primary Resource", primary.rawValue) : nil,
|
||||
!includes.isSame ? ("Includes", includes.rawValue) : nil,
|
||||
!meta.isSame ? ("Meta", meta.rawValue) : nil,
|
||||
!links.isSame ? ("Links", links.rawValue) : nil
|
||||
].compactMap { $0 },
|
||||
uniquingKeysWith: { $1 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
extension Document.Body.Data {
|
||||
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 == 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> {
|
||||
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 enum PrimaryResourceBodyComparison: Equatable, CustomStringConvertible {
|
||||
case single(ResourceObjectComparison)
|
||||
case many(ManyResourceObjectComparison)
|
||||
case other(Comparison)
|
||||
|
||||
public var isSame: Bool {
|
||||
switch self {
|
||||
case .other(let comparison):
|
||||
return comparison == .same
|
||||
case .single(let comparison):
|
||||
return comparison.isSame
|
||||
case .many(let comparison):
|
||||
return comparison.isSame
|
||||
}
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
switch self {
|
||||
case .other(let comparison):
|
||||
return comparison.rawValue
|
||||
case .single(let comparison):
|
||||
return comparison.rawValue
|
||||
case .many(let comparison):
|
||||
return comparison.rawValue
|
||||
}
|
||||
}
|
||||
|
||||
public var rawValue: String { return description }
|
||||
}
|
||||
|
||||
public struct ManyResourceObjectComparison: Equatable, PropertyComparable {
|
||||
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["resource \(next.offset + 1)"] = next.element.rawValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension SingleResourceBody where Entity: ResourceObjectType {
|
||||
public func compare(to other: Self) -> PrimaryResourceBodyComparison {
|
||||
return .single(.init(value, other.value))
|
||||
}
|
||||
}
|
||||
|
||||
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 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 .single(.init(one, two))
|
||||
}
|
||||
}
|
||||
|
||||
extension ManyResourceBody where Entity: ResourceObjectType {
|
||||
public func compare(to other: Self) -> PrimaryResourceBodyComparison {
|
||||
return .many(.init(values.compare(to: other.values, using: { r1, r2 in
|
||||
let r1AsResource = r1 as? AbstractResourceObjectType
|
||||
|
||||
let maybeComparison = r1AsResource
|
||||
.flatMap { resource in
|
||||
try? ArrayElementComparison(
|
||||
resourceObjectComparison: resource.abstractCompare(to: r2)
|
||||
)
|
||||
}
|
||||
|
||||
guard let comparison = maybeComparison else {
|
||||
return .differentValues(
|
||||
String(describing: r1),
|
||||
String(describing: r2)
|
||||
)
|
||||
}
|
||||
|
||||
return comparison
|
||||
})))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// IncludesCompare.swift
|
||||
//
|
||||
//
|
||||
// Created by Mathew Polzin on 11/4/19.
|
||||
//
|
||||
|
||||
import JSONAPI
|
||||
import Poly
|
||||
|
||||
public struct IncludesComparison: Equatable, PropertyComparable {
|
||||
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
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by Mathew Polzin on 11/3/19.
|
||||
//
|
||||
|
||||
import JSONAPI
|
||||
|
||||
extension Relationships {
|
||||
public func compare(to other: Self) -> [String: Comparison] {
|
||||
let mirror1 = Mirror(reflecting: self)
|
||||
let mirror2 = Mirror(reflecting: other)
|
||||
|
||||
var comparisons = [String: Comparison]()
|
||||
|
||||
for child in mirror1.children {
|
||||
guard let childLabel = child.label else { continue }
|
||||
|
||||
let childDescription = relationshipDescription(of: child.value)
|
||||
|
||||
guard let otherChild = mirror2.children.first(where: { $0.label == childLabel }) else {
|
||||
comparisons[childLabel] = .different(childDescription, "missing")
|
||||
continue
|
||||
}
|
||||
|
||||
if (relationshipsEqual(child.value, otherChild.value)) {
|
||||
comparisons[childLabel] = .same
|
||||
} else {
|
||||
let otherChildDescription = relationshipDescription(of: otherChild.value)
|
||||
|
||||
comparisons[childLabel] = .different(childDescription, otherChildDescription)
|
||||
}
|
||||
}
|
||||
|
||||
return comparisons
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func relationshipsEqual(_ one: Any, _ two: Any) -> Bool {
|
||||
guard let attr = one as? AbstractRelationship else {
|
||||
return false
|
||||
}
|
||||
|
||||
return attr.equals(two)
|
||||
}
|
||||
|
||||
fileprivate func relationshipDescription(of thing: Any) -> String {
|
||||
return (thing as? AbstractRelationship)?.abstractDescription ?? String(describing: thing)
|
||||
}
|
||||
|
||||
protocol AbstractRelationship {
|
||||
var abstractDescription: String { get }
|
||||
|
||||
func equals(_ other: Any) -> Bool
|
||||
}
|
||||
|
||||
extension ToOneRelationship: AbstractRelationship {
|
||||
var abstractDescription: String {
|
||||
if meta is NoMetadata && links is NoLinks {
|
||||
return String(describing: id)
|
||||
}
|
||||
|
||||
return String(describing:
|
||||
(
|
||||
String(describing: id),
|
||||
String(describing: meta),
|
||||
String(describing: links)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
func equals(_ other: Any) -> Bool {
|
||||
guard let attributeB = other as? Self else {
|
||||
return false
|
||||
}
|
||||
return abstractDescription == attributeB.abstractDescription
|
||||
}
|
||||
}
|
||||
|
||||
extension ToManyRelationship: AbstractRelationship {
|
||||
var abstractDescription: String {
|
||||
|
||||
let idsString = ids.map { String.init(describing: $0.rawValue) }.joined(separator: ", ")
|
||||
|
||||
if meta is NoMetadata && links is NoLinks {
|
||||
return idsString
|
||||
}
|
||||
|
||||
return String(describing:
|
||||
(
|
||||
idsString,
|
||||
String(describing: meta),
|
||||
String(describing: links)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
func equals(_ other: Any) -> Bool {
|
||||
guard let attributeB = other as? Self else {
|
||||
return false
|
||||
}
|
||||
return abstractDescription == attributeB.abstractDescription
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
//
|
||||
// ResourceObjectCompare.swift
|
||||
//
|
||||
//
|
||||
// Created by Mathew Polzin on 11/3/19.
|
||||
//
|
||||
|
||||
import JSONAPI
|
||||
|
||||
public struct ResourceObjectComparison: Equatable, PropertyComparable {
|
||||
public typealias ComparisonHash = [String: Comparison]
|
||||
|
||||
public let id: Comparison
|
||||
public let attributes: ComparisonHash
|
||||
public let relationships: ComparisonHash
|
||||
public let meta: Comparison
|
||||
public let links: Comparison
|
||||
|
||||
public init<T: ResourceObjectType>(_ one: T, _ two: T) {
|
||||
id = Comparison(one.id.rawValue, two.id.rawValue)
|
||||
attributes = one.attributes.compare(to: two.attributes)
|
||||
relationships = one.relationships.compare(to: two.relationships)
|
||||
meta = Comparison(one.meta, two.meta)
|
||||
links = Comparison(one.links, two.links)
|
||||
}
|
||||
|
||||
public var differences: NamedDifferences {
|
||||
return attributes.reduce(into: ComparisonHash()) { hash, next in
|
||||
hash["'\(next.key)' attribute"] = next.value
|
||||
}
|
||||
.merging(
|
||||
relationships.reduce(into: ComparisonHash()) { hash, next in
|
||||
hash["'\(next.key)' relationship"] = next.value
|
||||
},
|
||||
uniquingKeysWith: { $1 }
|
||||
)
|
||||
.merging(
|
||||
[
|
||||
"id": id,
|
||||
"meta": meta,
|
||||
"links": links
|
||||
],
|
||||
uniquingKeysWith: { $1 }
|
||||
)
|
||||
.filter { $1 != .same }
|
||||
.mapValues { $0.rawValue }
|
||||
}
|
||||
}
|
||||
|
||||
extension ResourceObjectType {
|
||||
public func compare(to other: Self) -> ResourceObjectComparison {
|
||||
return ResourceObjectComparison(self, other)
|
||||
}
|
||||
}
|
||||
|
||||
protocol AbstractResourceObjectType {
|
||||
func abstractCompare(to other: Any) throws -> ResourceObjectComparison
|
||||
}
|
||||
|
||||
enum AbstractCompareError: Swift.Error {
|
||||
case typeMismatch
|
||||
}
|
||||
|
||||
extension ResourceObject: AbstractResourceObjectType {
|
||||
func abstractCompare(to other: Any) throws -> ResourceObjectComparison {
|
||||
guard let otherResource = other as? Self else {
|
||||
throw AbstractCompareError.typeMismatch
|
||||
}
|
||||
return self.compare(to: otherResource)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
//
|
||||
// Optional+ZipWith.swift
|
||||
//
|
||||
// Created by Mathew Polzin on 1/19/19.
|
||||
//
|
||||
|
||||
/// Zip two optionals together with the given operation performed on
|
||||
/// the unwrapped contents. If either optional is nil, the zip
|
||||
/// yields nil.
|
||||
func zip<X, Y, Z>(_ left: X?, _ right: Y?, with fn: (X, Y) -> Z) -> Z? {
|
||||
return left.flatMap { lft in right.map { rght in fn(lft, rght) }}
|
||||
}
|
||||
Reference in New Issue
Block a user