Skip to content

Commit 21e88a0

Browse files
committed
Recursive diff: cleanup comments, renaming, reordering
1 parent 29009dc commit 21e88a0

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Sources/LoggerMiddleware/LoggerMiddleware.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,8 @@ extension LoggerMiddleware {
165165
return nil
166166
}
167167

168-
let leftMirror = Mirror(reflecting: leftHandSide)
169-
let rightMirror = Mirror(reflecting: rightHandSide)
170-
171-
// special handling for Dictionaries
172-
if let left = lhs as? Dictionary<AnyHashable, Any>, let right = rhs as? Dictionary<AnyHashable, Any> {
168+
// special handling for Dictionaries: stringify and order the keys before comparing
169+
if let left = leftHandSide as? Dictionary<AnyHashable, Any>, let right = rightHandSide as? Dictionary<AnyHashable, Any> {
173170

174171
let leftSorted = left.sorted { a, b in "\(a.key)" < "\(b.key)" }
175172
let rightSorted = right.sorted { a, b in "\(a.key)" < "\(b.key)" }
@@ -186,7 +183,7 @@ extension LoggerMiddleware {
186183
}
187184

188185
// special handling for sets as well: order the contents, compare as strings
189-
if let left = lhs as? Set<AnyHashable>, let right = rhs as? Set<AnyHashable> {
186+
if let left = leftHandSide as? Set<AnyHashable>, let right = rightHandSide as? Set<AnyHashable> {
190187
let leftSorted = left.map { "\($0)" }.sorted { a, b in a < b }
191188
let rightSorted = right.map { "\($0)" }.sorted { a, b in a < b }
192189

@@ -200,7 +197,10 @@ extension LoggerMiddleware {
200197
return "\(prefix).\(name): 📦 <\(leftPrintable)> → <\(rightPrintable)>"
201198
}
202199

203-
// if there are no children, compare lhs and rhs directly
200+
let leftMirror = Mirror(reflecting: leftHandSide)
201+
let rightMirror = Mirror(reflecting: rightHandSide)
202+
203+
// if there are no children, compare leftHandSide and rightHandSide directly
204204
if 0 == leftMirror.children.count {
205205
if "\(leftHandSide)" == "\(rightHandSide)" {
206206
return nil
@@ -213,7 +213,7 @@ extension LoggerMiddleware {
213213
let strings: [String] = leftMirror.children.map({ leftChild in
214214
let toDotOrNotToDot = (level > 0) ? "." : " "
215215
return Self.diff(prefix: "\(prefix)\(toDotOrNotToDot)\(name)",
216-
name: leftChild.label ?? "#", // label might be missing for items in collections
216+
name: leftChild.label ?? "#", // label might be missing for items in collections, # represents a collection element
217217
level: level + 1,
218218
lhs: leftChild.value,
219219
rhs: rightMirror.children.first(where: { $0.label == leftChild.label })?.value)

0 commit comments

Comments
 (0)