Skip to content

Be able to set nil values #19

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Sources/Reflection/Any+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension AnyExtensions {
return storage.assumingMemoryBound(to: self).pointee
}

static func write(_ value: Any, to storage: UnsafeMutableRawPointer) throws {
static func write(_ value: Any?, to storage: UnsafeMutableRawPointer) throws {
guard let this = value as? Self else {
throw ReflectionError.valueIsNotType(value: value, type: self)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Reflection/Properties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public struct Property {
public let key: String
public let type: Any.Type
let offset: Int
func write(_ value: Any, to storage: UnsafeMutableRawPointer) throws {
func write(_ value: Any?, to storage: UnsafeMutableRawPointer) throws {
return try extensions(of: type).write(value, to: storage.advanced(by: offset))
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Reflection/ReflectionError.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
public enum ReflectionError : Error, CustomStringConvertible, Equatable {
case notStruct(type: Any.Type)
case valueIsNotType(value: Any, type: Any.Type)
case valueIsNotType(value: Any?, type: Any.Type)
case instanceHasNoKey(type: Any.Type, key: String)
case requiredValueMissing(key: String)
case unexpected
Expand Down
6 changes: 3 additions & 3 deletions Sources/Reflection/Set.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/// Set value for key of an instance
public func set(_ value: Any, key: String, for instance: inout Any) throws {
public func set(_ value: Any?, key: String, for instance: inout Any) throws {
let type = Swift.type(of: instance)
try property(type: type, key: key).write(value, to: mutableStorage(instance: &instance, type: type))
}

/// Set value for key of an instance
public func set(_ value: Any, key: String, for instance: AnyObject) throws {
public func set(_ value: Any?, key: String, for instance: AnyObject) throws {
var copy: Any = instance
try set(value, key: key, for: &copy)
}

/// Set value for key of an instance
public func set<T>(_ value: Any, key: String, for instance: inout T) throws {
public func set<T>(_ value: Any?, key: String, for instance: inout T) throws {
try property(type: T.self, key: key).write(value, to: mutableStorage(instance: &instance))
}

Expand Down
15 changes: 10 additions & 5 deletions Tests/ReflectionTests/PublicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import XCTest
import Reflection

struct Person : Equatable {
var firstName: String
var firstName: String?
var lastName: String
var age: Int
var age: Int?

init(firstName: String, lastName: String, age: Int) {
self.firstName = firstName
Expand Down Expand Up @@ -133,7 +133,14 @@ public class PublicTests : XCTestCase {
func testSetValueForKeyOfInstance() throws {
var person = Person(firstName: "Brad", lastName: "Hilton", age: 27)
try set("Lawrence", key: "firstName", for: &person)
XCTAssert(person.firstName == "Lawrence")
XCTAssertEqual(person.firstName, "Lawrence")
try set(nil, key: "firstName", for: &person)
XCTAssertNil(person.firstName)
XCTAssertEqual(person.age, 27)
try set(nil, key: "age", for: &person)
XCTAssertNil(person.age)
try set(28, key: "age", for: &person)
XCTAssertEqual(person.age, 28)
}

func testValueForKeyOfInstance() throws {
Expand Down Expand Up @@ -204,9 +211,7 @@ public class PublicTests : XCTestCase {
XCTFail()
} catch let constructionErrors as ConstructionErrors {
let expectedErrors: [ReflectionError] = [
.requiredValueMissing(key: "firstName"),
.requiredValueMissing(key: "lastName"),
.requiredValueMissing(key: "age")
]
XCTAssertEqual(constructionErrors.errors.compactMap { $0 as? ReflectionError }, expectedErrors)
} catch {
Expand Down