Skip to content

Commit 3d6cd2f

Browse files
authored
Merge pull request #17 from iWECon/dev
Add Protocol: LookupUnwrap; Fix UUID
2 parents 9c64408 + fc87457 commit 3d6cd2f

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

Sources/Lookup/Lookup.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ fileprivate func unwrap(_ object: Any?) -> Any {
2020
switch object {
2121
case let lookup as Lookup:
2222
return unwrap(lookup.rawValue)
23+
case let lookupRawValue as LookupRawValue:
24+
return lookupRawValue.lookupRawValue
2325
case let number as NSNumber:
2426
return number
2527
case let str as String:
@@ -641,6 +643,13 @@ public extension Lookup {
641643
}
642644
}
643645

646+
var uuid: UUID? {
647+
if let string {
648+
return UUID(uuidString: string)
649+
}
650+
return nil
651+
}
652+
644653
var lookup: Lookup {
645654
Lookup(rawValue)
646655
}

Sources/Lookup/LookupRawValue.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ public protocol LookupRawValue {
99
}
1010

1111
extension Date: LookupRawValue {
12-
1312
public var lookupRawValue: Any {
1413
self.timeIntervalSince1970
1514
}
1615
}
16+
17+
extension UUID: LookupRawValue {
18+
public var lookupRawValue: Any {
19+
self.uuidString
20+
}
21+
}

Sources/Lookup/LookupUnwrap.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by i on 2024/7/16.
6+
//
7+
8+
import Foundation
9+
10+
public protocol LookupUnwrap {
11+
12+
func lookupUnwrap(key: String, value: Any) -> Any?
13+
}
14+
15+
extension LookupUnwrap {
16+
public func lookupUnwrap(key: String, value: Any) -> Any? {
17+
value
18+
}
19+
}

Sources/Lookup/Mirrors.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ public func mirrors(reflecting: Any?, _ each: ((_: String?, _: Any) -> Void)? =
3333
let mirror = Mirror(reflecting: reflecting)
3434
for child in mirror.children {
3535
if let label = child.label, !label.isEmpty {
36-
map[label] = canMirrorInto(child.value) ? mirrors(reflecting: child.value, each) : mirrorValue(child.value)
36+
if let unwrap = reflecting as? LookupUnwrap, let unwrapped = unwrap.lookupUnwrap(key: label, value: child.value) {
37+
map[label] = mirrorValue(unwrapped)
38+
} else {
39+
map[label] = canMirrorInto(child.value) ? mirrors(reflecting: child.value, each) : mirrorValue(child.value)
40+
}
3741
}
3842
each?(child.label, child.value)
3943
}
@@ -42,7 +46,11 @@ public func mirrors(reflecting: Any?, _ each: ((_: String?, _: Any) -> Void)? =
4246
while superMirror != nil {
4347
for child in superMirror!.children {
4448
if let label = child.label, !label.isEmpty {
45-
map[label] = canMirrorInto(child.value) ? mirrors(reflecting: child.value, each) : mirrorValue(child.value)
49+
if let unwrap = reflecting as? LookupUnwrap, let unwrapped = unwrap.lookupUnwrap(key: label, value: child.value) {
50+
map[label] = mirrorValue(unwrapped)
51+
} else {
52+
map[label] = canMirrorInto(child.value) ? mirrors(reflecting: child.value, each) : mirrorValue(child.value)
53+
}
4654
}
4755
each?(child.label, child.value)
4856
}

Tests/LookupTests/LookupTests.swift

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ final class Species: AnimalClass {
3535
let start: Date = Date()
3636
}
3737

38+
struct UnwrapModel: LookupUnwrap {
39+
let id: UUID
40+
let age: Int
41+
let type: AnimalType
42+
let intType: AnimalIntType
43+
let date: Date
44+
45+
func lookupUnwrap(key: String, value: Any) -> Any? {
46+
if key == "date" {
47+
return date.timeIntervalSince1970
48+
}
49+
return value
50+
}
51+
}
52+
3853
final class LookupTests: XCTestCase {
3954

4055
func tests() throws {
@@ -327,11 +342,11 @@ final class LookupTests: XCTestCase {
327342
func testSelect() throws {
328343

329344
struct User {
330-
let id: Int
345+
let id: UUID = UUID()
331346
let name: String
332347
let age: Int
333348
}
334-
let user = User(id: 1, name: "wei", age: 18)
349+
let user = User(name: "wei", age: 18)
335350
let lookup = Lookup(user)
336351
let keepLookup = lookup.keep(keys: ["name"])
337352

@@ -391,6 +406,13 @@ final class LookupTests: XCTestCase {
391406
}
392407
try testSelect()
393408

409+
func testUnwrap() throws {
410+
let model = UnwrapModel(id: UUID(), age: 1, type: .cat, intType: .cat, date: Date())
411+
let lookup = Lookup(model)
412+
print(lookup)
413+
}
414+
try testUnwrap()
415+
394416
#if os(iOS)
395417
func uiView() throws {
396418
let view = UIView()

0 commit comments

Comments
 (0)