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 187 188 189 190 191 192 193 194 195 196
|
/*
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 list of JSON patches generated as differences between two documents.
public typealias JSONPatchDifferences = [JSONPatchOperation]
typealias CodablePath = [CodingKey]
/// A protocol that defines Diffable conformance for properties and structs in RenderNode JSON.
protocol RenderJSONDiffable {
/// Returns the list of JSON patches (differences) to transform _other_ to _self_.
func difference(from other: Self, at path: CodablePath) -> JSONPatchDifferences
/// Returns if differences between _self_ and _other_ should be reported property-by-property or as a complete replacement.
func isSimilar(to other: Self) -> Bool
}
// If not otherwise defined, difference(from:at:) methods will assume Diffable objects are not similar,
// and should therefore be replaced.
extension RenderJSONDiffable where Self: Equatable {
func isSimilar(to other: Self) -> Bool {
return self == other
}
}
extension Array: RenderJSONDiffable where Element: Equatable & Encodable {
/// Returns the differences between this array and the given one.
func difference(from other: [Element], at path: CodablePath) -> JSONPatchDifferences {
let arrayDiffs = self.difference(from: other)
var differences = arrayDiffs.removals
differences.append(contentsOf: arrayDiffs.insertions)
let patchOperations = differences.map { diff -> JSONPatchOperation in
switch diff {
case .remove(let offset, _, _):
let pointer = JSONPointer(from: path + [JSON.IntegerKey(offset)])
return .remove(pointer: pointer)
case .insert(let offset, let element, _):
let pointer = JSONPointer(from: path + [JSON.IntegerKey(offset)])
return .add(pointer: pointer, encodableValue: element)
}
}
return patchOperations
}
/// Returns the differences between two arrays with diffable values.
func difference(
from other: [Element],
at path: CodablePath
) -> JSONPatchDifferences where Element: RenderJSONDiffable {
// This implementation computes both the insertions and deletions of significantly different elements
// and the per-property differences between elements that are similar to one another.
// First, compute the insertions and deletions based on elements that are _similar_.
let differences = self.difference(from: other) { element1, element2 in
return element1.isSimilar(to: element2)
}
var patchOperations = differences.map { diff -> JSONPatchOperation in
switch diff {
case .remove(let offset, _, _):
let pointer = JSONPointer(from: path + [JSON.IntegerKey(offset)])
return .remove(pointer: pointer)
case .insert(let offset, let element, _):
let pointer = JSONPointer(from: path + [JSON.IntegerKey(offset)])
return .add(pointer: pointer, encodableValue: element)
}
}
// Second, apply the insertions and deletions of significantly different elements so that the
// elements that are _similar_ are aligned (have the same index in both arrays).
let similarOther = other.applying(differences)!
// Finally, iterate over the similar — but not equal — elements to compute their per-property differences.
for (index, value) in enumerated() where similarOther[index] != value {
patchOperations.append(contentsOf: value.difference(from: similarOther[index],
at: path + [JSON.IntegerKey(index)]))
}
return patchOperations
}
// For now, whole arrays should not be replaced.
func isSimilar(to other: [Element]) -> Bool {
return true
}
}
extension Dictionary: RenderJSONDiffable where Key == String, Value: Encodable & Equatable {
/// Returns the differences between this dictionary and the given one.
func difference(from other: [Key: Value], at path: CodablePath) -> JSONPatchDifferences {
var differences = JSONPatchDifferences()
let uniqueKeysSet = Set(self.keys).union(Set(other.keys))
for key in uniqueKeysSet {
if self[key] == nil {
differences.append(.remove(
pointer: JSONPointer(from: path + [JSON.IntegerKey(key)])))
}
else if self[key] != other[key] {
differences.append(.replace(
pointer: JSONPointer(from: path + [JSON.IntegerKey(key)]),
encodableValue: self[key]))
}
}
return differences
}
/// Returns the differences between two dictionaries with diffable values.
func difference(
from other: [Key: Value],
at path: CodablePath
) -> JSONPatchDifferences where Value: RenderJSONDiffable {
var differences = JSONPatchDifferences()
let uniqueKeysSet = Set(self.keys).union(Set(other.keys))
for key in uniqueKeysSet {
differences.append(contentsOf: self[key].difference(from: other[key], at: path + [JSON.IntegerKey(key)]))
}
return differences
}
/// Returns the differences between two dictionaries of arrays with diffable values.
func arrayValueDifference<Element>(
from other: [Key: [Element]],
at path: CodablePath
) -> JSONPatchDifferences where Element: RenderJSONDiffable & Equatable & Encodable {
var differences = JSONPatchDifferences()
let uniqueKeysSet = Set(self.keys).union(Set(other.keys))
for key in uniqueKeysSet {
differences.append(contentsOf: (self[key] as! [Element]?).difference(from: other[key],
at: path + [JSON.IntegerKey(key)]))
}
return differences
}
// For now, we are not replacing whole dictionaries.
func isSimilar(to other: [String: Value]) -> Bool {
return true
}
}
extension Optional: RenderJSONDiffable where Wrapped: RenderJSONDiffable & Equatable & Encodable {
/// Returns the differences between this optional and the given one.
@_disfavoredOverload func difference(from other: Wrapped?, at path: CodablePath) -> JSONPatchDifferences {
var difference = JSONPatchDifferences()
if let current = self, let other {
difference.append(contentsOf: current.difference(from: other, at: path))
} else if other != nil {
difference.append(JSONPatchOperation.remove(
pointer: JSONPointer(from: path)))
} else if let current = self {
difference.append(JSONPatchOperation.add(
pointer: JSONPointer(from: path), encodableValue: current))
}
return difference
}
/// Returns the differences between this array of optional elements and the given one.
func difference<Element>(
from other: [Element]?,
at path: CodablePath
) -> JSONPatchDifferences where Element : RenderJSONDiffable & Equatable & Encodable {
var difference = JSONPatchDifferences()
if let current = self, let other {
difference.append(contentsOf: (current as! [Element]).difference(from: other, at: path))
} else if other != nil {
difference.append(JSONPatchOperation.remove(
pointer: JSONPointer(from: path)))
} else if let current = self {
difference.append(JSONPatchOperation.add(
pointer: JSONPointer(from: path), encodableValue: current))
}
return difference
}
// Optionals should deal with replacements on their own.
func isSimilar(to other: Wrapped?) -> Bool {
return true
}
}
|