Skip to content

Commit

Permalink
Add some poorly written documentation-comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Anviking committed Aug 10, 2015
1 parent 37bc358 commit dc546c7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
38 changes: 25 additions & 13 deletions Decodable/Operators.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@

import Foundation

// MARK: - Operators

infix operator => { associativity right precedence 150 }
infix operator =>? { associativity right precedence 150 }

// MARK: - Helpers

private func parse(object: AnyObject, key: String) throws -> AnyObject {
let dict = try NSDictionary.decode(object)
guard let result = dict[key] else {
Expand Down Expand Up @@ -40,17 +44,19 @@ private func catchErrorAndSetRootObject<T>(object: AnyObject, block: (Void throw
}
}

// MARK: Operators
// MARK: - Operator Overloads

// MARK: Default

// Middle
/// Middle-overload for chained expression, returning <T: Decodable>.
public func => <T: Decodable>(lhs: String, rhs: ((AnyObject) throws -> T)) -> ((AnyObject) throws -> T)
{
return catchErrorAndAppendPath(lhs) { (obj: AnyObject) in
return try rhs(parse(obj, key: lhs))
}
}

// End
/// At-end-overload for chained expression, returning <T: Decodable>.
public func => <T: Decodable>(lhs: String, key: String) -> ((AnyObject) throws -> T)
{
return lhs => { obj in
Expand All @@ -64,13 +70,13 @@ public func => <T: Decodable>(lhs: String, key: String) -> ((AnyObject) throws -
}
}

// MARK: Beginning

/// Beginning-overload for chained expression, returning <T>.
public func => <T>(lhs: AnyObject, rhs: ((AnyObject) throws -> T)) throws -> T
{
return try rhs(lhs)
}

/// Beginning-overload for a non-nested keypath, returning <T: Decodable>.
public func => <T: Decodable>(lhs: AnyObject, rhs: String) throws -> T
{
return try catchErrorAndSetRootObject(lhs) {
Expand All @@ -80,6 +86,7 @@ public func => <T: Decodable>(lhs: AnyObject, rhs: String) throws -> T

// MARK: Optionals

/// Optional-overload (beginning) for no-nested keypath, returning <T: Decodable>?
public func => <T: Decodable>(lhs: AnyObject, rhs: String) -> T?
{
do {
Expand All @@ -89,6 +96,7 @@ public func => <T: Decodable>(lhs: AnyObject, rhs: String) -> T?
}
}

/// Optional-overload (beginning) for nested keypath, returning <T: Decodable>?
public func => <T>(lhs: AnyObject, rhs: ((AnyObject) throws -> T)) -> T?
{
do {
Expand All @@ -98,27 +106,35 @@ public func => <T>(lhs: AnyObject, rhs: ((AnyObject) throws -> T)) -> T?
}
}

// MARK: No inffered type
// MARK: No inferred type

// If there is no inferred type, return NSDictionary so that chaining works
// E.g in let a: Int = json => "key1" => "key2" => "key3"
// the last two operators should "return" (vauge term, since it's probably a bit more complicated)
// a NSDictionary


/// NSDictionary overload (beginning) for no-nested keypath
public func => (lhs: AnyObject, rhs: String) throws -> NSDictionary
{
return try NSDictionary.decode(parse(lhs, key: rhs))
}

/// NSDictionary overload (beginning) for nested keypath
public func => (lhs: AnyObject, rhs: ((AnyObject) throws -> NSDictionary)) throws -> NSDictionary
{
return try NSDictionary.decode(rhs(lhs))
}

// Middle
/// NSDictionary overload (middle) for nested keypath
public func => (lhs: String, rhs: ((AnyObject) throws -> NSDictionary)) -> ((AnyObject) throws -> NSDictionary)
{
return catchErrorAndAppendPath(lhs) { (obj: AnyObject) in
return try rhs(parse(obj, key: lhs))
}
}

// End
/// NSDictionary overload (end) for nested keypath
public func => (lhs: String, key: String) -> ((AnyObject) throws -> NSDictionary)
{
return lhs => { obj in
Expand All @@ -130,8 +146,4 @@ public func => (lhs: String, key: String) -> ((AnyObject) throws -> NSDictionary
throw error
}
}
}

private func printArrayError(error: DecodingError) {
print("Error caught in nil-filtering Array Decoder (=>?): \(error)")
}
}
10 changes: 10 additions & 0 deletions DecodableTests/DecodableOperatorsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ class DecodableOperatorsTests: XCTestCase {
// then
XCTAssertEqual(result, value)
}

func testDecodeNestedIntSuccess() {
// given
let value = 4
let dictionary: NSDictionary = ["key1": ["key2": ["key3": value]]]
// when
let result: Int = try! dictionary => "key1" => "key2" => "key3"
// then
XCTAssertEqual(result, value)
}


func testDecodeNestedDictionaryCastingSuccess() {
Expand Down

0 comments on commit dc546c7

Please sign in to comment.