Skip to content

Commit c366e10

Browse files
committed
Update set functionality
1 parent aae725a commit c366e10

File tree

7 files changed

+20
-47
lines changed

7 files changed

+20
-47
lines changed

Sources/Reflection/Any+Extensions.swift

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ extension AnyExtensions {
1818
return try Reflection.construct(self, dictionary: dictionary)
1919
}
2020

21-
static func value(from pointer: UnsafeRawPointer) -> Any {
22-
return pointer.assumingMemoryBound(to: self).pointee
21+
static func isValueTypeOrSubtype(_ value: Any) -> Bool {
22+
return value is Self
2323
}
2424

25-
func write(to pointer: UnsafeMutableRawPointer) {
26-
pointer.assumingMemoryBound(to: type(of: self)).initialize(to: self)
25+
static func value(from storage: UnsafeRawPointer) -> Any {
26+
return storage.assumingMemoryBound(to: self).pointee
27+
}
28+
29+
static func write(_ value: Any, to storage: UnsafeMutableRawPointer) throws {
30+
guard let this = value as? Self else {
31+
throw ReflectionError.valueIsNotType(value: value, type: self)
32+
}
33+
storage.assumingMemoryBound(to: self).initialize(to: this)
2734
}
2835

2936
}

Sources/Reflection/AnyExistentialContainer.swift

Lines changed: 0 additions & 17 deletions
This file was deleted.

Sources/Reflection/Construct.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@ private func constructType(storage: UnsafeMutableRawPointer, values: inout [Any]
2626
for property in properties {
2727
do {
2828
let value = try constructor(property)
29-
guard Reflection.value(value, is: property.type) else { throw ReflectionError.valueIsNotType(value: value, type: property.type) }
3029
values.append(value)
31-
extensions(of: value).write(to: storage.advanced(by: property.offset))
30+
try property.write(value, to: storage)
3231
} catch {
3332
errors.append(error)
3433
}

Sources/Reflection/Identity.swift

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
/// Tests if `value` is `type` or a subclass of `type`
22
public func value(_ value: Any, is type: Any.Type) -> Bool {
3-
if type(of: value) == type {
4-
return true
5-
}
6-
guard var subclass = Metadata.Class(type: type(of: value)), let superclass = Metadata.Class(type: type) else {
7-
return false
8-
}
9-
while let parentClass = subclass.superclass {
10-
if parentClass == superclass {
11-
return true
12-
}
13-
subclass = parentClass
14-
}
15-
return false
3+
return extensions(of: type).isValueTypeOrSubtype(value)
164
}
175

186
/// Tests equality of any two existential types

Sources/Reflection/Properties.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public struct Property {
2121
public let key: String
2222
public let type: Any.Type
2323
let offset: Int
24+
func write(_ value: Any, to storage: UnsafeMutableRawPointer) throws {
25+
return try extensions(of: type).write(value, to: storage.advanced(by: offset))
26+
}
2427
}
2528
}
2629

Sources/Reflection/Set.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/// Set value for key of an instance
22
public func set(_ value: Any, key: String, for instance: inout Any) throws {
3-
let property = try Reflection.property(type: type(of: instance), key: key)
4-
try setValue(value, forKey: key, property: property, storage: mutableStorage(instance: &instance))
3+
try property(type: type(of: instance), key: key).write(value, to: mutableStorage(instance: &instance))
54
}
65

76
/// Set value for key of an instance
@@ -12,16 +11,10 @@ public func set(_ value: Any, key: String, for instance: AnyObject) throws {
1211

1312
/// Set value for key of an instance
1413
public func set<T>(_ value: Any, key: String, for instance: inout T) throws {
15-
let property = try Reflection.property(type: T.self, key: key)
16-
try setValue(value, forKey: key, property: property, storage: mutableStorage(instance: &instance))
14+
try property(type: T.self, key: key).write(value, to: mutableStorage(instance: &instance))
1715
}
1816

1917
private func property(type: Any.Type, key: String) throws -> Property.Description {
2018
guard let property = try properties(type).first(where: { $0.key == key }) else { throw ReflectionError.instanceHasNoKey(type: type, key: key) }
2119
return property
2220
}
23-
24-
private func setValue(_ value: Any, forKey key: String, property: Property.Description, storage: UnsafeMutableRawPointer) throws {
25-
guard Reflection.value(value, is: property.type) else { throw ReflectionError.valueIsNotType(value: value, type: property.type) }
26-
extensions(of: value).write(to: storage.advanced(by: property.offset))
27-
}

Tests/ReflectionTests/PublicTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public class PublicTests : XCTestCase {
7272
}
7373
let flags: Flags = try construct(dictionary: [
7474
"x": false,
75-
"y": nil as Optional<Bool>,
75+
"y": Optional<Bool>.none as Any,
7676
"z": (true, false)
7777
] as [String : Any])
7878
XCTAssert(!flags.x)
@@ -91,7 +91,7 @@ public class PublicTests : XCTestCase {
9191
let object: Object = try construct(dictionary: [
9292
"flag": true,
9393
"pair": (UInt8(1), UInt8(2)),
94-
"float": Optional(Float(89.0)),
94+
"float": Optional<Float>(89.0) as Any,
9595
"integer": 123,
9696
"string": "Hello, world"
9797
] as [String : Any])

0 commit comments

Comments
 (0)