From 3caf98f1bbcddf70971a702a5a587647a54946f0 Mon Sep 17 00:00:00 2001 From: Syo Ikeda Date: Mon, 23 Feb 2015 12:21:27 +0900 Subject: [PATCH 1/2] Use `success` and `failure` helper functions for Result creation --- APIKit/APIKit.swift | 8 ++++---- APIKit/RequestBodyBuilder.swift | 10 +++++----- APIKit/ResponseBodyParser.swift | 8 ++++---- APIKitTests/RequestBodyBuilderTests.swift | 4 ++-- APIKitTests/ResponseBodyParserTests.swift | 4 ++-- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/APIKit/APIKit.swift b/APIKit/APIKit.swift index 0fa764f0..69912fcb 100644 --- a/APIKit/APIKit.swift +++ b/APIKit/APIKit.swift @@ -97,15 +97,15 @@ public class API { switch self.responseBodyParser().parseData(data) { case .Failure(let box): - dispatch_async(mainQueue, { handler(.Failure(Box(box.unbox))) }) + dispatch_async(mainQueue, { handler(failure(box.unbox)) }) case .Success(let box): if let response = request.responseFromObject(box.unbox) { - dispatch_async(mainQueue, { handler(.Success(Box(response))) }) + dispatch_async(mainQueue, { handler(success(response)) }) } else { let userInfo = [NSLocalizedDescriptionKey: "failed to create model object from raw object."] let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo) - dispatch_async(mainQueue, { handler(.Failure(Box(error))) }) + dispatch_async(mainQueue, { handler(failure(error)) }) } } } @@ -114,7 +114,7 @@ public class API { } else { let userInfo = [NSLocalizedDescriptionKey: "failed to build request."] let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo) - dispatch_async(mainQueue, { handler(.Failure(Box(error))) }) + dispatch_async(mainQueue, { handler(failure(error)) }) } } } diff --git a/APIKit/RequestBodyBuilder.swift b/APIKit/RequestBodyBuilder.swift index d8bf0407..d07c75bf 100644 --- a/APIKit/RequestBodyBuilder.swift +++ b/APIKit/RequestBodyBuilder.swift @@ -32,23 +32,23 @@ public enum RequestBodyBuilder { if !NSJSONSerialization.isValidJSONObject(object) { let userInfo = [NSLocalizedDescriptionKey: "invalid object for JSON passed."] let error = NSError(domain: APIKitRequestBodyBuidlerErrorDomain, code: 0, userInfo: userInfo) - result = Result.Failure(Box(error)) + result = failure(error) break } var error: NSError? if let data = NSJSONSerialization.dataWithJSONObject(object, options: writingOptions, error: &error) { - result = Result.Success(Box(data)) + result = success(data) } else { - result = Result.Failure(Box(error!)) + result = failure(error!) } case .URL(let encoding): var error: NSError? if let data = URLEncodedSerialization.dataFromObject(object, encoding: encoding, error: &error) { - result = Result.Success(Box(data)) + result = success(data) } else { - result = Result.Failure(Box(error!)) + result = failure(error!) } case .Custom(let (_, buildBodyFromObject)): diff --git a/APIKit/ResponseBodyParser.swift b/APIKit/ResponseBodyParser.swift index a74977af..8cac087b 100644 --- a/APIKit/ResponseBodyParser.swift +++ b/APIKit/ResponseBodyParser.swift @@ -29,17 +29,17 @@ public enum ResponseBodyParser { case .JSON(let readingOptions): var error: NSError? if let object: AnyObject = NSJSONSerialization.JSONObjectWithData(data, options: readingOptions, error: &error) { - result = Result.Success(Box(object)) + result = success(object) } else { - result = Result.Failure(Box(error!)) + result = failure(error!) } case .URL(let encoding): var error: NSError? if let object: AnyObject = URLEncodedSerialization.objectFromData(data, encoding: encoding, error: &error) { - result = Result.Success(Box(object)) + result = success(object) } else { - result = Result.Failure(Box(error!)) + result = failure(error!) } case .Custom(let (accept, parseData)): diff --git a/APIKitTests/RequestBodyBuilderTests.swift b/APIKitTests/RequestBodyBuilderTests.swift index 1b44e79f..c4591bb3 100644 --- a/APIKitTests/RequestBodyBuilderTests.swift +++ b/APIKitTests/RequestBodyBuilderTests.swift @@ -60,7 +60,7 @@ class RequestBodyBuilderTests: XCTestCase { } func testCustomHeader() { - let builder = RequestBodyBuilder.Custom(contentTypeHeader: "foo", buildBodyFromObject: { o in Result.Success(Box(o as NSData)) }) + let builder = RequestBodyBuilder.Custom(contentTypeHeader: "foo", buildBodyFromObject: { o in success(o as NSData) }) XCTAssertEqual(builder.contentTypeHeader, "foo") } @@ -68,7 +68,7 @@ class RequestBodyBuilderTests: XCTestCase { let string = "foo" let expectedData = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)! let builder = RequestBodyBuilder.Custom(contentTypeHeader: "", buildBodyFromObject: { object in - return Result.Success(Box(expectedData)) + return success(expectedData) }) switch builder.buildBodyFromObject(string) { diff --git a/APIKitTests/ResponseBodyParserTests.swift b/APIKitTests/ResponseBodyParserTests.swift index ec2399c6..48f43079 100644 --- a/APIKitTests/ResponseBodyParserTests.swift +++ b/APIKitTests/ResponseBodyParserTests.swift @@ -65,7 +65,7 @@ class ResponseBodyParserTests: XCTestCase { } func testCustomAcceptHeader() { - let parser = ResponseBodyParser.Custom(acceptHeader: "foo", parseData: { d in Result.Success(Box(d)) }) + let parser = ResponseBodyParser.Custom(acceptHeader: "foo", parseData: { d in success(d) }) XCTAssertEqual(parser.acceptHeader, "foo") } @@ -73,7 +73,7 @@ class ResponseBodyParserTests: XCTestCase { let expectedDictionary = ["foo": 1] let data = NSData() let parser = ResponseBodyParser.Custom(acceptHeader: "", parseData: { data in - return Result.Success(Box(expectedDictionary)) + return success(expectedDictionary) }) switch parser.parseData(data) { From c3735c0fffd9aab4224cf48816e5a4b05d9c72df Mon Sep 17 00:00:00 2001 From: Syo Ikeda Date: Mon, 23 Feb 2015 12:59:54 +0900 Subject: [PATCH 2/2] Use `try` function and `Result.flatMap` --- APIKit/APIKit.swift | 16 +++++++--------- APIKit/RequestBodyBuilder.swift | 16 +++++----------- APIKit/ResponseBodyParser.swift | 14 ++++---------- APIKit/URLEncodedSerialization.swift | 8 ++++---- 4 files changed, 20 insertions(+), 34 deletions(-) diff --git a/APIKit/APIKit.swift b/APIKit/APIKit.swift index 69912fcb..331bcb1a 100644 --- a/APIKit/APIKit.swift +++ b/APIKit/APIKit.swift @@ -94,20 +94,18 @@ public class API { dispatch_async(mainQueue, { handler(.Failure(Box(error))) }) return } - - switch self.responseBodyParser().parseData(data) { - case .Failure(let box): - dispatch_async(mainQueue, { handler(failure(box.unbox)) }) - - case .Success(let box): - if let response = request.responseFromObject(box.unbox) { - dispatch_async(mainQueue, { handler(success(response)) }) + + let mappedResponse: Result = self.responseBodyParser().parseData(data).flatMap { rawResponse in + if let response = request.responseFromObject(rawResponse) { + return success(response) } else { let userInfo = [NSLocalizedDescriptionKey: "failed to create model object from raw object."] let error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo) - dispatch_async(mainQueue, { handler(failure(error)) }) + return failure(error) } + } + dispatch_async(mainQueue, { handler(mappedResponse) }) } task.resume() diff --git a/APIKit/RequestBodyBuilder.swift b/APIKit/RequestBodyBuilder.swift index d07c75bf..f40cb808 100644 --- a/APIKit/RequestBodyBuilder.swift +++ b/APIKit/RequestBodyBuilder.swift @@ -35,20 +35,14 @@ public enum RequestBodyBuilder { result = failure(error) break } - - var error: NSError? - if let data = NSJSONSerialization.dataWithJSONObject(object, options: writingOptions, error: &error) { - result = success(data) - } else { - result = failure(error!) + + result = try { error in + return NSJSONSerialization.dataWithJSONObject(object, options: writingOptions, error: error) } case .URL(let encoding): - var error: NSError? - if let data = URLEncodedSerialization.dataFromObject(object, encoding: encoding, error: &error) { - result = success(data) - } else { - result = failure(error!) + result = try { error in + return URLEncodedSerialization.dataFromObject(object, encoding: encoding, error: error) } case .Custom(let (_, buildBodyFromObject)): diff --git a/APIKit/ResponseBodyParser.swift b/APIKit/ResponseBodyParser.swift index 8cac087b..420d4e04 100644 --- a/APIKit/ResponseBodyParser.swift +++ b/APIKit/ResponseBodyParser.swift @@ -27,19 +27,13 @@ public enum ResponseBodyParser { switch self { case .JSON(let readingOptions): - var error: NSError? - if let object: AnyObject = NSJSONSerialization.JSONObjectWithData(data, options: readingOptions, error: &error) { - result = success(object) - } else { - result = failure(error!) + result = try { error in + return NSJSONSerialization.JSONObjectWithData(data, options: readingOptions, error: error) } case .URL(let encoding): - var error: NSError? - if let object: AnyObject = URLEncodedSerialization.objectFromData(data, encoding: encoding, error: &error) { - result = success(object) - } else { - result = failure(error!) + result = try { error in + return URLEncodedSerialization.objectFromData(data, encoding: encoding, error: error) } case .Custom(let (accept, parseData)): diff --git a/APIKit/URLEncodedSerialization.swift b/APIKit/URLEncodedSerialization.swift index 1fb39c39..4be61177 100644 --- a/APIKit/URLEncodedSerialization.swift +++ b/APIKit/URLEncodedSerialization.swift @@ -9,7 +9,7 @@ private func unescape(string: String) -> String { } public class URLEncodedSerialization { - public class func objectFromData(data: NSData, encoding: NSStringEncoding, inout error: NSError?) -> AnyObject? { + public class func objectFromData(data: NSData, encoding: NSStringEncoding, error: NSErrorPointer) -> AnyObject? { var dictionary: [String: AnyObject]? if let string = NSString(data: data, encoding: encoding) as? String { @@ -26,19 +26,19 @@ public class URLEncodedSerialization { if dictionary == nil { let userInfo = [NSLocalizedDescriptionKey: "failed to decode urlencoded string."] - error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo) + error.memory = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo) } return dictionary } - public class func dataFromObject(object: AnyObject, encoding: NSStringEncoding, inout error: NSError?) -> NSData? { + public class func dataFromObject(object: AnyObject, encoding: NSStringEncoding, error: NSErrorPointer) -> NSData? { let string = stringFromObject(object, encoding: encoding) let data = string.dataUsingEncoding(encoding, allowLossyConversion: false) if data == nil { let userInfo = [NSLocalizedDescriptionKey: "failed to decode urlencoded string."] - error = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo) + error.memory = NSError(domain: APIKitErrorDomain, code: 0, userInfo: userInfo) } return data