Skip to content

Commit

Permalink
nested array workaround helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
evermeer committed Apr 7, 2018
1 parent f27f302 commit 42d6132
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 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.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
Expand Down
Binary file not shown.
44 changes: 44 additions & 0 deletions Source/EVArrayExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,47 @@ public extension Array where Element: NSDictionary {
}

}

public extension NSArray {
func nestedArrayMap<T>(_ element: (NSDictionary)->T) -> [[T]] {
return (self.map {
(($0 as? NSArray)?.map {
element($0 as? NSDictionary ?? NSDictionary())
}) ?? []
})
}

func doubleNestedArrayMap<T>(_ element: (NSDictionary)->T) -> [[[T]]] {
return (self.map {
(($0 as? NSArray)?.nestedArrayMap { element($0) }) ?? [[]]
})
}

func tripleNestedArrayMap<T>(_ element: (NSDictionary)->T) -> [[[[T]]]] {
return (self.map {
(($0 as? NSArray)?.doubleNestedArrayMap { element($0) }) ?? [[[]]]
})
}

func quadrupleNestedArrayMap<T>(_ element: (NSDictionary)->T) -> [[[[[T]]]]] {
return (self.map {
(($0 as? NSArray)?.tripleNestedArrayMap { element($0) }) ?? [[[[]]]]
})
}

func quintupleNestedArrayMap<T>(_ element: (NSDictionary)->T) -> [[[[[[T]]]]]] {
return (self.map {
(($0 as? NSArray)?.quadrupleNestedArrayMap { element($0) }) ?? [[[[[]]]]]
})
}

func sextupleNestedArrayMap<T>(_ 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.
}
15 changes: 15 additions & 0 deletions UnitTests/EVReflectionTests/EVReflectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
31 changes: 18 additions & 13 deletions UnitTests/EVReflectionTests/TestObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 })]
}
}
Expand All @@ -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 })]
}
}
Expand Down

0 comments on commit 42d6132

Please sign in to comment.