Skip to content

Commit

Permalink
Support custom Dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Mar 20, 2016
1 parent 2e0a11b commit 289fdbb
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 15 deletions.
2 changes: 1 addition & 1 deletion EVReflection.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Pod::Spec.new do |s|
#

s.name = "EVReflection"
s.version = "2.20.2"
s.version = "2.22.0"
s.summary = "iOS: Swift helper library with reflection functions"
s.description = "Swift helper library with reflection functions including support for NSCoding, Printable, Hashable, Equatable and JSON"
s.homepage = "https://github.com/evermeer/EVReflection"
Expand Down
22 changes: 22 additions & 0 deletions EVReflection/EVReflectionTests/EVReflectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,28 @@ class EVReflectionTests: XCTestCase {
}
}

func testCustomNestedArrays() {
let json = "{\"containers\": [{ \"rows\": [{\"kind\": \"main\"}, {\"kind\": \"main2\"}] }, {\"rows\": [{\"kind\": \"main3\"}, {\"kind\": \"main4\"}, {\"kind\": \"main5\"}] }] }"

//let json = "{\"containers\": [{ \"rows\": [{\"kind\": \"main\"}, {\"kind\": \"main2\"}] }, { \"rows\": [{\"kind\": \"main3\"}, {\"kind\": \"main4\"}] }] }"
let c = TestObject8(json: json)
XCTAssertEqual(c.containers.count, 2, "There should be 1 container")
if(c.containers.count == 2) {
XCTAssertEqual(c.containers[0].rows.count, 2, "Container 0 should have 2 rows")
if(c.containers[0].rows.count == 2) {
XCTAssertEqual(c.containers[0].rows[0].kind, "main", "Row 0 of container 0 should contain main" )
XCTAssertEqual(c.containers[0].rows[1].kind, "main2", "Row 1 of container 0 should contain main2" )
}
XCTAssertEqual(c.containers[1].rows.count, 3, "Container 1 should have 3 rows")
if(c.containers[1].rows.count == 3) {
XCTAssertEqual(c.containers[1].rows[0].kind, "main3", "Row 0 of container 1 should contain main3" )
XCTAssertEqual(c.containers[1].rows[1].kind, "main4", "Row 1 of container 1 should contain main4" )
XCTAssertEqual(c.containers[1].rows[2].kind, "main5", "Row 2 of container 1 should contain main5" )
}
}
}


/**
Test if we can work with an object that contains all types of arrays
*/
Expand Down
18 changes: 10 additions & 8 deletions EVReflection/EVReflectionTests/EVReflectionWorkaroundsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,20 @@ class EVReflectionWorkaroundsTests: XCTestCase {
XCTAssertTrue(status.list[1]?.nullableType == 3, "the second item in the list should have nullableType 3")
}
}


func testCustomDictionary() {
func testWorkaroundsCustomDictionary() {
let json: String = "{\"dict\" : {\"firstkey\": {\"field\":5}, \"secondkey\": {\"field\":35}}}"
//TODO: Dictionary property fix
// let doc = WorkaroundObject(json: json)
// XCTAssertEqual(doc.dict.count, 2, "Should have 2 items in the dictionary")
// XCTAssertEqual(doc.dict["firstkey"]?.field, "5", "First sentence should have id 5")
// XCTAssertEqual(doc.dict["secondkey"]?.field, "35", "Second sentence should have id 35")
let doc = WorkaroundObject(json: json)
XCTAssertEqual(doc.dict.count, 2, "Should have 2 items in the dictionary")
XCTAssertEqual(doc.dict["firstkey"]?.field, "5", "First sentence should have id 5")
XCTAssertEqual(doc.dict["secondkey"]?.field, "35", "Second sentence should have id 35")
}
}




//
class WorkaroundObject: EVObject, EVArrayConvertable, EVDictionaryConvertable {

Expand Down Expand Up @@ -130,8 +132,8 @@ class WorkaroundObject: EVObject, EVArrayConvertable, EVDictionaryConvertable {
assert(field == "dict", "convertArray for key \(field) should be handled.")

let returnDict = NSMutableDictionary()
for (key, value) in dict as! [String: SubObject] {
returnDict[key] = value
for (key, value) in dict as! NSDictionary {
returnDict[key as! String] = SubObject(dictionary: value as! NSDictionary)
}
return returnDict
}
Expand Down
12 changes: 12 additions & 0 deletions EVReflection/EVReflectionTests/TestObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ public class TestObject6: EVObject {
}
}

class TestObject8: EVObject {
var containers: [TestObject8Container] = []
}

class TestObject8Container: EVObject {
var rows: [TestObject8Row] = []
}

class TestObject8Row: EVObject {
var kind: String?
}


/**
For testing objects with arrays
Expand Down
12 changes: 6 additions & 6 deletions EVReflection/pod/EVReflection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final public class EVReflection {
if !skipKey {
let mapping = keyMapping[k as! String]
let original:NSObject? = getValue(anyObject, key: mapping ?? k as! String)
if let dictValue = dictionaryAndArrayConversion(types[mapping ?? k as! String], original: original, dictValue: v) {
if let dictValue = dictionaryAndArrayConversion(anyObject, key: k as! String, fieldType: types[mapping ?? k as! String], original: original, dictValue: v) {
if let key:String = keyMapping[k as! String] {
setObjectValue(anyObject, key: key, value: dictValue, typeInObject: types[key])
} else {
Expand Down Expand Up @@ -564,7 +564,7 @@ final public class EVReflection {
}
} else if mi.displayStyle == .Struct {
valueType = "\(mi.subjectType)"
if valueType.containsString("_NativeDictionaryStorage<") {
if valueType.containsString("_NativeDictionaryStorage") {
if let dictionaryConverter = parentObject as? EVDictionaryConvertable {
let convertedValue = dictionaryConverter.convertDictionary(key!, dict: theValue)
return (convertedValue, valueType, false)
Expand Down Expand Up @@ -639,11 +639,9 @@ final public class EVReflection {
// }
} else {
if let (_, propertySetter, _) = (anyObject as? EVObject)?.propertyConverters().filter({$0.0 == key}).first {

guard let propertySetter = propertySetter else {
return // if the propertySetter is nil, skip setting the property
}

}
propertySetter(value)
return
}
Expand Down Expand Up @@ -791,7 +789,7 @@ final public class EVReflection {

:returns: The converted value
*/
private static func dictionaryAndArrayConversion(fieldType:String?, original:NSObject?, var dictValue: AnyObject?) -> AnyObject? {
private static func dictionaryAndArrayConversion(anyObject:NSObject, key: String, fieldType:String?, original:NSObject?, var dictValue: AnyObject?) -> AnyObject? {
if let type = fieldType {
if type.hasPrefix("Array<") && dictValue as? NSDictionary != nil {
if (dictValue as! NSDictionary).count == 1 {
Expand All @@ -806,6 +804,8 @@ final public class EVReflection {
array.append(dictValue as! NSDictionary)
dictValue = array
}
} else if let _ = type.rangeOfString("_NativeDictionaryStorageOwner") , let dict = dictValue as? NSDictionary, let org = anyObject as? EVDictionaryConvertable {
dictValue = org.convertDictionary(key, dict: dict)
} else if type != "NSDictionary" && dictValue as? NSDictionary != nil {
// Sub object
dictValue = dictToObject(type, original:original ,dict: dictValue as! NSDictionary)
Expand Down

0 comments on commit 289fdbb

Please sign in to comment.