From 92990382ad9eeb377a342957b8d8415440cf2a98 Mon Sep 17 00:00:00 2001 From: iWw Date: Mon, 11 Sep 2023 18:50:33 +0800 Subject: [PATCH 1/3] Add jsonData property --- Sources/Lookup/Lookup.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Sources/Lookup/Lookup.swift b/Sources/Lookup/Lookup.swift index b93e7bf..97e2cc6 100644 --- a/Sources/Lookup/Lookup.swift +++ b/Sources/Lookup/Lookup.swift @@ -560,6 +560,18 @@ public extension Lookup { var lookup: Lookup { Lookup(rawValue) } + + /// Available on `array` and `dict` + var jsonData: Data? { + switch rawType { + case .array: + return try? JSONSerialization.data(withJSONObject: rawArray) + case .dict: + return try? JSONSerialization.data(withJSONObject: rawDict) + default: + return nil + } + } } From 720ccff6e846189cfad0afb81dced559582bb8d1 Mon Sep 17 00:00:00 2001 From: iWw Date: Mon, 11 Sep 2023 19:03:07 +0800 Subject: [PATCH 2/3] Add decodeAs function --- Sources/Lookup/Lookup.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sources/Lookup/Lookup.swift b/Sources/Lookup/Lookup.swift index 97e2cc6..56a57db 100644 --- a/Sources/Lookup/Lookup.swift +++ b/Sources/Lookup/Lookup.swift @@ -574,6 +574,21 @@ public extension Lookup { } } +// MARK: Decode +extension Lookup { + + public enum DecodeError: Swift.Error { + case invalidJSONData + } + + public func decode(as decodable: D.Type, using decoder: JSONDecoder = JSONDecoder()) throws -> D where D: Decodable { + guard let jsonData else { + throw DecodeError.invalidJSONData + } + return try decoder.decode(D.self, from: jsonData) + } +} + // MARK: - Operator public func + (lhs: Lookup, rhs: Lookup) -> Lookup { From 02d0898bc812fa53632859532d88d313b3dd484d Mon Sep 17 00:00:00 2001 From: iWw Date: Tue, 12 Sep 2023 10:33:39 +0800 Subject: [PATCH 3/3] Update --- Sources/Lookup/Lookup.swift | 39 ++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/Sources/Lookup/Lookup.swift b/Sources/Lookup/Lookup.swift index 56a57db..bdb5f83 100644 --- a/Sources/Lookup/Lookup.swift +++ b/Sources/Lookup/Lookup.swift @@ -137,7 +137,7 @@ public struct Lookup: Swift.CustomStringConvertible, Swift.CustomDebugStringConv } } - // Resolve build warning: + // # Resolve build warning: // heterogeneous collection literal could only be inferred to '[String : Any]'; add explicit type annotation if this is intentional public init(_ anyDictionary: [String: Any]) { self.init(anyDictionary as Any) @@ -188,7 +188,6 @@ public struct Lookup: Swift.CustomStringConvertible, Swift.CustomDebugStringConv } } - // TODO: dynamicMember change private mutating func setNewValue(for dynamicMember: String, value: Lookup) { switch rawType { case .none: @@ -340,6 +339,7 @@ extension Lookup: ExpressibleByBooleanLiteral { // MARK: - Convert public extension Lookup { + /// return true when it is invalid `key` or `value` var isNone: Bool { rawType == .none } @@ -349,6 +349,7 @@ public extension Lookup { } // MARK: - String + /// Convert value to `String`, available when rawType is in `[.number, .string]` var string: String? { switch rawType { case .number: @@ -484,6 +485,7 @@ public extension Lookup { } // MARK: - Dict + /// Available when rawType is in `[.dict, .string]` (if use string, it **MUST** be jsonString) var dict: [String: Any]? { switch rawType { case .dict: @@ -503,6 +505,7 @@ public extension Lookup { dict! } + /// Available when rawType is in `[.dict, .string]` (if use string, it **MUST** be jsonString) var dictLookup: Lookup { switch rawType { case .dict: @@ -521,6 +524,7 @@ public extension Lookup { } // MARK: - Array + /// Available when rawType is in `[.array, .string]` (if use string, it **MUST** be jsonString) var array: [Any]? { switch rawType { case .array: @@ -540,6 +544,7 @@ public extension Lookup { array! } + /// Available when rawType is in `[.array, .string]` (if use string, it **MUST** be jsonString) var arrayLookup: [Lookup] { switch rawType { case .array: @@ -561,7 +566,7 @@ public extension Lookup { Lookup(rawValue) } - /// Available on `array` and `dict` + /// Available when rawType is in `[.array, .dict]` var jsonData: Data? { switch rawType { case .array: @@ -572,6 +577,34 @@ public extension Lookup { return nil } } + + /// Available when rawType is in `[.array, .dict, .string]` + var isEmpty: Bool { + switch rawType { + case .array: + return rawArray.isEmpty + case .dict: + return rawDict.isEmpty + case .string: + return rawString.isEmpty + default: + return false + } + } + + /// Available when rawType is in `[.array, .dict, .string]` + var count: Int { + switch rawType { + case .array: + return rawArray.count + case .dict: + return rawDict.count + case .string: + return rawString.count + default: + return 0 + } + } } // MARK: Decode