@@ -177,23 +177,32 @@ public extension Plugins.Log {
177177 /// The `curl` component of the log.
178178 struct Component : Hashable , RawRepresentable , ExpressibleByStringLiteral , CustomDebugStringConvertible , SmartSendable {
179179 public let rawValue : String
180+ /// The value is indicating the index the ``Component`` will iterated.
181+ public let sortingOrder : Int
180182
181183 public init ( rawValue: String ) {
182184 self . rawValue = rawValue
185+ self . sortingOrder = - 1
183186 }
184187
185188 public init ( stringLiteral value: String ) {
186189 self . rawValue = value
190+ self . sortingOrder = - 1
187191 }
188192
193+ init ( rawValue: String , sortingOrder: Int ) {
194+ self . rawValue = rawValue
195+ self . sortingOrder = sortingOrder
196+ }
197+
198+ public static let phase : Self = . init( rawValue: " phase " , sortingOrder: 0 )
199+ public static let url : Self = . init( rawValue: " url " , sortingOrder: 10 )
200+ public static let curl : Self = . init( rawValue: " curl " , sortingOrder: 20 )
201+ public static let error : Self = . init( rawValue: " error " , sortingOrder: 30 )
202+ public static let requestError : Self = . init( rawValue: " requestError " , sortingOrder: 40 )
203+ public static let body : Self = . init( rawValue: " body " , sortingOrder: 50 )
189204 public static let id : Self = " id "
190- public static let url : Self = " url "
191- public static let phase : Self = " phase "
192- public static let curl : Self = " curl "
193205 public static let headers : Self = " headers "
194- public static let error : Self = " error "
195- public static let requestError : Self = " requestError "
196- public static let body : Self = " body "
197206 public static let userInfo : Self = " userInfo "
198207 public static let parameters : Self = " parameters "
199208 public static let request : Self = " request "
@@ -246,28 +255,28 @@ public extension Plugins.Log {
246255 public static let all : Self = [ . willSend, . didFinish, . didReceive, . wasCancelled]
247256 }
248257
249- struct DataCollection : Sequence {
250- public typealias Getter = ( ) -> Any
258+ struct DataCollection : Sequence , CustomDebugStringConvertible {
259+ public typealias Getter < T > = ( ) -> T
251260
252- public let data : [ Component : Getter ]
261+ public let data : [ Component : Getter < Any > ]
253262
254- public init ( data: [ Component : Getter ] = [ : ] ) {
263+ public init ( data: [ Component : Getter < Any > ] = [ : ] ) {
255264 self . data = data
256265 }
257266
258- public func add( _ key: Component , _ value: @autoclosure @escaping Getter ) -> Self {
267+ public func add( _ key: Component , _ value: @autoclosure @escaping Getter < Any > ) -> Self {
259268 var data = data
260269 data [ key] = value
261270 return . init( data: data)
262271 }
263272
264- public func add( _ key: Component , _ value: @escaping Getter ) -> Self {
273+ public func add( _ key: Component , _ value: @escaping Getter < Any > ) -> Self {
265274 var data = data
266275 data [ key] = value
267276 return . init( data: data)
268277 }
269278
270- public func add( _ key: Component , if condition: Bool , _ value: @escaping Getter ) -> Self {
279+ public func add( _ key: Component , if condition: Bool , _ value: @escaping Getter < Any > ) -> Self {
271280 guard condition else {
272281 return self
273282 }
@@ -277,51 +286,75 @@ public extension Plugins.Log {
277286 return . init( data: data)
278287 }
279288
280- public func getClosure( safe key: Component ) -> ( ( ) -> Any ) ? {
289+ public func getClosure( safe key: Component ) -> Getter < Any > ? {
281290 return data [ key]
282291 }
283292
284- public func getClosure( _ key: Component ) -> ( ) -> Any {
293+ public func getClosure( _ key: Component ) -> Getter < Any > {
285294 return data [ key] !
286295 }
287296
288- public func getAny( safe key: Component ) -> Any ? {
297+ public func getClosure< T> ( safe key: Component , ofType: T . Type ) -> Getter < T > ? {
298+ if let value = data [ key] ? ( ) as? T {
299+ return { [ value] in
300+ return value
301+ }
302+ }
303+ return nil
304+ }
305+
306+ public func getClosure< T> ( _ key: Component , ofType: T . Type ) -> Getter < T > {
307+ return { [ data] in
308+ return data [ key] !( ) as! T
309+ }
310+ }
311+
312+ public func get( safe key: Component ) -> Any ? {
289313 return data [ key] ? ( )
290314 }
291315
292- public func getAny ( _ key: Component ) -> Any {
316+ public func get ( _ key: Component ) -> Any {
293317 return data [ key] !( )
294318 }
295319
296- public func get< T> ( safe key: Component , ofType: T . Type = T . self ) -> T ? {
320+ public func get< T> ( safe key: Component , ofType: T . Type ) -> T ? {
297321 return data [ key] ? ( ) as? T
298322 }
299323
300- public func get< T> ( _ key: Component , ofType: T . Type = T . self ) -> T {
324+ public func get< T> ( _ key: Component , ofType: T . Type ) -> T {
301325 return data [ key] ? ( ) as! T
302326 }
303327
304- public subscript< T> ( safe key: Component ) -> T ? {
305- return get ( safe: key)
328+ public subscript< T> ( safe key: Component , ofType type : T . Type ) -> T ? {
329+ return get ( safe: key, ofType : type )
306330 }
307331
308- public subscript< T> ( _ key: Component ) -> T {
309- return get ( key)
332+ public subscript< T> ( _ key: Component , ofType type : T . Type ) -> T {
333+ return get ( key, ofType : type )
310334 }
311335
312336 public func makeIterator( ) -> Iterator {
313337 return . init( data: self )
314338 }
315339
316340 public struct Iterator : IteratorProtocol {
317- public typealias Element = ( key: Component , value: Getter )
341+ public typealias Element = ( key: Component , value: Getter < Any > )
318342
319343 private let data : [ Element ]
320344 private var index : Int
321345
322346 init ( data: DataCollection ) {
323347 self . data = data. data. sorted ( by: { a, b in
324- return a. key. rawValue < b. key. rawValue
348+ switch ( a. key. sortingOrder, b. key. sortingOrder) {
349+ case ( - 1 , - 1 ) :
350+ return a. key. rawValue < b. key. rawValue
351+ case ( - 1 , _) :
352+ return false
353+ case ( _, - 1 ) :
354+ return true
355+ default :
356+ return a. key. sortingOrder < b. key. sortingOrder
357+ }
325358 } )
326359 self . index = 0
327360 }
@@ -336,5 +369,11 @@ public extension Plugins.Log {
336369 }
337370 }
338371 }
372+
373+ public var debugDescription : String {
374+ return data. mapValues {
375+ return $0 ( )
376+ } . debugDescription
377+ }
339378 }
340379}
0 commit comments