Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Protocol: LookupUnwrap; Fix UUID #17

Merged
merged 1 commit into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Sources/Lookup/Lookup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -641,6 +643,13 @@ public extension Lookup {
}
}

var uuid: UUID? {
if let string {
return UUID(uuidString: string)
}
return nil
}

var lookup: Lookup {
Lookup(rawValue)
}
Expand Down
7 changes: 6 additions & 1 deletion Sources/Lookup/LookupRawValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
19 changes: 19 additions & 0 deletions Sources/Lookup/LookupUnwrap.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
12 changes: 10 additions & 2 deletions Sources/Lookup/Mirrors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
26 changes: 24 additions & 2 deletions Tests/LookupTests/LookupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"])

Expand Down Expand Up @@ -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()
Expand Down
Loading