1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021-2023 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import Foundation
/// A builder that collects ``Differences`` between two objects of the same type.
///
/// The DifferenceBuilder is used when diffing two ``RenderNode`` objects against each other.
/// Initalize the DifferenceBuilder with the two.
struct DifferenceBuilder<T> {
var differences: JSONPatchDifferences
let current: T
let other: T
let path: CodablePath
init(current: T, other: T, basePath: CodablePath) {
self.differences = []
self.current = current
self.other = other
self.path = basePath
}
/// Adds the difference between two properties to the DifferenceBuilder.
mutating func addDifferences(atKeyPath keyPath: KeyPath<T, some Equatable & Encodable>, forKey codingKey: CodingKey)
{
let currentProperty = current[keyPath: keyPath]
let otherProperty = other[keyPath: keyPath]
if currentProperty != otherProperty {
differences.append(.replace(pointer: JSONPointer(from: path + [codingKey]), encodableValue: currentProperty))
}
}
/// Determines the difference between the two diffable objects at the KeyPaths given.
mutating func addDifferences(atKeyPath keyPath: KeyPath<T, some RenderJSONDiffable & Equatable & Encodable>, forKey codingKey: CodingKey)
{
let currentProperty = current[keyPath: keyPath]
let otherProperty = other[keyPath: keyPath]
if currentProperty == otherProperty {
return
}
if currentProperty.isSimilar(to: otherProperty) {
differences.append(contentsOf: currentProperty.difference(from: otherProperty, at: path + [codingKey]))
} else {
differences.append(.replace(pointer: JSONPointer(from: path + [codingKey]), encodableValue: currentProperty))
}
}
/// Determines the difference between the two arrays of diffable objects at the KeyPaths given.
mutating func addDifferences(atKeyPath keyPath: KeyPath<T, [String: some RenderJSONDiffable & Equatable & Encodable]>, forKey codingKey: CodingKey)
{
let currentProperty = current[keyPath: keyPath]
let otherProperty = other[keyPath: keyPath]
if currentProperty == otherProperty {
return
}
if currentProperty.isSimilar(to: otherProperty) {
differences.append(contentsOf: currentProperty.difference(from: otherProperty, at: path + [codingKey]))
} else {
differences.append(.replace(pointer: JSONPointer(from: path + [codingKey]), encodableValue: currentProperty))
}
}
/// Determines the difference between the two dictionaries mapping strings to diffable objects at the KeyPaths given.
mutating func addDifferences(atKeyPath keyPath: KeyPath<T, [some RenderJSONDiffable & Equatable & Codable]>, forKey codingKey: CodingKey)
{
let currentProperty = current[keyPath: keyPath]
let otherProperty = other[keyPath: keyPath]
if currentProperty == otherProperty {
return
}
if currentProperty.isSimilar(to: otherProperty) {
differences.append(contentsOf: currentProperty.difference(from: otherProperty, at: path + [codingKey]))
} else {
differences.append(.replace(pointer: JSONPointer(from: path + [codingKey]), encodableValue: currentProperty))
}
}
/// Determines the difference between the two dictionaries mapping strings to diffable objects at the KeyPaths given.
mutating func addDifferences(atKeyPath keyPath: KeyPath<T, [some RenderJSONDiffable & Equatable & Codable]?>, forKey codingKey: CodingKey)
{
let currentProperty = current[keyPath: keyPath]
let otherProperty = other[keyPath: keyPath]
if currentProperty == otherProperty {
return
}
if currentProperty.isSimilar(to: otherProperty) {
differences.append(contentsOf: currentProperty.difference(from: otherProperty, at: path + [codingKey]))
} else {
differences.append(.replace(pointer: JSONPointer(from: path + [codingKey]), encodableValue: currentProperty))
}
}
/// Determines the difference between the two dictionaries mapping strings to arrays of diffable objects at the KeyPaths given.
mutating func addDifferences(atKeyPath keyPath: KeyPath<T, [String: [some RenderJSONDiffable & Equatable & Encodable]]>, forKey codingKey: CodingKey)
{
let currentProperty = current[keyPath: keyPath]
let otherProperty = other[keyPath: keyPath]
if currentProperty == otherProperty {
return
}
if currentProperty.isSimilar(to: otherProperty) {
differences.append(contentsOf: currentProperty.arrayValueDifference(from: otherProperty, at: path + [codingKey]))
} else {
differences.append(.replace(pointer: JSONPointer(from: path + [codingKey]), encodableValue: currentProperty))
}
}
/// Unwraps and adds the difference between two optional RenderSections.
mutating func addDifferences(atKeyPath keyPath: KeyPath<T, RenderSection?>, forKey key: CodingKey) {
let currentProperty = current[keyPath: keyPath]
let otherProperty = other[keyPath: keyPath]
if let currentProperty, let otherProperty {
let anyCurrent = AnyRenderSection(currentProperty)
let anyOther = AnyRenderSection(otherProperty)
if anyCurrent.isSimilar(to: anyOther) {
differences.append(contentsOf: anyCurrent.difference(from: anyOther, at: path + [key]))
} else {
differences.append(.replace(pointer: JSONPointer(from: path + [key]), encodableValue: currentProperty))
}
} else if otherProperty != nil {
differences.append(.remove(pointer: JSONPointer(from: path + [key])))
} else if let currentProp = currentProperty {
differences.append(.add(pointer: JSONPointer(from: path + [key]), encodableValue: currentProp))
}
}
/// Determines the difference between the two diffable Arrays of RenderSections at the KeyPaths given.
mutating func addDifferences(atKeyPath keyPath: KeyPath<T, [RenderSection]>, forKey codingKey: CodingKey) {
let currentArray = current[keyPath: keyPath]
let otherArray = other[keyPath: keyPath]
let typeErasedCurrentArray = currentArray.map { section in
return AnyRenderSection(section)
}
let typeErasedOtherArray = otherArray.map { section in
return AnyRenderSection(section)
}
if typeErasedCurrentArray == typeErasedOtherArray {
return
}
if typeErasedCurrentArray.isSimilar(to: typeErasedOtherArray) {
differences.append(contentsOf: typeErasedCurrentArray.difference(from: typeErasedOtherArray, at: path + [codingKey]))
} else {
differences.append(.replace(pointer: JSONPointer(from: path + [codingKey]), encodableValue: typeErasedCurrentArray))
}
}
/// Determines the difference between the two dictionaries of RenderReferences at the KeyPaths given.
mutating func addDifferences(atKeyPath keyPath: KeyPath<T, [String: RenderReference]>, forKey codingKey: CodingKey) {
let currentDict = current[keyPath: keyPath].mapValues { section in
return AnyRenderReference(section)
}
let otherDict = other[keyPath: keyPath].mapValues { section in
return AnyRenderReference(section)
}
if currentDict == otherDict {
return
}
differences.append(contentsOf: currentDict.difference(from: otherDict, at: path + [codingKey]))
}
}
|