From 8ca53e228047ecbf480f0289e8357f5b8bd53dfe Mon Sep 17 00:00:00 2001 From: Yosuke Ishikawa Date: Wed, 21 Sep 2016 17:09:18 +0900 Subject: [PATCH] Initial Swift 3 support --- Cartfile.resolved | 4 +- Carthage/Checkouts/APIKit | 2 +- Carthage/Checkouts/Result | 2 +- .../Example/BatchRequestViewController.swift | 4 +- .../Example/SingleRequestViewController.swift | 4 +- JSONRPCKit.xcodeproj/project.pbxproj | 8 +- JSONRPCKit/BatchElementType.swift | 75 +++++----- JSONRPCKit/BatchFactory.swift | 20 +-- JSONRPCKit/BatchType.swift | 54 +++---- JSONRPCKit/Id.swift | 28 ++-- JSONRPCKit/JSONRPCError.swift | 39 ++--- JSONRPCKit/NumberIdGenerator.swift | 4 +- JSONRPCKit/RequestType.swift | 12 +- Tests/BatchElementTests.swift | 140 +++++++++--------- Tests/BatchFactoryTests.swift | 6 +- Tests/TestRequest.swift | 20 +-- 16 files changed, 217 insertions(+), 205 deletions(-) diff --git a/Cartfile.resolved b/Cartfile.resolved index b4d3c30..3a3c303 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,2 +1,2 @@ -github "antitypical/Result" "2.1.3" -github "ishkawa/APIKit" "2.0.5" +github "antitypical/Result" "3.0.0" +github "ishkawa/APIKit" "3.0.0-beta.1" diff --git a/Carthage/Checkouts/APIKit b/Carthage/Checkouts/APIKit index ee42834..2296315 160000 --- a/Carthage/Checkouts/APIKit +++ b/Carthage/Checkouts/APIKit @@ -1 +1 @@ -Subproject commit ee42834e908c893638e3f4ed2eb7fc9c7f164145 +Subproject commit 22963156dd462134a5029a586e5af94879d2f1d6 diff --git a/Carthage/Checkouts/Result b/Carthage/Checkouts/Result index 9b5e373..df233a8 160000 --- a/Carthage/Checkouts/Result +++ b/Carthage/Checkouts/Result @@ -1 +1 @@ -Subproject commit 9b5e373891dfe0de11ba2a8e9a5cafd570b858c4 +Subproject commit df233a8d23a545153d5b5de13b1ac8db2503f088 diff --git a/Example/Example/BatchRequestViewController.swift b/Example/Example/BatchRequestViewController.swift index c373573..3a6851c 100644 --- a/Example/Example/BatchRequestViewController.swift +++ b/Example/Example/BatchRequestViewController.swift @@ -35,11 +35,11 @@ class BatchRequestViewController: UIViewController { Session.sendRequest(httpRequest) { [weak self] result in switch result { - case .Success(let subtractAnswer, let multiplyAnswer): + case .success(let subtractAnswer, let multiplyAnswer): self?.subtractAnswerLabel.text = "\(subtractAnswer)" self?.multiplyAnswerLabel.text = "\(multiplyAnswer)" - case .Failure(let error): + case .failure(let error): self?.subtractAnswerLabel.text = "?" self?.multiplyAnswerLabel.text = "?" self?.showAlertWithError(error) diff --git a/Example/Example/SingleRequestViewController.swift b/Example/Example/SingleRequestViewController.swift index 1be18b2..e4fe386 100644 --- a/Example/Example/SingleRequestViewController.swift +++ b/Example/Example/SingleRequestViewController.swift @@ -42,10 +42,10 @@ class SingleRequestViewController: UIViewController { Session.sendRequest(httpRequest) { [weak self] result in switch result { - case .Success(let answer): + case .success(let answer): self?.subtractAnswerLabel.text = "\(answer)" - case .Failure(let error): + case .failure(let error): self?.subtractAnswerLabel.text = "?" self?.showAlertWithError(error) } diff --git a/JSONRPCKit.xcodeproj/project.pbxproj b/JSONRPCKit.xcodeproj/project.pbxproj index c4c2d6d..6f1c043 100644 --- a/JSONRPCKit.xcodeproj/project.pbxproj +++ b/JSONRPCKit.xcodeproj/project.pbxproj @@ -282,7 +282,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "-.Tests"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -295,7 +295,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "-.Tests"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; }; @@ -409,7 +409,7 @@ PRODUCT_NAME = "$(PROJECT_NAME)"; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -428,7 +428,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.bricklife.ios.JSONRPCKit; PRODUCT_NAME = "$(PROJECT_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; }; diff --git a/JSONRPCKit/BatchElementType.swift b/JSONRPCKit/BatchElementType.swift index 58584f8..4656a83 100644 --- a/JSONRPCKit/BatchElementType.swift +++ b/JSONRPCKit/BatchElementType.swift @@ -15,74 +15,79 @@ public protocol BatchElementType { var request: Request { get } var version: String { get } var id: Id? { get } - var body: AnyObject { get } + var body: Any { get } - func responseFromObject(object: AnyObject) throws -> Request.Response - func responseFromBatchObjects(objects: [AnyObject]) throws -> Request.Response + func responseFromObject(_ object: Any) throws -> Request.Response + func responseFromBatchObjects(_ objects: [Any]) throws -> Request.Response - func resultFromObject(object: AnyObject) -> Result - func resultFromBatchObjects(objects: [AnyObject]) -> Result + func resultFromObject(_ object: Any) -> Result + func resultFromBatchObjects(_ objects: [Any]) -> Result } public extension BatchElementType { /// - Throws: JSONRPCError - public func responseFromObject(object: AnyObject) throws -> Request.Response { + public func responseFromObject(_ object: Any) throws -> Request.Response { switch resultFromObject(object) { - case .Success(let response): + case .success(let response): return response - case .Failure(let error): + case .failure(let error): throw error } } /// - Throws: JSONRPCError - public func responseFromBatchObjects(objects: [AnyObject]) throws -> Request.Response { + public func responseFromBatchObjects(_ objects: [Any]) throws -> Request.Response { switch resultFromBatchObjects(objects) { - case .Success(let response): + case .success(let response): return response - case .Failure(let error): + case .failure(let error): throw error } } - public func resultFromObject(object: AnyObject) -> Result { - let receivedVersion = object["jsonrpc"] as? String + public func resultFromObject(_ object: Any) -> Result { + guard let dictionary = object as? [String: Any] else { + fatalError("FIXME") + } + + let receivedVersion = dictionary["jsonrpc"] as? String guard version == receivedVersion else { - return .Failure(.UnsupportedVersion(receivedVersion)) + return .failure(.unsupportedVersion(receivedVersion)) } - guard id == object["id"].flatMap(Id.init) else { - return .Failure(.ResponseNotFound(requestId: id, object: object)) + guard id == dictionary["id"].flatMap(Id.init) else { + return .failure(.responseNotFound(requestId: id, object: dictionary)) } - let resultObject: AnyObject? = object["result"] - let errorObject: AnyObject? = object["error"] + let resultObject = dictionary["result"] + let errorObject = dictionary["error"] switch (resultObject, errorObject) { case (nil, let errorObject?): - return .Failure(JSONRPCError(errorObject: errorObject)) + return .failure(JSONRPCError(errorObject: errorObject)) case (let resultObject?, nil): do { - return .Success(try request.responseFromResultObject(resultObject)) + return .success(try request.responseFromResultObject(resultObject)) } catch { - return .Failure(.ResultObjectParseError(error)) + return .failure(.resultObjectParseError(error)) } default: - return .Failure(.MissingBothResultAndError(object)) + return .failure(.missingBothResultAndError(dictionary)) } } - public func resultFromBatchObjects(objects: [AnyObject]) -> Result { + public func resultFromBatchObjects(_ objects: [Any]) -> Result { let matchedObject = objects + .flatMap { $0 as? [String: Any] } .filter { $0["id"].flatMap(Id.init) == id } .first guard let object = matchedObject else { - return .Failure(.ResponseNotFound(requestId: id, object: objects)) + return .failure(.responseNotFound(requestId: id, object: objects)) } return resultFromObject(object) @@ -90,20 +95,20 @@ public extension BatchElementType { } public extension BatchElementType where Request.Response == Void { - public func responseFromObject(object: AnyObject) throws -> Request.Response { + public func responseFromObject(_ object: Any) throws -> Request.Response { return () } - public func responseFromBatchObjects(objects: [AnyObject]) throws -> Request.Response { + public func responseFromBatchObjects(_ objects: [Any]) throws -> Request.Response { return () } - public func resultFromObject(object: AnyObject) -> Result { - return .Success() + public func resultFromObject(_ object: Any) -> Result { + return .success() } - public func resultFromBatchObjects(objects: [AnyObject]) -> Result { - return .Success() + public func resultFromBatchObjects(_ objects: [Any]) -> Result { + return .success() } } @@ -111,13 +116,13 @@ public struct BatchElement: BatchElementType { public let request: Request public let version: String public let id: Id? - public let body: AnyObject + public let body: Any public init(request: Request, version: String, id: Id) { let id: Id? = request.isNotification ? nil : id - var body: [String: AnyObject] = [ - "jsonrpc": version, - "method": request.method, + var body: [String: Any] = [ + "jsonrpc": version as Any, + "method": request.method as Any, ] if let id = id { @@ -135,7 +140,7 @@ public struct BatchElement: BatchElementType { self.request = request self.version = version self.id = id - self.body = body + self.body = body as Any } } diff --git a/JSONRPCKit/BatchFactory.swift b/JSONRPCKit/BatchFactory.swift index 9260684..17d9f1a 100644 --- a/JSONRPCKit/BatchFactory.swift +++ b/JSONRPCKit/BatchFactory.swift @@ -13,36 +13,36 @@ public final class BatchFactory { public let version: String public var idGenerator: IdGeneratorType - private let semaphore = dispatch_semaphore_create(1) + fileprivate let semaphore = DispatchSemaphore(value: 1) public init(version: String = "2.0", idGenerator: IdGeneratorType = NumberIdGenerator()) { self.version = version self.idGenerator = idGenerator } - public func create(request: Request) -> Batch { - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) + public func create(_ request: Request) -> Batch { + _ = semaphore.wait(timeout: DispatchTime.distantFuture) let batchElement = BatchElement(request: request, version: version, id: idGenerator.next()) - dispatch_semaphore_signal(semaphore) + semaphore.signal() return Batch(batchElement: batchElement) } - public func create(request1: Request1, _ request2: Request2) -> Batch2 { - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) + public func create(_ request1: Request1, _ request2: Request2) -> Batch2 { + _ = semaphore.wait(timeout: DispatchTime.distantFuture) let batchElement1 = BatchElement(request: request1, version: version, id: idGenerator.next()) let batchElement2 = BatchElement(request: request2, version: version, id: idGenerator.next()) - dispatch_semaphore_signal(semaphore) + semaphore.signal() return Batch2(batchElement1: batchElement1, batchElement2: batchElement2) } - public func create(request1: Request1, _ request2: Request2, _ request3: Request3) -> Batch3 { - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER) + public func create(_ request1: Request1, _ request2: Request2, _ request3: Request3) -> Batch3 { + _ = semaphore.wait(timeout: DispatchTime.distantFuture) let batchElement1 = BatchElement(request: request1, version: version, id: idGenerator.next()) let batchElement2 = BatchElement(request: request2, version: version, id: idGenerator.next()) let batchElement3 = BatchElement(request: request3, version: version, id: idGenerator.next()) - dispatch_semaphore_signal(semaphore) + semaphore.signal() return Batch3(batchElement1: batchElement1, batchElement2: batchElement2, batchElement3: batchElement3) } diff --git a/JSONRPCKit/BatchType.swift b/JSONRPCKit/BatchType.swift index b961bbb..147d542 100644 --- a/JSONRPCKit/BatchType.swift +++ b/JSONRPCKit/BatchType.swift @@ -13,12 +13,12 @@ public protocol BatchType { associatedtype Responses associatedtype Results - var requestObject: AnyObject { get } + var requestObject: Any { get } - func responsesFromObject(object: AnyObject) throws -> Responses - func resultsFromObject(object: AnyObject) -> Results + func responsesFromObject(_ object: Any) throws -> Responses + func resultsFromObject(_ object: Any) -> Results - static func responsesFromResults(results: Results) throws -> Responses + static func responsesFromResults(_ results: Results) throws -> Responses } public struct Batch: BatchType { @@ -27,19 +27,19 @@ public struct Batch: BatchType { public let batchElement: BatchElement - public var requestObject: AnyObject { + public var requestObject: Any { return batchElement.body } - public func responsesFromObject(object: AnyObject) throws -> Responses { + public func responsesFromObject(_ object: Any) throws -> Responses { return try batchElement.responseFromObject(object) } - public func resultsFromObject(object: AnyObject) -> Results { + public func resultsFromObject(_ object: Any) -> Results { return batchElement.resultFromObject(object) } - public static func responsesFromResults(results: Results) throws -> Responses { + public static func responsesFromResults(_ results: Results) throws -> Responses { return try results.dematerialize() } } @@ -51,16 +51,16 @@ public struct Batch2: BatchType { public let batchElement1: BatchElement public let batchElement2: BatchElement - public var requestObject: AnyObject { + public var requestObject: Any { return [ batchElement1.body, batchElement2.body, ] } - public func responsesFromObject(object: AnyObject) throws -> Responses { - guard let batchObjects = object as? [AnyObject] else { - throw JSONRPCError.NonArrayResponse(object) + public func responsesFromObject(_ object: Any) throws -> Responses { + guard let batchObjects = object as? [Any] else { + throw JSONRPCError.nonArrayResponse(object) } return ( @@ -69,11 +69,11 @@ public struct Batch2: BatchType { ) } - public func resultsFromObject(object: AnyObject) -> Results { - guard let batchObjects = object as? [AnyObject] else { + public func resultsFromObject(_ object: Any) -> Results { + guard let batchObjects = object as? [Any] else { return ( - .Failure(.NonArrayResponse(object)), - .Failure(.NonArrayResponse(object)) + .failure(.nonArrayResponse(object)), + .failure(.nonArrayResponse(object)) ) } @@ -83,7 +83,7 @@ public struct Batch2: BatchType { ) } - public static func responsesFromResults(results: Results) throws -> Responses { + public static func responsesFromResults(_ results: Results) throws -> Responses { return ( try results.0.dematerialize(), try results.1.dematerialize() @@ -99,7 +99,7 @@ public struct Batch3 public let batchElement3: BatchElement - public var requestObject: AnyObject { + public var requestObject: Any { return [ batchElement1.body, batchElement2.body, @@ -107,9 +107,9 @@ public struct Batch3 Responses { - guard let batchObjects = object as? [AnyObject] else { - throw JSONRPCError.NonArrayResponse(object) + public func responsesFromObject(_ object: Any) throws -> Responses { + guard let batchObjects = object as? [Any] else { + throw JSONRPCError.nonArrayResponse(object) } return ( @@ -119,12 +119,12 @@ public struct Batch3 Results { - guard let batchObjects = object as? [AnyObject] else { + public func resultsFromObject(_ object: Any) -> Results { + guard let batchObjects = object as? [Any] else { return ( - .Failure(.NonArrayResponse(object)), - .Failure(.NonArrayResponse(object)), - .Failure(.NonArrayResponse(object)) + .failure(.nonArrayResponse(object)), + .failure(.nonArrayResponse(object)), + .failure(.nonArrayResponse(object)) ) } @@ -135,7 +135,7 @@ public struct Batch3 Responses { + public static func responsesFromResults(_ results: Results) throws -> Responses { return ( try results.0.dematerialize(), try results.1.dematerialize(), diff --git a/JSONRPCKit/Id.swift b/JSONRPCKit/Id.swift index 76ea716..b742cbd 100644 --- a/JSONRPCKit/Id.swift +++ b/JSONRPCKit/Id.swift @@ -9,29 +9,29 @@ import Foundation public enum Id { - case Number(Int) - case String(Swift.String) + case number(Int) + case string(Swift.String) } extension Id { - public init?(value: AnyObject) { + public init?(value: Any) { switch value { case let number as Int: - self = .Number(number) + self = .number(number) case let string as Swift.String: - self = .String(string) + self = .string(string) default: return nil } } - public var value: AnyObject { + public var value: Any { switch self { - case Number(let number): - return number - case String(let string): - return string + case .number(let number): + return number as Any + case .string(let string): + return string as Any } } } @@ -40,20 +40,20 @@ extension Id: Hashable { public var hashValue: Int { switch self { - case Number(let number): + case .number(let number): return number - case String(let string): + case .string(let string): return string.hashValue } } } public func ==(lhs: Id, rhs: Id) -> Bool { - if case let (.Number(left), .Number(right)) = (lhs, rhs) { + if case let (.number(left), .number(right)) = (lhs, rhs) { return left == right } - if case let (.String(left), .String(right)) = (lhs, rhs) { + if case let (.string(left), .string(right)) = (lhs, rhs) { return left == right } diff --git a/JSONRPCKit/JSONRPCError.swift b/JSONRPCKit/JSONRPCError.swift index a6be070..364e058 100644 --- a/JSONRPCKit/JSONRPCError.swift +++ b/JSONRPCKit/JSONRPCError.swift @@ -8,32 +8,37 @@ import Foundation -public enum JSONRPCError: ErrorType { - case ResponseError(code: Int, message: String, data: AnyObject?) - case ResponseNotFound(requestId: Id?, object: AnyObject) - case ResultObjectParseError(ErrorType) - case ErrorObjectParseError(ErrorType) - case UnsupportedVersion(String?) - case MissingBothResultAndError(AnyObject) - case NonArrayResponse(AnyObject) +public enum JSONRPCError: Error { + case responseError(code: Int, message: String, data: Any?) + case responseNotFound(requestId: Id?, object: Any) + case resultObjectParseError(Error) + case errorObjectParseError(Error) + case unsupportedVersion(String?) + case missingBothResultAndError(Any) + case nonArrayResponse(Any) - public init(errorObject: AnyObject) { - enum ParseError: ErrorType { - case MissingKey(key: String, errorObject: AnyObject) + public init(errorObject: Any) { + enum ParseError: Error { + case nonDictionaryObject(object: Any) + case missingKey(key: String, errorObject: Any) } do { - guard let code = errorObject["code"] as? Int else { - throw ParseError.MissingKey(key: "code", errorObject: errorObject) + guard let dictionary = errorObject as? [String: Any] else { + throw ParseError.nonDictionaryObject(object: errorObject) + } + + guard let code = dictionary["code"] as? Int else { + throw ParseError.missingKey(key: "code", errorObject: errorObject) } - guard let message = errorObject["message"] as? String else { - throw ParseError.MissingKey(key: "message", errorObject: errorObject) + guard let message = dictionary["message"] as? String else { + throw ParseError.missingKey(key: "message", errorObject: errorObject) } - self = .ResponseError(code: code, message: message, data: errorObject["data"]) + self = .responseError(code: code, message: message, data: dictionary["data"]) } catch { - self = .ErrorObjectParseError(error) + self = .errorObjectParseError(error) } } } diff --git a/JSONRPCKit/NumberIdGenerator.swift b/JSONRPCKit/NumberIdGenerator.swift index 3d2b0a8..faf51b6 100644 --- a/JSONRPCKit/NumberIdGenerator.swift +++ b/JSONRPCKit/NumberIdGenerator.swift @@ -10,7 +10,7 @@ import Foundation public struct NumberIdGenerator: IdGeneratorType { - private var currentId = 1 + fileprivate var currentId = 1 public init() {} @@ -19,6 +19,6 @@ public struct NumberIdGenerator: IdGeneratorType { currentId += 1 } - return .Number(currentId) + return .number(currentId) } } diff --git a/JSONRPCKit/RequestType.swift b/JSONRPCKit/RequestType.swift index ae2d10e..285510e 100644 --- a/JSONRPCKit/RequestType.swift +++ b/JSONRPCKit/RequestType.swift @@ -13,19 +13,19 @@ public protocol RequestType { associatedtype Response var method: String { get } - var parameters: AnyObject? { get } - var extendedFields: [String: AnyObject]? { get } + var parameters: Any? { get } + var extendedFields: [String: Any]? { get } var isNotification: Bool { get } - func responseFromResultObject(resultObject: AnyObject) throws -> Response + func responseFromResultObject(_ resultObject: Any) throws -> Response } public extension RequestType { - public var parameters: AnyObject? { + public var parameters: Any? { return nil } - public var extendedFields: [String: AnyObject]? { + public var extendedFields: [String: Any]? { return nil } @@ -39,7 +39,7 @@ public extension RequestType where Response == Void { return true } - public func responseFromResultObject(resultObject: AnyObject) throws -> Response { + public func responseFromResultObject(_ resultObject: Any) throws -> Response { return () } } diff --git a/Tests/BatchElementTests.swift b/Tests/BatchElementTests.swift index 396a417..e3c20df 100644 --- a/Tests/BatchElementTests.swift +++ b/Tests/BatchElementTests.swift @@ -13,45 +13,45 @@ class BatchElementTests: XCTestCase { func testRequestObject() { let request = TestRequest(method: "method", parameters: ["key": "value"], extendedFields: ["exkey": "exvalue"]) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) - XCTAssertEqual(batchElement.id, Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) + XCTAssertEqual(batchElement.id, Id.number(1)) XCTAssertEqual(batchElement.version, "2.0") - let requestObject = batchElement.body as? [String: AnyObject] + let requestObject = batchElement.body as? [String: Any] XCTAssertEqual(requestObject?.keys.count, 5) XCTAssertEqual(requestObject?["jsonrpc"] as? String, "2.0") XCTAssertEqual(requestObject?["id"] as? Int, 1) XCTAssertEqual(requestObject?["method"] as? String, "method") XCTAssertEqual(requestObject?["exkey"] as? String, "exvalue") - let parameters = requestObject?["params"] as? [String: AnyObject] + let parameters = requestObject?["params"] as? [String: Any] XCTAssertEqual(parameters?.keys.count, 1) XCTAssertEqual(parameters?["key"] as? String, "value") } func testNotificationRequestObject() { let request = TestNotificationRequest(method: "method", parameters: ["key": "value"]) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) XCTAssertNil(batchElement.id) XCTAssertEqual(batchElement.version, "2.0") - let requestObject = batchElement.body as? [String: AnyObject] + let requestObject = batchElement.body as? [String: Any] XCTAssertEqual(requestObject?.keys.count, 3) XCTAssertEqual(requestObject?["jsonrpc"] as? String, "2.0") XCTAssertEqual(requestObject?["method"] as? String, "method") XCTAssertNil(requestObject?["id"]) - let parameters = requestObject?["params"] as? [String: AnyObject] + let parameters = requestObject?["params"] as? [String: Any] XCTAssertEqual(parameters?.keys.count, 1) XCTAssertEqual(parameters?["key"] as? String, "value") } func testResponseFromObject() { let request = TestRequest(method: "method", parameters: nil) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) - let responseObject: AnyObject = [ + let responseObject: Any = [ "id": 1, "jsonrpc": "2.0", "result": [ @@ -60,14 +60,15 @@ class BatchElementTests: XCTestCase { ] let response = try? batchElement.responseFromObject(responseObject) - XCTAssertEqual(response?["key"], "value") + let dictionary = response as? [String: Any] + XCTAssertEqual(dictionary?["key"] as? String, "value") } func testResponseFromArray() { let request = TestRequest(method: "method", parameters: nil) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) - let responseArray: [AnyObject] = [ + let responseArray: [Any] = [ [ "id": 1, "jsonrpc": "2.0", @@ -85,14 +86,15 @@ class BatchElementTests: XCTestCase { ] let response = try? batchElement.responseFromBatchObjects(responseArray) - XCTAssertEqual(response?["key1"], "value1") + let dictionary = response as? [String: Any] + XCTAssertEqual(dictionary?["key1"] as? String, "value1") } func testResponseFromObjectResponseError() { let request = TestRequest(method: "method", parameters: nil) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) - let responseObject: AnyObject = [ + let responseObject: Any = [ "id": 1, "jsonrpc": "2.0", "error": [ @@ -105,11 +107,11 @@ class BatchElementTests: XCTestCase { ] do { - try batchElement.responseFromObject(responseObject) + _ = try batchElement.responseFromObject(responseObject) XCTFail() } catch { let error = error as? JSONRPCError - if case .ResponseError(let code, let message, let data as [String: AnyObject])? = error { + if case .responseError(let code, let message, let data as [String: AnyObject])? = error { XCTAssertEqual(code, 123) XCTAssertEqual(message, "abc") XCTAssertEqual(data["key"] as? String, "value") @@ -121,9 +123,9 @@ class BatchElementTests: XCTestCase { func testResponseFromArrayResponseError() { let request = TestRequest(method: "method", parameters: nil) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) - let responseArray: [AnyObject] = [ + let responseArray: [Any] = [ [ "id": 1, "jsonrpc": "2.0", @@ -143,11 +145,11 @@ class BatchElementTests: XCTestCase { ] do { - try batchElement.responseFromBatchObjects(responseArray) + _ = try batchElement.responseFromBatchObjects(responseArray) XCTFail() } catch { let error = error as? JSONRPCError - if case .ResponseError(let code, let message, let data as [String: AnyObject])? = error { + if case .responseError(let code, let message, let data as [String: AnyObject])? = error { XCTAssertEqual(code, 123) XCTAssertEqual(message, "abc") XCTAssertEqual(data["key"] as? String, "value") @@ -157,22 +159,22 @@ class BatchElementTests: XCTestCase { } } - func testResponseFromObjectResultObjectParseError() { + func testResponseFromObjectresultObjectParseError() { let request = TestParseErrorRequest(method: "method", parameters: nil) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) - let responseObject: AnyObject = [ + let responseObject: Any = [ "id": 1, "jsonrpc": "2.0", "result": [:], ] do { - try batchElement.responseFromObject(responseObject) + _ = try batchElement.responseFromObject(responseObject) XCTFail() } catch { let error = error as? JSONRPCError - if case .ResultObjectParseError(let error)? = error { + if case .resultObjectParseError(let error)? = error { XCTAssert(error is TestParseErrorRequest.ParseError) } else { XCTFail() @@ -180,11 +182,11 @@ class BatchElementTests: XCTestCase { } } - func testResponseFromArrayResultObjectParseError() { + func testResponseFromArrayresultObjectParseError() { let request = TestParseErrorRequest(method: "method", parameters: nil) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) - let responseArray: [AnyObject] = [ + let responseArray: [Any] = [ [ "id": 1, "jsonrpc": "2.0", @@ -198,11 +200,11 @@ class BatchElementTests: XCTestCase { ] do { - try batchElement.responseFromBatchObjects(responseArray) + _ = try batchElement.responseFromBatchObjects(responseArray) XCTFail() } catch { let error = error as? JSONRPCError - if case .ResultObjectParseError(let error)? = error { + if case .resultObjectParseError(let error)? = error { XCTAssert(error is TestParseErrorRequest.ParseError) } else { XCTFail() @@ -212,9 +214,9 @@ class BatchElementTests: XCTestCase { func testResponseFromObjectErrorObjectParseError() { let request = TestRequest(method: "method", parameters: nil) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) - let responseObject: AnyObject = [ + let responseObject: Any = [ "id": 1, "jsonrpc": "2.0", "error": [ @@ -223,11 +225,11 @@ class BatchElementTests: XCTestCase { ] do { - try batchElement.responseFromObject(responseObject) + _ = try batchElement.responseFromObject(responseObject) XCTFail() } catch { let error = error as? JSONRPCError - if case .ErrorObjectParseError? = error { + if case .errorObjectParseError? = error { } else { XCTFail() @@ -237,9 +239,9 @@ class BatchElementTests: XCTestCase { func testResponseFromArrayErrorObjectParseError() { let request = TestRequest(method: "method", parameters: nil) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) - let responseArray: [AnyObject] = [ + let responseArray: [Any] = [ [ "id": 1, "jsonrpc": "2.0", @@ -255,11 +257,11 @@ class BatchElementTests: XCTestCase { ] do { - try batchElement.responseFromBatchObjects(responseArray) + _ = try batchElement.responseFromBatchObjects(responseArray) XCTFail() } catch { let error = error as? JSONRPCError - if case .ErrorObjectParseError? = error { + if case .errorObjectParseError? = error { } else { XCTFail() @@ -267,11 +269,11 @@ class BatchElementTests: XCTestCase { } } - func testResponseFromObjectUnsupportedVersion() { + func testResponseFromObjectunsupportedVersion() { let request = TestRequest(method: "method", parameters: nil) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) - let responseObject: AnyObject = [ + let responseObject: Any = [ "id": 1, "jsonrpc": "1.0", "result": [ @@ -280,11 +282,11 @@ class BatchElementTests: XCTestCase { ] do { - try batchElement.responseFromObject(responseObject) + _ = try batchElement.responseFromObject(responseObject) XCTFail() } catch { let error = error as? JSONRPCError - if case .UnsupportedVersion(let version)? = error { + if case .unsupportedVersion(let version)? = error { XCTAssertEqual(version, "1.0") } else { XCTFail() @@ -292,11 +294,11 @@ class BatchElementTests: XCTestCase { } } - func testResponseFromArrayUnsupportedVersion() { + func testResponseFromArrayunsupportedVersion() { let request = TestRequest(method: "method", parameters: nil) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) - let responseArray: [AnyObject] = [ + let responseArray: [Any] = [ [ "id": 1, "jsonrpc": "1.0", @@ -310,11 +312,11 @@ class BatchElementTests: XCTestCase { ] do { - try batchElement.responseFromBatchObjects(responseArray) + _ = try batchElement.responseFromBatchObjects(responseArray) XCTFail() } catch { let error = error as? JSONRPCError - if case .UnsupportedVersion(let version)? = error { + if case .unsupportedVersion(let version)? = error { XCTAssertEqual(version, "1.0") } else { XCTFail() @@ -322,22 +324,22 @@ class BatchElementTests: XCTestCase { } } - func testResponseFromObjectResponseNotFound() { + func testResponseFromObjectresponseNotFound() { let request = TestRequest(method: "method", parameters: nil) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) - let responseObject: AnyObject = [ + let responseObject: Any = [ "id": 2, "jsonrpc": "2.0", "result": [:] ] do { - try batchElement.responseFromObject(responseObject) + _ = try batchElement.responseFromObject(responseObject) XCTFail() } catch { let error = error as? JSONRPCError - if case .ResponseNotFound(let id, let object as [String: AnyObject])? = error { + if case .responseNotFound(let id, let object as [String: Any])? = error { XCTAssertEqual(id, batchElement.id) XCTAssertEqual(object["id"] as? Int, 2) } else { @@ -346,11 +348,11 @@ class BatchElementTests: XCTestCase { } } - func testResponseFromArrayResponseNotFound() { + func testResponseFromArrayresponseNotFound() { let request = TestRequest(method: "method", parameters: nil) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) - let responseArray: [AnyObject] = [ + let responseArray: [Any] = [ [ "id": 2, "jsonrpc": "2.0", @@ -364,11 +366,11 @@ class BatchElementTests: XCTestCase { ] do { - try batchElement.responseFromBatchObjects(responseArray) + _ = try batchElement.responseFromBatchObjects(responseArray) XCTFail() } catch { let error = error as? JSONRPCError - if case .ResponseNotFound(let id, let object as [[String: AnyObject]])? = error { + if case .responseNotFound(let id, let object as [[String: Any]])? = error { XCTAssertEqual(id, batchElement.id) XCTAssertEqual(object[0]["id"] as? Int, 2) } else { @@ -377,21 +379,21 @@ class BatchElementTests: XCTestCase { } } - func testResponseFromObjectMissingBothResultAndError() { + func testResponseFromObjectmissingBothResultAndError() { let request = TestRequest(method: "method", parameters: nil) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) - let responseObject: AnyObject = [ + let responseObject: Any = [ "id": 1, "jsonrpc": "2.0", ] do { - try batchElement.responseFromObject(responseObject) + _ = try batchElement.responseFromObject(responseObject) XCTFail() } catch { let error = error as? JSONRPCError - if case .MissingBothResultAndError? = error { + if case .missingBothResultAndError? = error { } else { XCTFail() @@ -399,11 +401,11 @@ class BatchElementTests: XCTestCase { } } - func testResponseFromArrayMissingBothResultAndError() { + func testResponseFromArraymissingBothResultAndError() { let request = TestRequest(method: "method", parameters: nil) - let batchElement = BatchElement(request: request, version: "2.0", id: Id.Number(1)) + let batchElement = BatchElement(request: request, version: "2.0", id: Id.number(1)) - let responseArray: [AnyObject] = [ + let responseArray: [Any] = [ [ "id": 1, "jsonrpc": "2.0", @@ -416,11 +418,11 @@ class BatchElementTests: XCTestCase { ] do { - try batchElement.responseFromBatchObjects(responseArray) + _ = try batchElement.responseFromBatchObjects(responseArray) XCTFail() } catch { let error = error as? JSONRPCError - if case .MissingBothResultAndError? = error { + if case .missingBothResultAndError? = error { } else { XCTFail() diff --git a/Tests/BatchFactoryTests.swift b/Tests/BatchFactoryTests.swift index d58d7f0..7395d0b 100644 --- a/Tests/BatchFactoryTests.swift +++ b/Tests/BatchFactoryTests.swift @@ -47,12 +47,12 @@ class BatchFactoryTests: XCTestCase { } func testThreadSafety() { - let operationQueue = NSOperationQueue() + let operationQueue = OperationQueue() for _ in 1..<10000 { - operationQueue.addOperationWithBlock { + operationQueue.addOperation { let request = TestRequest(method: "method", parameters: nil) - self.batchFactory.create(request) + _ = self.batchFactory.create(request) } } diff --git a/Tests/TestRequest.swift b/Tests/TestRequest.swift index 76850d1..728f3b7 100644 --- a/Tests/TestRequest.swift +++ b/Tests/TestRequest.swift @@ -10,19 +10,19 @@ import Foundation import JSONRPCKit struct TestRequest: RequestType { - typealias Response = AnyObject + typealias Response = Any let method: String - let parameters: AnyObject? - let extendedFields: [String : AnyObject]? + let parameters: Any? + let extendedFields: [String : Any]? - init(method: String, parameters: AnyObject?, extendedFields: [String: AnyObject]? = nil) { + init(method: String, parameters: Any?, extendedFields: [String: Any]? = nil) { self.method = method self.parameters = parameters self.extendedFields = extendedFields } - func responseFromResultObject(resultObject: AnyObject) throws -> AnyObject { + func responseFromResultObject(_ resultObject: Any) throws -> Any { return resultObject } } @@ -31,20 +31,20 @@ struct TestNotificationRequest: RequestType { typealias Response = Void let method: String - let parameters: AnyObject? + let parameters: Any? } struct TestParseErrorRequest: RequestType { - struct ParseError: ErrorType { + struct ParseError: Error { } - typealias Response = AnyObject + typealias Response = Any let method: String - let parameters: AnyObject? + let parameters: Any? - func responseFromResultObject(resultObject: AnyObject) throws -> AnyObject { + func responseFromResultObject(_ resultObject: Any) throws -> Any { throw ParseError() } }