From 0077e24fa4225aaa1c8868f13ed2261a83ec33f3 Mon Sep 17 00:00:00 2001 From: iWw Date: Thu, 18 Jul 2024 02:19:04 +0800 Subject: [PATCH] fix compactMapValues --- Sources/Lookup/Lookup.swift | 16 ++++++---------- Tests/LookupTests/LookupTests.swift | 4 ++-- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/Sources/Lookup/Lookup.swift b/Sources/Lookup/Lookup.swift index bc7d3cd..4ab6325 100644 --- a/Sources/Lookup/Lookup.swift +++ b/Sources/Lookup/Lookup.swift @@ -700,15 +700,11 @@ 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 + public func decode(as decodable: D.Type, using decoder: JSONDecoder = JSONDecoder()) throws -> D? where D: Decodable { + guard let encoded = try? JSONEncoder().encode(self) else { + return nil } - return try decoder.decode(D.self, from: jsonData) + return try decoder.decode(D.self, from: encoded) } } @@ -919,7 +915,7 @@ extension Lookup { return self case .array: let map = rawArray.map { value in - Lookup(value).compactMapValues() + Lookup(value).compactMapValues(keepEmptyValue: keepEmptyValue) } return Lookup(map) case .object, .dict: @@ -928,7 +924,7 @@ extension Lookup { if vl.isNone || (keepEmptyValue && vl.isEmpty) { return nil } - return (k, vl.compactMapValues()) + return (k, vl.compactMapValues(keepEmptyValue: keepEmptyValue)) } return Lookup(Dictionary(uniqueKeysWithValues: map)) } diff --git a/Tests/LookupTests/LookupTests.swift b/Tests/LookupTests/LookupTests.swift index 2b303fa..3a0b19d 100644 --- a/Tests/LookupTests/LookupTests.swift +++ b/Tests/LookupTests/LookupTests.swift @@ -5,11 +5,11 @@ import Foundation import UIKit #endif -enum AnimalType { +enum AnimalType: String, Codable { case dog, cat } -enum AnimalIntType: Int, LookupEnum { +enum AnimalIntType: Int, LookupEnum, Codable { case dog = 0, cat var lookupRawValue: Any {