From 0a0b49e3e663dffb78232dde8b740ca58929b80e Mon Sep 17 00:00:00 2001 From: Clay Ellis Date: Mon, 25 Jul 2016 14:02:15 -0600 Subject: [PATCH] =?UTF-8?q?Added=20support=20for=20directly=20unboxing=20a?= =?UTF-8?q?=20model=20using=20keyPath=20Reordered=20the=20new=20Unbox=20me?= =?UTF-8?q?thods=20according=20to=20use=20cases=20Fixed=20tests=20?= =?UTF-8?q?=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/Unbox.swift | 14 +++++++------- Tests/UnboxTests.swift | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/Sources/Unbox.swift b/Sources/Unbox.swift index bed33a3..c0debdf 100644 --- a/Sources/Unbox.swift +++ b/Sources/Unbox.swift @@ -47,13 +47,6 @@ public func Unbox(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(dictionary: UnboxableDictionary, at key: String, isKeyPath: Bool = true) throws -> [T] { - let context = UnboxContainerContext(key: key, isKeyPath: isKeyPath) - let container: UnboxArrayContainer = 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(dictionaries: [UnboxableDictionary], context: Any? = nil, allowInvalidElements: Bool = false) throws -> [T] { return try dictionaries.mapAllowingInvalidElements(allowInvalidElements, transform: { @@ -61,6 +54,13 @@ public func Unbox(dictionaries: [UnboxableDictionary], context: An }) } +/// 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(dictionary: UnboxableDictionary, at key: String, isKeyPath: Bool = true) throws -> [T] { + let context = UnboxContainerContext(key: key, isKeyPath: isKeyPath) + let container: UnboxArrayContainer = 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(data: NSData, context: Any? = nil) throws -> T { return try Unboxer.unboxer(from: data, context: context).performUnboxing() diff --git a/Tests/UnboxTests.swift b/Tests/UnboxTests.swift index 7c894d4..6b90a9c 100644 --- a/Tests/UnboxTests.swift +++ b/Tests/UnboxTests.swift @@ -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 } @@ -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 }