Skip to content

Commit

Permalink
Added support for directly unboxing a model using keyPath
Browse files Browse the repository at this point in the history
Reordered the new Unbox methods according to use cases
Fixed tests 🚀
  • Loading branch information
clayellis committed Jul 25, 2016
1 parent acd997f commit 0a0b49e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
14 changes: 7 additions & 7 deletions Sources/Unbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,20 @@ public func Unbox<T: Unboxable>(dictionary: UnboxableDictionary, at key: String,
return container.model
}

/// Unbox an array JSON dictionary into an array of model `T` beginning at a provided key, optionally using a contextual object and/or invalid elements. Throws `UnboxError`.
public func Unbox<T: Unboxable>(dictionary: UnboxableDictionary, at key: String, isKeyPath: Bool = true) throws -> [T] {
let context = UnboxContainerContext(key: key, isKeyPath: isKeyPath)
let container: UnboxArrayContainer<T> = try Unbox(dictionary, context: context)
return container.models
}

/// Unbox an array of JSON dictionaries into an array of `T`, optionally using a contextual object and/or invalid elements. Throws `UnboxError`.
public func Unbox<T: Unboxable>(dictionaries: [UnboxableDictionary], context: Any? = nil, allowInvalidElements: Bool = false) throws -> [T] {
return try dictionaries.mapAllowingInvalidElements(allowInvalidElements, transform: {
try Unbox($0, context: context)
})
}

/// Unbox an array JSON dictionary into an array of model `T` beginning at a provided key, optionally using a contextual object and/or invalid elements. Throws `UnboxError`.
public func Unbox<T: Unboxable>(dictionary: UnboxableDictionary, at key: String, isKeyPath: Bool = true) throws -> [T] {
let context = UnboxContainerContext(key: key, isKeyPath: isKeyPath)
let container: UnboxArrayContainer<T> = try Unbox(dictionary, context: context)
return container.models
}

/// Unbox binary data into a model `T`, optionally using a contextual object. Throws `UnboxError`.
public func Unbox<T: Unboxable>(data: NSData, context: Any? = nil) throws -> T {
return try Unboxer.unboxer(from: data, context: context).performUnboxing()
Expand Down
8 changes: 4 additions & 4 deletions Tests/UnboxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1234,8 +1234,8 @@ class UnboxTests: XCTestCase {
]

do {
let unboxed: UnboxTestSimpleMock = try Unbox(dictionary, at: "B")
XCTAssertEqual(unboxed.int, 14)
let _ : UnboxTestSimpleMock = try Unbox(dictionary, at: "B")
XCTFail()
} catch {
// Test Passed
}
Expand Down Expand Up @@ -1266,8 +1266,8 @@ class UnboxTests: XCTestCase {
]

do {
let unboxed: UnboxTestSimpleMock = try Unbox(dictionary, at: "A.B", isKeyPath: true)
XCTAssertEqual(unboxed.int, 14)
let _: UnboxTestSimpleMock = try Unbox(dictionary, at: "A.B", isKeyPath: true)
XCTFail()
} catch {
// Test Passed
}
Expand Down

0 comments on commit 0a0b49e

Please sign in to comment.