Skip to content

Commit

Permalink
Merge pull request JohnSundell#77 from PSchu/feature/Flattened-Custom…
Browse files Browse the repository at this point in the history
…-Array-Unboxing

Added flattened custom Array unboxing function
  • Loading branch information
JohnSundell authored Jun 18, 2016
2 parents 3a8664b + 69f394f commit a7736ce
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
16 changes: 9 additions & 7 deletions Sources/Unbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -408,15 +408,17 @@ public class Unboxer {
}

/// Perform custom unboxing on an array of dictionaries, executing a closure with a new Unboxer for each one, or throw an UnboxError
public static func performCustomUnboxingWithArray<T>(array: [UnboxableDictionary], context: Any? = nil, closure: Unboxer throws -> T?) throws -> [T] {
var unboxedArray = [T]()
public static func performCustomUnboxingWithArray<T>(array: [UnboxableDictionary], context: Any? = nil, allowInvalidElements: Bool = false, closure: Unboxer throws -> T?) throws -> [T] {

for dictionary in array {
let unboxed = try self.performCustomUnboxingWithDictionary(dictionary, context: context, closure: closure)
unboxedArray.append(unboxed)
if allowInvalidElements {
return array.flatMap { (dictionary) -> T? in
return try? self.performCustomUnboxingWithDictionary(dictionary, context: context, closure: closure)
}
} else {
return try array.map { (dictionary) -> T in
return try self.performCustomUnboxingWithDictionary(dictionary, context: context, closure: closure)
}
}

return unboxedArray
}

/// Perform custom unboxing using an Unboxer (created from NSData) passed to a closure, or throw an UnboxError
Expand Down
44 changes: 44 additions & 0 deletions Tests/UnboxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,50 @@ class UnboxTests: XCTestCase {
XCTFail("Unexpected error thrown: \(error)")
}
}

func testCustomUnboxingFromArrayWithMultipleClassesAndAllowedInvalid() {
struct ModelA {
let int: Int
}

struct ModelB {
let string: String
}

let array: [UnboxableDictionary] = [
[
"type" : "A",
"int" : 22
],
[
"type" : "B",
"WrongKey" : "hello"
]
]
do {
let unboxed: [Any] = try Unboxer.performCustomUnboxingWithArray(array, allowInvalidElements: true, closure: {
let unboxer = $0
let type = unboxer.unbox("type") as String

switch type {
case "A":
return ModelA(int: unboxer.unbox("int"))
case "B":
return ModelB(string: unboxer.unbox("string"))
default:
XCTFail()
}

return nil
})

XCTAssertEqual((unboxed.first as! ModelA).int, 22)
XCTAssertTrue(unboxed.count == 1)
} catch {
XCTFail("Unexpected error thrown: \(error)")
}
}

}

private func UnboxTestDictionaryWithAllRequiredKeysWithValidValues(nested: Bool) -> UnboxableDictionary {
Expand Down

0 comments on commit a7736ce

Please sign in to comment.