@@ -150,7 +150,7 @@ public struct Lookup: Swift.CustomStringConvertible, Swift.CustomDebugStringConv
150
150
}
151
151
}
152
152
153
- // Resolve build warning:
153
+ // # Resolve build warning:
154
154
// heterogeneous collection literal could only be inferred to '[String : Any]'; add explicit type annotation if this is intentional
155
155
public init ( _ anyDictionary: [ String : Any ] ) {
156
156
self . init ( anyDictionary as Any )
@@ -201,7 +201,6 @@ public struct Lookup: Swift.CustomStringConvertible, Swift.CustomDebugStringConv
201
201
}
202
202
}
203
203
204
- // TODO: dynamicMember change
205
204
private mutating func setNewValue( for dynamicMember: String , value: Lookup ) {
206
205
switch rawType {
207
206
case . none:
@@ -343,6 +342,7 @@ extension Lookup: ExpressibleByBooleanLiteral {
343
342
// MARK: - Convert
344
343
public extension Lookup {
345
344
345
+ /// return true when it is invalid `key` or `value`
346
346
var isNone : Bool {
347
347
rawType == . none
348
348
}
@@ -352,6 +352,7 @@ public extension Lookup {
352
352
}
353
353
354
354
// MARK: - String
355
+ /// Convert value to `String`, available when rawType is in `[.number, .string]`
355
356
var string : String ? {
356
357
switch rawType {
357
358
case . number:
@@ -487,6 +488,7 @@ public extension Lookup {
487
488
}
488
489
489
490
// MARK: - Dict
491
+ /// Available when rawType is in `[.dict, .string]` (if use string, it **MUST** be jsonString)
490
492
var dict : [ String : Any ] ? {
491
493
switch rawType {
492
494
case . dict:
@@ -506,6 +508,7 @@ public extension Lookup {
506
508
dict!
507
509
}
508
510
511
+ /// Available when rawType is in `[.dict, .string]` (if use string, it **MUST** be jsonString)
509
512
var dictLookup : Lookup {
510
513
switch rawType {
511
514
case . dict:
@@ -524,6 +527,7 @@ public extension Lookup {
524
527
}
525
528
526
529
// MARK: - Array
530
+ /// Available when rawType is in `[.array, .string]` (if use string, it **MUST** be jsonString)
527
531
var array : [ Any ] ? {
528
532
switch rawType {
529
533
case . array:
@@ -543,6 +547,7 @@ public extension Lookup {
543
547
array!
544
548
}
545
549
550
+ /// Available when rawType is in `[.array, .string]` (if use string, it **MUST** be jsonString)
546
551
var arrayLookup : [ Lookup ] {
547
552
switch rawType {
548
553
case . array:
@@ -563,6 +568,61 @@ public extension Lookup {
563
568
var lookup : Lookup {
564
569
Lookup ( rawValue)
565
570
}
571
+
572
+ /// Available when rawType is in `[.array, .dict]`
573
+ var jsonData : Data ? {
574
+ switch rawType {
575
+ case . array:
576
+ return try ? JSONSerialization . data ( withJSONObject: rawArray)
577
+ case . dict:
578
+ return try ? JSONSerialization . data ( withJSONObject: rawDict)
579
+ default :
580
+ return nil
581
+ }
582
+ }
583
+
584
+ /// Available when rawType is in `[.array, .dict, .string]`
585
+ var isEmpty : Bool {
586
+ switch rawType {
587
+ case . array:
588
+ return rawArray. isEmpty
589
+ case . dict:
590
+ return rawDict. isEmpty
591
+ case . string:
592
+ return rawString. isEmpty
593
+ default :
594
+ return false
595
+ }
596
+ }
597
+
598
+ /// Available when rawType is in `[.array, .dict, .string]`
599
+ var count : Int {
600
+ switch rawType {
601
+ case . array:
602
+ return rawArray. count
603
+ case . dict:
604
+ return rawDict. count
605
+ case . string:
606
+ return rawString. count
607
+ default :
608
+ return 0
609
+ }
610
+ }
611
+ }
612
+
613
+ // MARK: Decode
614
+ extension Lookup {
615
+
616
+ public enum DecodeError : Swift . Error {
617
+ case invalidJSONData
618
+ }
619
+
620
+ public func decode< D> ( as decodable: D . Type , using decoder: JSONDecoder = JSONDecoder ( ) ) throws -> D where D: Decodable {
621
+ guard let jsonData else {
622
+ throw DecodeError . invalidJSONData
623
+ }
624
+ return try decoder. decode ( D . self, from: jsonData)
625
+ }
566
626
}
567
627
568
628
0 commit comments