Skip to content

Commit 98b9dce

Browse files
committed
[PlaygroundLogger] Unwrap optionality when generating log entries.
The original implementation of PlaygroundLogger treated Optional.some as effectively not existing; it would always unwrap the optionality and present the payload as if it were top-level. This doesn't quite do the same thing (the type name will be Optional<T>, not T) but it preserves the main behavior. This fixes testOptionalGetsStripped().
1 parent 10dc22d commit 98b9dce

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

PlaygroundLogger/PlaygroundLogger/LogEntry+Reflection.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,18 @@ extension LogEntry {
6262
self = .opaque(name: name, typeName: typeName, summary: summary, preferBriefSummary: false, representation: cgImage.opaqueRepresentation)
6363
default:
6464
// This isn't one of the CF types we want to specially handle, so the log entry should just reflect the instance's structure.
65-
self = .init(structureOf: instance, name: name, typeName: typeName, summary: summary)
65+
66+
// Get a Mirror which reflects the instance being logged.
67+
let mirror = Mirror(reflecting: instance)
68+
69+
if mirror.displayStyle == .optional && mirror.children.count == 1 {
70+
// If the mirror displays as an Optional and has exactly one child, then we want to unwrap the optionality and generate a log entry for the child.
71+
self = .init(describing: mirror.children.first!.value, name: name, typeName: typeName, summary: nil)
72+
}
73+
else {
74+
// Otherwise, we want to generate a log entry with the structure from the mirror.
75+
self = .init(structureFrom: mirror, name: name, typeName: typeName, summary: summary)
76+
}
6677
}
6778
}
6879
}
@@ -72,9 +83,7 @@ extension LogEntry {
7283
self = .opaque(name: name, typeName: typeName, summary: summary, preferBriefSummary: false, representation: playgroundQuickLook.opaqueRepresentation)
7384
}
7485

75-
private init(structureOf instance: Any, name: String, typeName: String, summary: String) {
76-
let mirror = Mirror(reflecting: instance)
77-
86+
private init(structureFrom mirror: Mirror, name: String, typeName: String, summary: String) {
7887
self = .structured(name: name, typeName: typeName, summary: summary, totalChildrenCount: Int(mirror.children.count), children: mirror.children.map { LogEntry(describing: $0.value, name: $0.label) }, disposition: .init(displayStyle: mirror.displayStyle))
7988
}
8089
}

0 commit comments

Comments
 (0)