1
1
import Foundation
2
2
3
- // MARK: - extension Array Helper merging multi lookup into one lookup
4
- public extension Array where Element == Lookup {
5
-
6
- /// Merging multi `rawDict` into one
7
- /// ⚠️ Only support `Dictionary`
8
- ///
9
- /// - Parameter uniquingKeysWith: uniquing keys with conflict
10
- /// - Returns: Merged `Lookup`
11
- func merging( uniquingKeysWith: ( Any , Any ) -> Any ) -> Lookup {
12
- let dictLookups = self . compactMap ( { $0. dict } )
13
- var temp : [ String : Any ] = [ : ]
14
- for value in dictLookups {
15
- temp. merge ( value, uniquingKeysWith: uniquingKeysWith)
16
- }
17
- return Lookup ( temp)
18
- }
19
- }
20
-
21
3
extension Array {
22
4
var countIndex : Int {
23
5
count - 1
@@ -34,7 +16,7 @@ fileprivate extension String {
34
16
}
35
17
36
18
// MARK: Unwrap
37
- fileprivate func unwrap( _ object: Any ) -> Any {
19
+ fileprivate func unwrap( _ object: Any ? ) -> Any {
38
20
switch object {
39
21
case let lookup as Lookup :
40
22
return unwrap ( lookup. object)
@@ -58,7 +40,7 @@ fileprivate func unwrap(_ object: Any) -> Any {
58
40
return array. map ( unwrap)
59
41
60
42
default :
61
- return object
43
+ return object ?? NSNull ( )
62
44
}
63
45
}
64
46
@@ -286,6 +268,17 @@ public struct Lookup: Swift.CustomStringConvertible, Swift.CustomDebugStringConv
286
268
public var debugDescription : String { description }
287
269
288
270
fileprivate static var null : Lookup { Lookup ( NSNull ( ) ) }
271
+
272
+ fileprivate mutating func merge( other: Lookup ) {
273
+ switch ( self . rawType, other. rawType) {
274
+ case ( . dict, . dict) :
275
+ self . rawDict. merge ( other. rawDict, uniquingKeysWith: { $1 } )
276
+ case ( . array, . array) :
277
+ self . rawArray += other. rawArray
278
+ default :
279
+ break
280
+ }
281
+ }
289
282
}
290
283
291
284
extension Lookup : ExpressibleByArrayLiteral {
@@ -529,6 +522,24 @@ public extension Lookup {
529
522
}
530
523
}
531
524
525
+
526
+ // MARK: - Operator
527
+ public func + ( lhs: Lookup , rhs: Lookup ) -> Lookup {
528
+ switch ( lhs. rawType, rhs. rawType) {
529
+ case ( . dict, . dict) :
530
+ let lhsRawDict = lhs. rawDict
531
+ return Lookup ( lhsRawDict. merging ( rhs. rawDict, uniquingKeysWith: { $1 } ) )
532
+ case ( . array, . array) :
533
+ return Lookup ( lhs. rawArray + rhs. rawArray)
534
+ default :
535
+ return . null
536
+ }
537
+ }
538
+
539
+ public func += ( lhs: inout Lookup , rhs: Lookup ) {
540
+ lhs. merge ( other: rhs)
541
+ }
542
+
532
543
// MARK: - Codable
533
544
extension Lookup : Codable {
534
545
0 commit comments