Skip to content

Commit

Permalink
Merge pull request #18 from iWECon/dev
Browse files Browse the repository at this point in the history
fix compactMapValues
  • Loading branch information
iWECon authored Jul 17, 2024
2 parents 3d6cd2f + 0077e24 commit ac2e544
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
16 changes: 6 additions & 10 deletions Sources/Lookup/Lookup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -700,15 +700,11 @@ public extension Lookup {
// MARK: Decode
extension Lookup {

public enum DecodeError: Swift.Error {
case invalidJSONData
}

public func decode<D>(as decodable: D.Type, using decoder: JSONDecoder = JSONDecoder()) throws -> D where D: Decodable {
guard let jsonData else {
throw DecodeError.invalidJSONData
public func decode<D>(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)
}
}

Expand Down Expand Up @@ -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:
Expand All @@ -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))
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/LookupTests/LookupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ac2e544

Please sign in to comment.