Skip to content

Commit

Permalink
Created a static func that would allow for an Unboxer to be created s…
Browse files Browse the repository at this point in the history
…tarting at a specified key in an UnboxableDictionary that follows the same pattern as creating an Unboxer from NSData (Issue #73)

Updated the method signatures for unboxerFromData and unboxersFromData to follow Swift 3 syntax.
  • Loading branch information
clayellis committed Jul 20, 2016
1 parent b92074c commit 4802b93
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions Sources/Unbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public func Unbox<T: Unboxable>(dictionary: UnboxableDictionary, context: Any? =
return try Unboxer(dictionary: dictionary, context: context).performUnboxing()
}

/// Unbox a JSON dictionary into a model `T` beginning at a provided key, optionally using a contextual object. Throws `UnboxError`.
public func Unbox<T: Unboxable>(dictionary: UnboxableDictionary, at key: String, context: Any? = nil) throws -> T {
return try Unboxer.unboxer(at: key, in: dictionary, context: context).performUnboxing()
}

/// 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: {
Expand All @@ -49,12 +54,12 @@ public func Unbox<T: Unboxable>(dictionaries: [UnboxableDictionary], context: An

/// 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.unboxerFromData(data, context: context).performUnboxing()
return try Unboxer.unboxer(from: data, context: context).performUnboxing()
}

/// Unbox binary data into an array of `T`, optionally using a contextual object and/or invalid elements. Throws `UnboxError`.
public func Unbox<T: Unboxable>(data: NSData, context: Any? = nil, allowInvalidElements: Bool = false) throws -> [T] {
return try Unboxer.unboxersFromData(data, context: context).mapAllowingInvalidElements(allowInvalidElements, transform: {
return try Unboxer.unboxers(from: data, context: context).mapAllowingInvalidElements(allowInvalidElements, transform: {
return try $0.performUnboxing()
})
}
Expand All @@ -73,12 +78,12 @@ public func Unbox<T: UnboxableWithContext>(dictionaries: [UnboxableDictionary],

/// Unbox binary data into a model `T` using a required contextual object. Throws `UnboxError`.
public func Unbox<T: UnboxableWithContext>(data: NSData, context: T.ContextType) throws -> T {
return try Unboxer.unboxerFromData(data, context: context).performUnboxingWithContext(context)
return try Unboxer.unboxer(from: data, context: context).performUnboxingWithContext(context)
}

/// Unbox binary data into an array of `T` using a required contextual object and/or invalid elements. Throws `UnboxError`.
public func Unbox<T: UnboxableWithContext>(data: NSData, context: T.ContextType, allowInvalidElements: Bool = false) throws -> [T] {
return try Unboxer.unboxersFromData(data, context: context).mapAllowingInvalidElements(allowInvalidElements, transform: {
return try Unboxer.unboxers(from: data, context: context).mapAllowingInvalidElements(allowInvalidElements, transform: {
return try $0.performUnboxingWithContext(context)
})
}
Expand Down Expand Up @@ -423,7 +428,7 @@ public class Unboxer {

/// Perform custom unboxing using an Unboxer (created from NSData) passed to a closure, or throw an UnboxError
public static func performCustomUnboxingWithData<T>(data: NSData, context: Any? = nil, closure: Unboxer throws -> T?) throws -> T {
return try Unboxer.unboxerFromData(data, context: context).performCustomUnboxingWithClosure(closure)
return try Unboxer.unboxer(from: data, context: context).performCustomUnboxingWithClosure(closure)
}

// MARK: - Value accessing API
Expand Down Expand Up @@ -860,7 +865,7 @@ private extension UnboxableWithContext {
}

private extension Unboxer {
static func unboxerFromData(data: NSData, context: Any?) throws -> Unboxer {
static func unboxer(from data: NSData, context: Any?) throws -> Unboxer {
do {
guard let dictionary = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? UnboxableDictionary else {
throw UnboxError.InvalidData
Expand All @@ -872,7 +877,7 @@ private extension Unboxer {
}
}

static func unboxersFromData(data: NSData, context: Any?) throws -> [Unboxer] {
static func unboxers(from data: NSData, context: Any?) throws -> [Unboxer] {
do {
guard let array = try NSJSONSerialization.JSONObjectWithData(data, options: [.AllowFragments]) as? [UnboxableDictionary] else {
throw UnboxError.InvalidData
Expand All @@ -886,6 +891,14 @@ private extension Unboxer {
}
}

static func unboxer(at key: String, in dictionary: UnboxableDictionary, context: Any?) throws -> Unboxer {
guard let nestedDictionary = dictionary[key] as? UnboxableDictionary else {
throw UnboxValueError.MissingValueForKey(key)
}

return Unboxer(dictionary: nestedDictionary, context: context)
}

func performUnboxing<T: Unboxable>() throws -> T {
let unboxed = T(unboxer: self)
try self.throwIfFailed()
Expand Down

0 comments on commit 4802b93

Please sign in to comment.