Skip to content

Commit

Permalink
CustomReflectable fix
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Aug 24, 2018
1 parent 54ce05f commit 5805b36
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 19 deletions.
2 changes: 1 addition & 1 deletion EVReflection.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "EVReflection"
s.version = "5.6.2"
s.version = "5.7.0"
s.summary = "Reflection based object mapping. (Dictionary, CKRecord, NSManagedObject, Realm, JSON, XML, Alamofire, Moya, RxSwift, ReactiveSwift)"

s.description = <<-EOS
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,21 @@
breakpointStackSelectionBehavior = "1">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Source/Realm/RealmOptionalEVCustomReflectable.swift"
timestampString = "556751533.85367"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "18"
endingLineNumber = "18"
landmarkName = "constructWith(value:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
8 changes: 6 additions & 2 deletions Source/CloudKit/CKRecordID+EVCustomReflectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import CloudKit

// We have to use custom reflection for a CKRecordID because its a special type
extension CKRecordID: EVCustomReflectable {
public func constructWith(value: Any?) -> EVCustomReflectable {
public static func constructWith(value: Any?) -> EVCustomReflectable? {
if let dict = value as? NSDictionary {
return CKRecordID(recordName: dict["recordName"] as? String ?? "")
}
print("ERROR: Could not create CKRecordID for \(String(describing: value))")
return self
return nil
}

public func constructWith(value: Any?) -> EVCustomReflectable? {
return CKRecordID.constructWith(value: value)
}

public func toCodableValue() -> Any {
Expand Down
8 changes: 6 additions & 2 deletions Source/CloudKit/CKReference+EVCustomReflectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ import CloudKit

// We have to use custom reflection for a CKRecordID because its a special type
extension CKReference: EVCustomReflectable {
public func constructWith(value: Any?) -> EVCustomReflectable {
public static func constructWith(value: Any?) -> EVCustomReflectable? {
if let dict = value as? NSDictionary {
return CKReference(recordID: CKRecordID(recordName: dict["recordName"] as? String ?? ""), action: CKReferenceAction.none)
}
print("ERROR: Could not create CKReference for \(String(describing: value))")
return self
return nil
}

public func constructWith(value: Any?) -> EVCustomReflectable? {
return CKReference.constructWith(value: value)
}

public func toCodableValue() -> Any {
Expand Down
3 changes: 2 additions & 1 deletion Source/EVCustomReflectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation

// Protocol that can be used for sub objects to define that parsing will be done in the parent using the 'setValue forKey' function
public protocol EVCustomReflectable {
func constructWith(value: Any?) -> EVCustomReflectable
static func constructWith(value: Any?) -> EVCustomReflectable?
func constructWith(value: Any?) -> EVCustomReflectable?
func toCodableValue() -> Any
}
11 changes: 7 additions & 4 deletions Source/EVReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ final public class EVReflection {
(dictValue, valid) = dictionaryAndArrayConversion(anyObject, key: keyInObject!, fieldType: types[dictKey] as? String ?? types[keyInObject!] as? String, original: original, theDictValue: v as Any?, conversionOptions: conversionOptions)
}

if let value: Any = valid ? dictValue : (v as Any) {
if var custom = original as? EVCustomReflectable {
custom = custom.constructWith(value: value)
if var value: Any = valid ? dictValue : (v as Any) {
if let type: String = types[k as! String] as? String {
let t: AnyClass? = swiftClassTypeFromString(type)
if let c = t as? EVCustomReflectable {
value = c.constructWith(value: value)!
}
}
setObjectValue(anyObject, key: keyInObject!, theValue: value, typeInObject: types[keyInObject!] as? String, valid: valid, conversionOptions: conversionOptions)
}
Expand Down Expand Up @@ -1437,7 +1440,7 @@ final public class EVReflection {

if let v = value as? EVCustomReflectable {
unboxedValue = v.toCodableValue() as AnyObject
valueType = "String"
valueType = String(describing: type(of: v))
isObject = false
}

Expand Down
6 changes: 5 additions & 1 deletion Source/Realm/RealmListEVCustomReflectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension List : EVCustomReflectable {
- parameter value: The dictionary that will be converted to an object
*/
public func constructWith(value: Any?) -> EVCustomReflectable {
public func constructWith(value: Any?) -> EVCustomReflectable? {
if let array = value as? [NSDictionary] {
self.removeAll()
for dict in array {
Expand All @@ -33,6 +33,10 @@ extension List : EVCustomReflectable {
}
return self
}

public static func constructWith(value: Any?) -> EVCustomReflectable? {
return List().constructWith(value: value)
}

/**
If you have a custom type that requires special conversion, then you can extend it with the EVCustomReflectable protocol.
Expand Down
6 changes: 5 additions & 1 deletion Source/Realm/RealmObjectEVCustomReflectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ extension Object: EVCustomReflectable {
- parameter value: The dictionary that will be converted to an object
*/
public func constructWith(value: Any?) -> EVCustomReflectable {
public func constructWith(value: Any?) -> EVCustomReflectable? {
if let jsonDict = value as? NSDictionary {
EVReflection.setPropertiesfromDictionary(jsonDict, anyObject: self)
}
return self
}

public static func constructWith(value: Any?) -> EVCustomReflectable? {
return Object().constructWith(value: value)
}

/**
If you have a custom type that requires special conversion, then you can extend it with the EVCustomReflectable protocol.
Expand Down
6 changes: 5 additions & 1 deletion Source/Realm/RealmOptionalEVCustomReflectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension RealmOptional : EVCustomReflectable {
- parameter value: The dictionary that will be converted to an object
*/
public func constructWith(value: Any?) -> EVCustomReflectable {
public func constructWith(value: Any?) -> EVCustomReflectable? {
if value is NSNull {
self.value = nil
} else {
Expand All @@ -23,6 +23,10 @@ extension RealmOptional : EVCustomReflectable {
return self
}

public static func constructWith(value: Any?) -> EVCustomReflectable? {
return RealmOptional().constructWith(value: value)
}

/**
If you have a custom type that requires special conversion, then you can extend it with the EVCustomReflectable protocol.
Since Mirror does not work for a Realm Object we use the .value forKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ open class XObject: EVObject {
}

extension XObject : EVCustomReflectable {
public func constructWith(value: Any?) -> EVCustomReflectable {
public static func constructWith(value: Any?) -> EVCustomReflectable? {
return XObject().constructWith(value: value)
}
public func constructWith(value: Any?) -> EVCustomReflectable? {
if let jsonDict = value as? NSDictionary {
EVReflection.setPropertiesfromDictionary(jsonDict, anyObject: self)
}
Expand Down
5 changes: 4 additions & 1 deletion UnitTests/EVReflectionTests/EVReflectionIssue197.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,15 @@ extension Base197 : EVCustomReflectable {
- parameter value: The dictionary that will be converted to an object
*/
public func constructWith(value: Any?) -> EVCustomReflectable {
public func constructWith(value: Any?) -> EVCustomReflectable? {
if let jsonDict = value as? NSDictionary {
EVReflection.setPropertiesfromDictionary(jsonDict, anyObject: self)
}
return self
}
public static func constructWith(value: Any?) -> EVCustomReflectable? {
return Base197().constructWith(value: value)
}

/**
If you have a custom type that requires special conversion, then you can extend it with the EVCustomReflectable protocol.
Expand Down
12 changes: 9 additions & 3 deletions UnitTests/EVReflectionTests/WorkaroundEnumTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,21 @@ public class EnumWrapper: NSObject, EVCustomReflectable {
return (value?.anyRawValue ?? raw)
}

public init(_ value: EVRaw) {
self.value = value
public init(_ value: EVRaw? = nil) {
if let value = value {
self.value = value
}
}

public func constructWith(value: Any?) -> EVCustomReflectable {
public func constructWith(value: Any?) -> EVCustomReflectable? {
self.value = nil
self.raw = value
return self
}

public static func constructWith(value: Any?) -> EVCustomReflectable? {
return EnumWrapper().constructWith(value: value)
}

public func toCodableValue() -> Any {
return (value?.anyRawValue ?? raw)!
Expand Down
1 change: 0 additions & 1 deletion UnitTests/RealmTests/RealmTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ class RealmTests: XCTestCase {
print("dict = \(dict)")
let newObj = PrimitiveListsObject(dictionary: dict)
print("newObj = \(newObj)")

}

func testIssue270() {
Expand Down

0 comments on commit 5805b36

Please sign in to comment.