@@ -58,14 +58,15 @@ public func customDump<T, TargetStream>(
58
58
) -> T where TargetStream: TextOutputStream {
59
59
60
60
var idPerItem : [ ObjectIdentifier : UInt ] = [ : ]
61
- var occurencePerType : [ String : UInt ] = [ : ]
61
+ var occurrencePerType : [ String : UInt ] = [ : ]
62
62
var visitedItems : Set < ObjectIdentifier > = [ ]
63
63
64
64
func customDumpHelp< T, TargetStream> (
65
65
_ value: T ,
66
66
to target: inout TargetStream ,
67
67
name: String ? ,
68
68
indent: Int ,
69
+ isRoot: Bool ,
69
70
maxDepth: Int
70
71
) where TargetStream: TextOutputStream {
71
72
if T . self is AnyObject . Type , withUnsafeBytes ( of: value, { $0. allSatisfy { $0 == 0 } } ) {
@@ -89,7 +90,12 @@ public func customDump<T, TargetStream>(
89
90
var childOut = " "
90
91
let child = mirror. children. first!
91
92
customDumpHelp (
92
- child. value, to: & childOut, name: child. label, indent: 0 , maxDepth: maxDepth - 1
93
+ child. value,
94
+ to: & childOut,
95
+ name: child. label,
96
+ indent: 0 ,
97
+ isRoot: false ,
98
+ maxDepth: maxDepth - 1
93
99
)
94
100
if childOut. contains ( " \n " ) {
95
101
if maxDepth == 0 {
@@ -113,7 +119,13 @@ public func customDump<T, TargetStream>(
113
119
for (offset, var child) in children. enumerated ( ) {
114
120
transform ( & child, offset)
115
121
customDumpHelp (
116
- child. value, to: & out, name: child. label, indent: 2 , maxDepth: maxDepth - 1 )
122
+ child. value,
123
+ to: & out,
124
+ name: child. label,
125
+ indent: 2 ,
126
+ isRoot: false ,
127
+ maxDepth: maxDepth - 1
128
+ )
117
129
if offset != children. count - 1 {
118
130
out. write ( " , " )
119
131
}
@@ -132,16 +144,18 @@ public func customDump<T, TargetStream>(
132
144
out. write ( value. customDumpDescription)
133
145
134
146
case let ( value as CustomDumpRepresentable , _) :
135
- customDumpHelp ( value. customDumpValue, to: & out, name: nil , indent: 0 , maxDepth: maxDepth - 1 )
147
+ customDumpHelp (
148
+ value. customDumpValue, to: & out, name: nil , indent: 0 , isRoot: false , maxDepth: maxDepth - 1
149
+ )
136
150
137
151
case let ( value as AnyObject , . class? ) :
138
152
let item = ObjectIdentifier ( value)
139
- var occurence = occurencePerType [ typeName ( mirror. subjectType) , default: 0 ] {
140
- didSet { occurencePerType [ typeName ( mirror. subjectType) ] = occurence }
153
+ var occurrence = occurrencePerType [ typeName ( mirror. subjectType) , default: 0 ] {
154
+ didSet { occurrencePerType [ typeName ( mirror. subjectType) ] = occurrence }
141
155
}
142
156
143
157
var id : String {
144
- let id = idPerItem [ item, default: occurence ]
158
+ let id = idPerItem [ item, default: occurrence ]
145
159
idPerItem [ item] = id
146
160
147
161
return id > 1 ? " # \( id) " : " "
@@ -150,7 +164,7 @@ public func customDump<T, TargetStream>(
150
164
out. write ( " \( typeName ( mirror. subjectType) ) \( id) (↩︎) " )
151
165
} else {
152
166
visitedItems. insert ( item)
153
- occurence += 1
167
+ occurrence += 1
154
168
var children = Array ( mirror. children)
155
169
156
170
var superclassMirror = mirror. superclassMirror
@@ -192,7 +206,7 @@ public func customDump<T, TargetStream>(
192
206
}
193
207
194
208
case ( _, . enum? ) :
195
- out. write ( " \( typeName ( mirror. subjectType) ) . " )
209
+ out. write ( isRoot ? " \( typeName ( mirror. subjectType) ) . " : " . " )
196
210
if let child = mirror. children. first {
197
211
let childMirror = Mirror ( customDumpReflecting: child. value)
198
212
let associatedValuesMirror =
@@ -215,7 +229,7 @@ public func customDump<T, TargetStream>(
215
229
216
230
case ( _, . optional? ) :
217
231
if let value = mirror. children. first? . value {
218
- customDumpHelp ( value, to: & out, name: nil , indent: 0 , maxDepth: maxDepth)
232
+ customDumpHelp ( value, to: & out, name: nil , indent: 0 , isRoot : false , maxDepth: maxDepth)
219
233
} else {
220
234
out. write ( " nil " )
221
235
}
@@ -266,7 +280,7 @@ public func customDump<T, TargetStream>(
266
280
target. write ( ( name. map { " \( $0) : " } ?? " " ) . appending ( out) . indenting ( by: indent) )
267
281
}
268
282
269
- customDumpHelp ( value, to: & target, name: name, indent: indent, maxDepth: maxDepth)
283
+ customDumpHelp ( value, to: & target, name: name, indent: indent, isRoot : true , maxDepth: maxDepth)
270
284
return value
271
285
}
272
286
0 commit comments