Skip to content

Commit b803caa

Browse files
authored
Filter properties prefixed with _$ (#100)
1 parent af3903b commit b803caa

File tree

4 files changed

+81
-15
lines changed

4 files changed

+81
-15
lines changed

Sources/CustomDump/Diff.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
8585
elementIndent: Int,
8686
elementSeparator: String,
8787
collapseUnchanged: Bool,
88+
filter isIncluded: (Mirror.Child) -> Bool = { _ in true },
8889
areEquivalent: (Mirror.Child, Mirror.Child) -> Bool = { $0.label == $1.label },
8990
areInIncreasingOrder: ((Mirror.Child, Mirror.Child) -> Bool)? = nil,
90-
_ transform: (inout Mirror.Child, Int) -> Void = { _, _ in }
91+
map transform: (inout Mirror.Child, Int) -> Void = { _, _ in }
9192
) {
9293
var lhsChildren = Array(lhsMirror.children)
9394
var rhsChildren = Array(rhsMirror.children)
@@ -156,6 +157,9 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
156157
return
157158
}
158159

160+
lhsChildren.removeAll(where: { !isIncluded($0) })
161+
rhsChildren.removeAll(where: { !isIncluded($0) })
162+
159163
let name = rhsName.map { "\($0): " } ?? ""
160164
print(
161165
name
@@ -355,7 +359,8 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
355359
suffix: ")",
356360
elementIndent: 2,
357361
elementSeparator: ",",
358-
collapseUnchanged: false
362+
collapseUnchanged: false,
363+
filter: { $0.label.map { !$0.hasPrefix("_$") } ?? true }
359364
)
360365
}
361366

@@ -371,7 +376,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
371376
areEquivalent: {
372377
isIdentityEqual($0.value, $1.value) || isMirrorEqual($0.value, $1.value)
373378
},
374-
{ $0.label = "[\($1)]" }
379+
map: { $0.label = "[\($1)]" }
375380
)
376381

377382
case (_, .dictionary?, _, .dictionary?):
@@ -443,7 +448,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
443448
elementIndent: 2,
444449
elementSeparator: ",",
445450
collapseUnchanged: false,
446-
{ child, _ in
451+
map: { child, _ in
447452
if child.label?.first == "." {
448453
child.label = nil
449454
}
@@ -499,7 +504,8 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
499504
suffix: ")",
500505
elementIndent: 2,
501506
elementSeparator: ",",
502-
collapseUnchanged: false
507+
collapseUnchanged: false,
508+
filter: { $0.label.map { !$0.hasPrefix("_$") } ?? true }
503509
)
504510

505511
case (_, .tuple?, _, .tuple?):
@@ -511,7 +517,7 @@ public func diff<T>(_ lhs: T, _ rhs: T, format: DiffFormat = .default) -> String
511517
elementIndent: 2,
512518
elementSeparator: ",",
513519
collapseUnchanged: false,
514-
{ child, _ in
520+
map: { child, _ in
515521
if child.label?.first == "." {
516522
child.label = nil
517523
}

Sources/CustomDump/Dump.swift

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ func _customDump<T, TargetStream>(
101101
of mirror: Mirror,
102102
prefix: String,
103103
suffix: String,
104+
filter isIncluded: (Mirror.Child) -> Bool = { _ in true },
104105
by areInIncreasingOrder: ((Mirror.Child, Mirror.Child) -> Bool)? = nil,
105-
_ transform: (inout Mirror.Child, Int) -> Void = { _, _ in }
106+
map transform: (inout Mirror.Child, Int) -> Void = { _, _ in }
106107
) {
107108
out.write(prefix)
108109
if !mirror.children.isEmpty {
@@ -133,6 +134,7 @@ func _customDump<T, TargetStream>(
133134
} else {
134135
out.write("\n")
135136
var children = Array(mirror.children)
137+
children.removeAll(where: { !isIncluded($0) })
136138
if let areInIncreasingOrder = areInIncreasingOrder {
137139
children.sort(by: areInIncreasingOrder)
138140
}
@@ -195,12 +197,13 @@ func _customDump<T, TargetStream>(
195197
dumpChildren(
196198
of: Mirror(value, children: children),
197199
prefix: "\(typeName(mirror.subjectType))\(id)(",
198-
suffix: ")"
200+
suffix: ")",
201+
filter: { $0.label.map { !$0.hasPrefix("_$") } ?? true }
199202
)
200203
}
201204

202205
case (_, .collection?):
203-
dumpChildren(of: mirror, prefix: "[", suffix: "]", { $0.label = "[\($1)]" })
206+
dumpChildren(of: mirror, prefix: "[", suffix: "]", map: { $0.label = "[\($1)]" })
204207

205208
case (_, .dictionary?):
206209
if mirror.children.isEmpty {
@@ -220,13 +223,14 @@ func _customDump<T, TargetStream>(
220223
< _customDump(rhsKey.base, name: nil, indent: 0, isRoot: false, maxDepth: 1)
221224
}
222225
: nil,
223-
{ child, _ in
226+
map: { child, _ in
224227
guard let pair = child.value as? (key: AnyHashable, value: Any) else { return }
225228
let key = _customDump(
226229
pair.key.base, name: nil, indent: 0, isRoot: false, maxDepth: maxDepth - 1
227230
)
228231
child = (key, pair.value)
229-
})
232+
}
233+
)
230234
}
231235

232236
case (_, .enum?):
@@ -241,7 +245,7 @@ func _customDump<T, TargetStream>(
241245
of: associatedValuesMirror,
242246
prefix: "\(child.label ?? "@unknown")(",
243247
suffix: ")",
244-
{ child, _ in
248+
map: { child, _ in
245249
if child.label?.first == "." {
246250
child.label = nil
247251
}
@@ -271,18 +275,24 @@ func _customDump<T, TargetStream>(
271275
)
272276

273277
case (_, .struct?):
274-
dumpChildren(of: mirror, prefix: "\(typeName(mirror.subjectType))(", suffix: ")")
278+
dumpChildren(
279+
of: mirror,
280+
prefix: "\(typeName(mirror.subjectType))(",
281+
suffix: ")",
282+
filter: { $0.label.map { !$0.hasPrefix("_$") } ?? true }
283+
)
275284

276285
case (_, .tuple?):
277286
dumpChildren(
278287
of: mirror,
279288
prefix: "(",
280289
suffix: ")",
281-
{ child, _ in
290+
map: { child, _ in
282291
if child.label?.first == "." {
283292
child.label = nil
284293
}
285-
})
294+
}
295+
)
286296

287297
default:
288298
if let value = stringFromStringProtocol(value) {

Tests/CustomDumpTests/DiffTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,28 @@ final class DiffTests: XCTestCase {
11591159
"""
11601160
)
11611161
}
1162+
1163+
func testObservationRegistrarFiltered() {
1164+
struct ObservationRegistrar: Equatable {}
1165+
struct Value: Equatable {
1166+
var name: String
1167+
let _$observationRegistrar = ObservationRegistrar()
1168+
}
1169+
let blobSr = Value(name: "Blob Sr.")
1170+
let blobJr = Value(name: "Blob Jr.")
1171+
XCTAssertNoDifference(
1172+
diff(
1173+
blobSr,
1174+
blobJr
1175+
),
1176+
"""
1177+
DiffTests.Value(
1178+
- name: "Blob Sr."
1179+
+ name: "Blob Jr."
1180+
)
1181+
"""
1182+
)
1183+
}
11621184
}
11631185

11641186
private struct Stack<State: Equatable>: CustomDumpReflectable, Equatable {

Tests/CustomDumpTests/DumpTests.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,4 +1301,32 @@ final class DumpTests: XCTestCase {
13011301
)
13021302
}
13031303
#endif
1304+
1305+
func testObservationRegistrarFiltered() {
1306+
struct ObservationRegistrar {}
1307+
class Object {
1308+
let name = "Blob Sr."
1309+
let _$observationRegistrar = ObservationRegistrar()
1310+
}
1311+
XCTAssertNoDifference(
1312+
String(customDumping: Object()),
1313+
"""
1314+
DumpTests.Object(
1315+
name: "Blob Sr."
1316+
)
1317+
"""
1318+
)
1319+
struct Value {
1320+
let name = "Blob Jr."
1321+
let _$observationRegistrar = ObservationRegistrar()
1322+
}
1323+
XCTAssertNoDifference(
1324+
String(customDumping: Value()),
1325+
"""
1326+
DumpTests.Value(
1327+
name: "Blob Jr."
1328+
)
1329+
"""
1330+
)
1331+
}
13041332
}

0 commit comments

Comments
 (0)