Skip to content

Commit

Permalink
changed to allowInvalidElements parameter convention
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Schumacher authored and Peter Schumacher committed Jun 13, 2016
1 parent 9035fa3 commit 69f394f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
23 changes: 9 additions & 14 deletions Sources/Unbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -407,23 +407,18 @@ public class Unboxer {
return try Unboxer(dictionary: dictionary, context: context).performCustomUnboxingWithClosure(closure)
}

/// Perform custom unboxing on an array of dictionaries, executing a closure with a new Unboxer for each one, discarding elements producing errors
public static func performFlattenedCustomUnboxingWithArray<T>(array: [UnboxableDictionary], context: Any? = nil, closure: Unboxer throws -> T?) -> [T] {
return array.flatMap { (dictionary) -> T? in
return try? self.performCustomUnboxingWithDictionary(dictionary, context: context, closure: closure)
}
}

/// 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
42 changes: 23 additions & 19 deletions Tests/UnboxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@ class UnboxTests: XCTestCase {
}
}

func testFlattenedCustomUnboxingFromArrayWithMultipleClasses() {
func testCustomUnboxingFromArrayWithMultipleClassesAndAllowedInvalid() {
struct ModelA {
let int: Int
}
Expand All @@ -927,26 +927,30 @@ class UnboxTests: XCTestCase {
"WrongKey" : "hello"
]
]

let unboxed: [Any] = Unboxer.performFlattenedCustomUnboxingWithArray(array, 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()
}
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
})

return nil
})

XCTAssertEqual((unboxed.first as! ModelA).int, 22)
XCTAssertTrue(unboxed.count == 1)
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 69f394f

Please sign in to comment.