diff --git a/EVReflection.podspec b/EVReflection.podspec index 3d15590c..2ee04e7e 100755 --- a/EVReflection.podspec +++ b/EVReflection.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "EVReflection" - s.version = "5.5.6" + s.version = "5.6.0" s.summary = "Reflection based object mapping. (Dictionary, CKRecord, NSManagedObject, Realm, JSON, XML, Alamofire, Moya, RxSwift, ReactiveSwift)" s.description = <<-EOS diff --git a/EVReflection.xcworkspace/xcuserdata/evermeer.xcuserdatad/UserInterfaceState.xcuserstate b/EVReflection.xcworkspace/xcuserdata/evermeer.xcuserdatad/UserInterfaceState.xcuserstate index d5c9963f..21f6adb4 100644 Binary files a/EVReflection.xcworkspace/xcuserdata/evermeer.xcuserdatad/UserInterfaceState.xcuserstate and b/EVReflection.xcworkspace/xcuserdata/evermeer.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/Source/EVArrayExtension.swift b/Source/EVArrayExtension.swift index 85156cc9..cf9fc8c9 100755 --- a/Source/EVArrayExtension.swift +++ b/Source/EVArrayExtension.swift @@ -190,3 +190,47 @@ public extension Array where Element: NSDictionary { } } + +public extension NSArray { + func nestedArrayMap(_ element: (NSDictionary)->T) -> [[T]] { + return (self.map { + (($0 as? NSArray)?.map { + element($0 as? NSDictionary ?? NSDictionary()) + }) ?? [] + }) + } + + func doubleNestedArrayMap(_ element: (NSDictionary)->T) -> [[[T]]] { + return (self.map { + (($0 as? NSArray)?.nestedArrayMap { element($0) }) ?? [[]] + }) + } + + func tripleNestedArrayMap(_ element: (NSDictionary)->T) -> [[[[T]]]] { + return (self.map { + (($0 as? NSArray)?.doubleNestedArrayMap { element($0) }) ?? [[[]]] + }) + } + + func quadrupleNestedArrayMap(_ element: (NSDictionary)->T) -> [[[[[T]]]]] { + return (self.map { + (($0 as? NSArray)?.tripleNestedArrayMap { element($0) }) ?? [[[[]]]] + }) + } + + func quintupleNestedArrayMap(_ element: (NSDictionary)->T) -> [[[[[[T]]]]]] { + return (self.map { + (($0 as? NSArray)?.quadrupleNestedArrayMap { element($0) }) ?? [[[[[]]]]] + }) + } + + func sextupleNestedArrayMap(_ element: (NSDictionary)->T) -> [[[[[[[T]]]]]]] { + return (self.map { + (($0 as? NSArray)?.quintupleNestedArrayMap { element($0) }) ?? [[[[[[]]]]]] + }) + } + + // If you need deeper nesting, whell, then you probably see the pattern above that you need to implement :-) + // just name them septuple, octuple, nonuple and decuple + // I'm not sure how far swift can handle it, but you should not want something like that. +} diff --git a/UnitTests/EVReflectionTests/EVReflectionTests.swift b/UnitTests/EVReflectionTests/EVReflectionTests.swift index f1beb209..59c47b53 100755 --- a/UnitTests/EVReflectionTests/EVReflectionTests.swift +++ b/UnitTests/EVReflectionTests/EVReflectionTests.swift @@ -502,6 +502,21 @@ class EVReflectionTests: XCTestCase { XCTAssert(obj.array[0][0].count == 3, "3 dimentional array should have 3 items inside the first item of the first item") XCTAssert(obj.array[0][0][0].openId == "value1", "3 dimentional array should have openId with a value of value1 in the first item of the first item in the first item") } + + func testSuperNested(){ + // For now using the propertyConverters workaround. + let json = "{\"array\":[[[[[[[{\"openId\":\"value1\"},{\"openId\":\"value2\"},{\"openId\":\"value3\"}],[{\"openId\":\"value3\"},{\"openId\":\"value4\"}]]]]]]]}" + let obj = A81d(json: json) + print(obj) + XCTAssert(obj.array.count == 1, "6 dimentional array should have 1 item") + XCTAssert(obj.array[0].count == 1, "6 dimentional array should have 1 item on nesting 1") + XCTAssert(obj.array[0][0].count == 1, "6 dimentional array should have 1 item on nesting 2") + XCTAssert(obj.array[0][0][0].count == 1, "6 dimentional array should have 1 item on nesting 3") + XCTAssert(obj.array[0][0][0][0].count == 1, "6 dimentional array should have 1 item on nesting 4") + XCTAssert(obj.array[0][0][0][0][0].count == 2, "6 dimentional array should have 2 item on nesting 5") + XCTAssert(obj.array[0][0][0][0][0][0].count == 3, "6 dimentional array should have 3 items inside nesting 6") + XCTAssert(obj.array[0][0][0][0][0][0][0].openId == "value1", "6 dimentional array should have openId with a value of value1 in the first available item") + } // Swift bug, class inside class works, class inside struct does not work. func testNestedDefinition() { diff --git a/UnitTests/EVReflectionTests/TestObjects.swift b/UnitTests/EVReflectionTests/TestObjects.swift index c51b8a2e..96fd17f6 100755 --- a/UnitTests/EVReflectionTests/TestObjects.swift +++ b/UnitTests/EVReflectionTests/TestObjects.swift @@ -270,12 +270,8 @@ class A81b: EVObject { // Failure should be fixed for https://github.com/evermeer/EVReflection/issues/212 override func propertyConverters() -> [(key: String, decodeConverter: ((Any?) -> ()), encodeConverter: (() -> Any?))] { return [(key: "array", - decodeConverter: { - self.array = (($0 as? NSArray)?.map { - (($0 as? NSArray)?.map { - A81(dictionary: ($0 as? NSDictionary ?? NSDictionary())) - }) ?? [] - }) ?? [[]] + decodeConverter: { + self.array = ($0 as? NSArray)?.nestedArrayMap { A81(dictionary: $0) } ?? [[]] }, encodeConverter: { return self.array })] } } @@ -288,13 +284,22 @@ class A81c: EVObject { override func propertyConverters() -> [(key: String, decodeConverter: ((Any?) -> ()), encodeConverter: (() -> Any?))] { return [(key: "array", decodeConverter: { - self.array = (($0 as? NSArray)?.map { - (($0 as? NSArray)?.map { - (($0 as? NSArray)?.map { - A81(dictionary: ($0 as? NSDictionary ?? NSDictionary())) - }) ?? [] - }) ?? [[]] - }) ?? [[[]]] + self.array = ($0 as? NSArray)?.doubleNestedArrayMap { A81(dictionary: $0) } ?? [[]] + }, encodeConverter: { return self.array })] + } +} + +class A81d: EVObject { + // Skipping the triple, quadruple, quintuple nestedArrayMap and go strait to the sextuple + // You should not want something like this + var array: [[[[[[[A81]]]]]]] = [[[[[[[]]]]]]] + + // Now only working using this workaround + // Failure should be fixed for https://github.com/evermeer/EVReflection/issues/212 + override func propertyConverters() -> [(key: String, decodeConverter: ((Any?) -> ()), encodeConverter: (() -> Any?))] { + return [(key: "array", + decodeConverter: { + self.array = ($0 as? NSArray)?.sextupleNestedArrayMap { A81(dictionary: $0) } ?? [[[[[[]]]]]] }, encodeConverter: { return self.array })] } }