From fc874572017b0d333f43d61b3ab39c2d52b051d3 Mon Sep 17 00:00:00 2001 From: iWw Date: Tue, 16 Jul 2024 22:45:00 +0800 Subject: [PATCH] Add Protocol: LookupUnwrap; Fix UUID --- Sources/Lookup/Lookup.swift | 9 +++++++++ Sources/Lookup/LookupRawValue.swift | 7 ++++++- Sources/Lookup/LookupUnwrap.swift | 19 +++++++++++++++++++ Sources/Lookup/Mirrors.swift | 12 ++++++++++-- Tests/LookupTests/LookupTests.swift | 26 ++++++++++++++++++++++++-- 5 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 Sources/Lookup/LookupUnwrap.swift diff --git a/Sources/Lookup/Lookup.swift b/Sources/Lookup/Lookup.swift index b171f55..bc7d3cd 100644 --- a/Sources/Lookup/Lookup.swift +++ b/Sources/Lookup/Lookup.swift @@ -20,6 +20,8 @@ fileprivate func unwrap(_ object: Any?) -> Any { switch object { case let lookup as Lookup: return unwrap(lookup.rawValue) + case let lookupRawValue as LookupRawValue: + return lookupRawValue.lookupRawValue case let number as NSNumber: return number case let str as String: @@ -641,6 +643,13 @@ public extension Lookup { } } + var uuid: UUID? { + if let string { + return UUID(uuidString: string) + } + return nil + } + var lookup: Lookup { Lookup(rawValue) } diff --git a/Sources/Lookup/LookupRawValue.swift b/Sources/Lookup/LookupRawValue.swift index 21aaba5..516ad51 100644 --- a/Sources/Lookup/LookupRawValue.swift +++ b/Sources/Lookup/LookupRawValue.swift @@ -9,8 +9,13 @@ public protocol LookupRawValue { } extension Date: LookupRawValue { - public var lookupRawValue: Any { self.timeIntervalSince1970 } } + +extension UUID: LookupRawValue { + public var lookupRawValue: Any { + self.uuidString + } +} diff --git a/Sources/Lookup/LookupUnwrap.swift b/Sources/Lookup/LookupUnwrap.swift new file mode 100644 index 0000000..a385480 --- /dev/null +++ b/Sources/Lookup/LookupUnwrap.swift @@ -0,0 +1,19 @@ +// +// File.swift +// +// +// Created by i on 2024/7/16. +// + +import Foundation + +public protocol LookupUnwrap { + + func lookupUnwrap(key: String, value: Any) -> Any? +} + +extension LookupUnwrap { + public func lookupUnwrap(key: String, value: Any) -> Any? { + value + } +} diff --git a/Sources/Lookup/Mirrors.swift b/Sources/Lookup/Mirrors.swift index 988ddeb..ed32ebd 100644 --- a/Sources/Lookup/Mirrors.swift +++ b/Sources/Lookup/Mirrors.swift @@ -33,7 +33,11 @@ public func mirrors(reflecting: Any?, _ each: ((_: String?, _: Any) -> Void)? = let mirror = Mirror(reflecting: reflecting) for child in mirror.children { if let label = child.label, !label.isEmpty { - map[label] = canMirrorInto(child.value) ? mirrors(reflecting: child.value, each) : mirrorValue(child.value) + if let unwrap = reflecting as? LookupUnwrap, let unwrapped = unwrap.lookupUnwrap(key: label, value: child.value) { + map[label] = mirrorValue(unwrapped) + } else { + map[label] = canMirrorInto(child.value) ? mirrors(reflecting: child.value, each) : mirrorValue(child.value) + } } each?(child.label, child.value) } @@ -42,7 +46,11 @@ public func mirrors(reflecting: Any?, _ each: ((_: String?, _: Any) -> Void)? = while superMirror != nil { for child in superMirror!.children { if let label = child.label, !label.isEmpty { - map[label] = canMirrorInto(child.value) ? mirrors(reflecting: child.value, each) : mirrorValue(child.value) + if let unwrap = reflecting as? LookupUnwrap, let unwrapped = unwrap.lookupUnwrap(key: label, value: child.value) { + map[label] = mirrorValue(unwrapped) + } else { + map[label] = canMirrorInto(child.value) ? mirrors(reflecting: child.value, each) : mirrorValue(child.value) + } } each?(child.label, child.value) } diff --git a/Tests/LookupTests/LookupTests.swift b/Tests/LookupTests/LookupTests.swift index e142a92..2b303fa 100644 --- a/Tests/LookupTests/LookupTests.swift +++ b/Tests/LookupTests/LookupTests.swift @@ -35,6 +35,21 @@ final class Species: AnimalClass { let start: Date = Date() } +struct UnwrapModel: LookupUnwrap { + let id: UUID + let age: Int + let type: AnimalType + let intType: AnimalIntType + let date: Date + + func lookupUnwrap(key: String, value: Any) -> Any? { + if key == "date" { + return date.timeIntervalSince1970 + } + return value + } +} + final class LookupTests: XCTestCase { func tests() throws { @@ -327,11 +342,11 @@ final class LookupTests: XCTestCase { func testSelect() throws { struct User { - let id: Int + let id: UUID = UUID() let name: String let age: Int } - let user = User(id: 1, name: "wei", age: 18) + let user = User(name: "wei", age: 18) let lookup = Lookup(user) let keepLookup = lookup.keep(keys: ["name"]) @@ -391,6 +406,13 @@ final class LookupTests: XCTestCase { } try testSelect() + func testUnwrap() throws { + let model = UnwrapModel(id: UUID(), age: 1, type: .cat, intType: .cat, date: Date()) + let lookup = Lookup(model) + print(lookup) + } + try testUnwrap() + #if os(iOS) func uiView() throws { let view = UIView()