Skip to content

Commit 04c6bc5

Browse files
authored
Merge pull request #15 from iWECon/dev
dev
2 parents 5103714 + 02d0898 commit 04c6bc5

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

Sources/Lookup/Lookup.swift

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public struct Lookup: Swift.CustomStringConvertible, Swift.CustomDebugStringConv
150150
}
151151
}
152152

153-
// Resolve build warning:
153+
// # Resolve build warning:
154154
// heterogeneous collection literal could only be inferred to '[String : Any]'; add explicit type annotation if this is intentional
155155
public init(_ anyDictionary: [String: Any]) {
156156
self.init(anyDictionary as Any)
@@ -201,7 +201,6 @@ public struct Lookup: Swift.CustomStringConvertible, Swift.CustomDebugStringConv
201201
}
202202
}
203203

204-
// TODO: dynamicMember change
205204
private mutating func setNewValue(for dynamicMember: String, value: Lookup) {
206205
switch rawType {
207206
case .none:
@@ -343,6 +342,7 @@ extension Lookup: ExpressibleByBooleanLiteral {
343342
// MARK: - Convert
344343
public extension Lookup {
345344

345+
/// return true when it is invalid `key` or `value`
346346
var isNone: Bool {
347347
rawType == .none
348348
}
@@ -352,6 +352,7 @@ public extension Lookup {
352352
}
353353

354354
// MARK: - String
355+
/// Convert value to `String`, available when rawType is in `[.number, .string]`
355356
var string: String? {
356357
switch rawType {
357358
case .number:
@@ -487,6 +488,7 @@ public extension Lookup {
487488
}
488489

489490
// MARK: - Dict
491+
/// Available when rawType is in `[.dict, .string]` (if use string, it **MUST** be jsonString)
490492
var dict: [String: Any]? {
491493
switch rawType {
492494
case .dict:
@@ -506,6 +508,7 @@ public extension Lookup {
506508
dict!
507509
}
508510

511+
/// Available when rawType is in `[.dict, .string]` (if use string, it **MUST** be jsonString)
509512
var dictLookup: Lookup {
510513
switch rawType {
511514
case .dict:
@@ -524,6 +527,7 @@ public extension Lookup {
524527
}
525528

526529
// MARK: - Array
530+
/// Available when rawType is in `[.array, .string]` (if use string, it **MUST** be jsonString)
527531
var array: [Any]? {
528532
switch rawType {
529533
case .array:
@@ -543,6 +547,7 @@ public extension Lookup {
543547
array!
544548
}
545549

550+
/// Available when rawType is in `[.array, .string]` (if use string, it **MUST** be jsonString)
546551
var arrayLookup: [Lookup] {
547552
switch rawType {
548553
case .array:
@@ -563,6 +568,61 @@ public extension Lookup {
563568
var lookup: Lookup {
564569
Lookup(rawValue)
565570
}
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+
}
566626
}
567627

568628

0 commit comments

Comments
 (0)