Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename closure functions #104

Merged
merged 1 commit into from
Jul 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Generator/Generator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ indirect enum Decodable {
// case .AnyObject:
// return "{$0}"
case Optional(let T):
return "catchNull(\(T.decodeClosure(provider)))"
return "optional(\(T.decodeClosure(provider)))"
case Array(let T):
return "decodeArray(\(T.decodeClosure(provider)))"
return "array(\(T.decodeClosure(provider)))"
case .Dictionary(let K, let T):
return "decodeDictionary(\(K.decodeClosure(provider)), elementDecodeClosure: \(T.decodeClosure(provider)))"
return "dictionary(key: \(K.decodeClosure(provider)), value: \(T.decodeClosure(provider)))"
}
}

Expand Down
15 changes: 10 additions & 5 deletions Sources/Closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

import Foundation

func catchNull<T>(_ decodeClosure: (AnyObject) throws -> T) -> (AnyObject) throws -> T? {
/// Takes a decode closure and returns one that returns nil if the json object is `NSNull`
func optional<T>(_ decodeClosure: (AnyObject) throws -> T) -> (AnyObject) throws -> T? {
return { json in
if json is NSNull {
return nil
Expand All @@ -18,15 +19,19 @@ func catchNull<T>(_ decodeClosure: (AnyObject) throws -> T) -> (AnyObject) throw
}
}

/// Designed to be used with parse(json, path, decodeClosure) as the decodeClosure. Thats why it's curried and a "top-level" function instead of a function in an array extension. For everyday use, prefer using [T].decode(json) instead.
public func decodeArray<T>(_ elementDecodeClosure: (AnyObject) throws -> T) -> (json: AnyObject) throws -> [T] {
/// Create an array-decode-closure from an element decode closure
///
/// - returns: A closure that takes an `NSArray` and maps it using the element decode closure
public func array<T>(_ elementDecodeClosure: (AnyObject) throws -> T) -> (json: AnyObject) throws -> [T] {
return { json in
return try NSArray.decode(json).map { try elementDecodeClosure($0) }
}
}

/// Designed to be used with parse(json, path, decodeClosure) as the decodeClosure. Thats why it's curried. For everyday use, prefer using [K: V].decode(json) instead (declared in Decodable.swift).
public func decodeDictionary<K,V>(_ keyDecodeClosure: (AnyObject) throws -> K, elementDecodeClosure: (AnyObject) throws -> V) -> (json: AnyObject) throws -> [K: V] {
/// Create an dictionary-decode-closure from key- and value- decode closures
///
/// - returns: A closure that takes a `NSDictionary` and "maps" it using key and value decode closures
public func dictionary<K,V>(key keyDecodeClosure: (AnyObject) throws -> K, value elementDecodeClosure: (AnyObject) throws -> V) -> (json: AnyObject) throws -> [K: V] {
return { json in
var dict = [K: V]()
for (key, value) in try NSDictionary.decode(json) {
Expand Down
6 changes: 3 additions & 3 deletions Sources/Decodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ extension NSArray {

extension Dictionary where Key: Decodable, Value: Decodable {
public static func decode(_ j: AnyObject) throws -> Dictionary {
return try decodeDictionary(Key.decode, elementDecodeClosure: Value.decode)(json: j)
return try dictionary(key: Key.decode, value: Value.decode)(json: j)
}
}

extension Array where Element: Decodable {
public static func decode(_ j: AnyObject, ignoreInvalidObjects: Bool = false) throws -> [Element] {
if ignoreInvalidObjects {
return try decodeArray { try? Element.decode($0) }(json: j).flatMap {$0}
return try array { try? Element.decode($0) }(json: j).flatMap {$0}
} else {
return try decodeArray(Element.decode)(json: j)
return try array(Element.decode)(json: j)
}
}
}
Expand Down
Loading