Skip to content

Commit 719c809

Browse files
committed
Update Mirrors
1 parent da8df86 commit 719c809

File tree

3 files changed

+69
-66
lines changed

3 files changed

+69
-66
lines changed

Sources/Lookup/Lookup.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public struct Lookup: @unchecked Sendable {
352352
}
353353

354354
extension Lookup: ExpressibleByArrayLiteral {
355-
public typealias ArrayLiteralElement = Any
355+
public typealias ArrayLiteralElement = any Any & Sendable
356356

357357
public init(arrayLiteral elements: ArrayLiteralElement...) {
358358
self.init(jsonObject: elements)
@@ -361,7 +361,7 @@ extension Lookup: ExpressibleByArrayLiteral {
361361

362362
extension Lookup: ExpressibleByDictionaryLiteral {
363363
public typealias Key = String
364-
public typealias Value = Any?
364+
public typealias Value = (any Any & Sendable)?
365365
public init(dictionaryLiteral elements: (Key, Value)...) {
366366
self.init(jsonObject: Dictionary(uniqueKeysWithValues: elements))
367367
}

Sources/Lookup/Mirrors.swift

Lines changed: 41 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,42 @@ func mirrorArray(_ array: [Any], _ each: ((_: String?, _: Any) -> Void)?) -> [An
6565
}
6666
}
6767

68+
private func processChild(label: String, value: Any, reflecting: Any, each: ((String?, Any) -> Void)?) -> (String, Any)? {
69+
if let unwrap = reflecting as? LookupUnwrap, let unwrapped = unwrap.lookupUnwrap(key: label, value: value) {
70+
let processedValue = mirrorValue(unwrapped)
71+
if let opt = processedValue as? OptionalProtocol, opt.isNil {
72+
return nil
73+
} else if processedValue is NSNull {
74+
return nil
75+
} else {
76+
return (label, processedValue)
77+
}
78+
} else {
79+
let unwrappedValue = unwrapValue(value)
80+
if let array = unwrappedValue as? [Any] {
81+
let mirroredArray = mirrorArray(array, each)
82+
if !mirroredArray.isEmpty {
83+
return (label, mirroredArray)
84+
}
85+
} else if canMirrorInto(unwrappedValue) {
86+
let mirroredDict = mirrors(reflecting: unwrappedValue, each)
87+
if !mirroredDict.isEmpty {
88+
return (label, mirroredDict)
89+
}
90+
} else {
91+
let processedValue = mirrorValue(value)
92+
if let opt = processedValue as? OptionalProtocol, opt.isNil {
93+
return nil
94+
} else if processedValue is NSNull {
95+
return nil
96+
} else {
97+
return (label, processedValue)
98+
}
99+
}
100+
}
101+
return nil
102+
}
103+
68104
public func mirrors(reflecting: Any?, _ each: ((_: String?, _: Any) -> Void)? = nil) -> [String: Any] {
69105
guard let reflecting = reflecting else { return [:] }
70106

@@ -73,37 +109,8 @@ public func mirrors(reflecting: Any?, _ each: ((_: String?, _: Any) -> Void)? =
73109
let mirror = Mirror(reflecting: reflecting)
74110
for child in mirror.children {
75111
if let label = child.label, !label.isEmpty {
76-
if let unwrap = reflecting as? LookupUnwrap, let unwrapped = unwrap.lookupUnwrap(key: label, value: child.value) {
77-
let value = mirrorValue(unwrapped)
78-
if let opt = value as? OptionalProtocol, opt.isNil {
79-
// skip
80-
} else if value is NSNull {
81-
// skip
82-
} else {
83-
map[label] = value
84-
}
85-
} else {
86-
let value = unwrapValue(child.value)
87-
if let array = value as? [Any] {
88-
let mirroredArray = mirrorArray(array, each)
89-
if !mirroredArray.isEmpty {
90-
map[label] = mirroredArray
91-
}
92-
} else if canMirrorInto(value) {
93-
let mirroredDict = mirrors(reflecting: value, each)
94-
if !mirroredDict.isEmpty {
95-
map[label] = mirroredDict
96-
}
97-
} else {
98-
let value = mirrorValue(child.value)
99-
if let opt = value as? OptionalProtocol, opt.isNil {
100-
// skip
101-
} else if value is NSNull {
102-
// skip
103-
} else {
104-
map[label] = value
105-
}
106-
}
112+
if let result = processChild(label: label, value: child.value, reflecting: reflecting, each: each) {
113+
map[result.0] = result.1
107114
}
108115
}
109116
each?(child.label, child.value)
@@ -113,37 +120,8 @@ public func mirrors(reflecting: Any?, _ each: ((_: String?, _: Any) -> Void)? =
113120
while superMirror != nil {
114121
for child in superMirror!.children {
115122
if let label = child.label, !label.isEmpty {
116-
if let unwrap = reflecting as? LookupUnwrap, let unwrapped = unwrap.lookupUnwrap(key: label, value: child.value) {
117-
let value = mirrorValue(unwrapped)
118-
if let opt = value as? OptionalProtocol, opt.isNil {
119-
// skip
120-
} else if value is NSNull {
121-
// skip
122-
} else {
123-
map[label] = value
124-
}
125-
} else {
126-
let value = unwrapValue(child.value)
127-
if let array = value as? [Any] {
128-
let mirroredArray = mirrorArray(array, each)
129-
if !mirroredArray.isEmpty {
130-
map[label] = mirroredArray
131-
}
132-
} else if canMirrorInto(value) {
133-
let mirroredDict = mirrors(reflecting: value, each)
134-
if !mirroredDict.isEmpty {
135-
map[label] = mirroredDict
136-
}
137-
} else {
138-
let value = mirrorValue(child.value)
139-
if let opt = value as? OptionalProtocol, opt.isNil {
140-
// skip
141-
} else if value is NSNull {
142-
// skip
143-
} else {
144-
map[label] = value
145-
}
146-
}
123+
if let result = processChild(label: label, value: child.value, reflecting: reflecting, each: each) {
124+
map[result.0] = result.1
147125
}
148126
}
149127
each?(child.label, child.value)
@@ -152,3 +130,4 @@ public func mirrors(reflecting: Any?, _ each: ((_: String?, _: Any) -> Void)? =
152130
}
153131
return map
154132
}
133+

Tests/LookupTests/LookupTests.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ import Foundation
55
import UIKit
66
#endif
77

8+
struct Params {
9+
let lookup: Lookup
10+
init(_ lookup: Lookup) {
11+
self.lookup = lookup
12+
}
13+
}
14+
15+
extension Params: ExpressibleByDictionaryLiteral {
16+
typealias Key = String
17+
typealias Value = any Any & Sendable
18+
init(dictionaryLiteral elements: (String, any Sendable)...) {
19+
self.init(Lookup(Dictionary(uniqueKeysWithValues: elements)))
20+
}
21+
}
22+
823
enum AnimalType: String, Codable {
924
case dog, cat
1025
}
@@ -459,8 +474,10 @@ struct LookupTests {
459474

460475
@Test("Test ExpressionByDictionary")
461476
func testExpressionByDictionary() throws {
462-
let lookup = Lookup(["ids": [UUID(), UUID(), UUID()]])
463-
#expect(lookup.ids.count == 3)
477+
478+
let params: Params = ["ids": [UUID(), UUID(), UUID()]]
479+
print(params.lookup)
480+
#expect(params.lookup.ids.count == 3)
464481
}
465482

466483
@Test("Test Nesting Codable")
@@ -504,6 +521,13 @@ struct LookupTests {
504521
#expect(clookup.hasKey("user") == false)
505522
}
506523

524+
@Test("Test Array")
525+
func testArray() async throws {
526+
let lookup: Lookup = ["ids": [UUID()]]
527+
print(lookup.description)
528+
#expect(lookup.ids.count == 1)
529+
}
530+
507531
#if os(iOS)
508532
@Test("Test UIView")
509533
func testUIView() throws {

0 commit comments

Comments
 (0)