From 1521a9281cde16b4acdaa45bfb559e0ab056175b Mon Sep 17 00:00:00 2001 From: Shinichiro Oba Date: Thu, 12 Nov 2015 12:04:09 +0900 Subject: [PATCH] rename and add convenience methods --- Example/SingleRequestViewController.swift | 2 +- JSONRPCKit/JSONRPC.swift | 30 ++++++------ JSONRPCKit/NumberIdentifierGenerator.swift | 2 +- JSONRPCKit/RequestIdentifier.swift | 52 +++++++++++++++++---- JSONRPCKit/RequestIdentifierGenerator.swift | 1 + 5 files changed, 59 insertions(+), 28 deletions(-) diff --git a/Example/SingleRequestViewController.swift b/Example/SingleRequestViewController.swift index 2b908ff..9f1b506 100644 --- a/Example/SingleRequestViewController.swift +++ b/Example/SingleRequestViewController.swift @@ -14,7 +14,7 @@ public class StringIdentifierGenerator: RequestIdentifierGenerator { private var currentIdentifier = 1 public func next() -> RequestIdentifier { - return .StringType("\(self.currentIdentifier++)") + return .StringIdentifier("id\(self.currentIdentifier++)") } } diff --git a/JSONRPCKit/JSONRPC.swift b/JSONRPCKit/JSONRPC.swift index 97bd572..32d448e 100644 --- a/JSONRPCKit/JSONRPC.swift +++ b/JSONRPCKit/JSONRPC.swift @@ -27,21 +27,21 @@ public class JSONRPC { } public func addRequest(request: T, handler: (Result) -> Void) { - let id = self.identifierGenerator.next() + let identifier = self.identifierGenerator.next() - self.requests.append(self.buildJSONFromRequest(request, id: id)) + self.requests.append(self.buildJSONFromRequest(request, identifier: identifier)) - self.handlers[id] = { (json: [String: AnyObject]) -> Void in + self.handlers[identifier] = { (json: [String: AnyObject]) -> Void in if let result = json["result"] { if let response = request.responseFromObject(result) { handler(.Success(response)) } else { - handler(.Failure(JSONRPCError.InvalidResult(result))) + handler(.Failure(.InvalidResult(result))) } } else if let error = json["error"] { - handler(.Failure(JSONRPCError.ErrorRequest(error))) + handler(.Failure(.ErrorRequest(error))) } else { - handler(.Failure(JSONRPCError.InvalidResponse(json))) + handler(.Failure(.InvalidResponse(json))) } } } @@ -50,18 +50,13 @@ public class JSONRPC { self.requests.append(self.buildJSONFromRequest(request)) } - private func buildJSONFromRequest(request: T, id: RequestIdentifier? = nil) -> [String: AnyObject] { + private func buildJSONFromRequest(request: T, identifier: RequestIdentifier? = nil) -> [String: AnyObject] { var json: [String: AnyObject] = request.buildJSON() json["jsonrpc"] = self.version - if let id = id { - switch id { - case .StringType(let string): - json["id"] = string - case .NumberType(let number): - json["id"] = number - } + if let identifier = identifier { + json["id"] = identifier.value } return json @@ -90,10 +85,11 @@ public class JSONRPC { throw JSONRPCError.UnsupportedVersion(version) } - if let id = json["id"] as? String, handle = self.handlers[.StringType(id)] { - handle(json) + guard let id = json["id"] else { + return } - if let id = json["id"] as? Int, handle = self.handlers[.NumberType(id)] { + + if let identifier = RequestIdentifier(value: id), handle = self.handlers[identifier] { handle(json) } } diff --git a/JSONRPCKit/NumberIdentifierGenerator.swift b/JSONRPCKit/NumberIdentifierGenerator.swift index 5be220f..c186536 100644 --- a/JSONRPCKit/NumberIdentifierGenerator.swift +++ b/JSONRPCKit/NumberIdentifierGenerator.swift @@ -13,6 +13,6 @@ public class NumberIdentifierGenerator: RequestIdentifierGenerator { private var currentIdentifier = 1 public func next() -> RequestIdentifier { - return .NumberType(self.currentIdentifier++) + return .NumberIdentifier(self.currentIdentifier++) } } diff --git a/JSONRPCKit/RequestIdentifier.swift b/JSONRPCKit/RequestIdentifier.swift index 47ff919..70b5f6d 100644 --- a/JSONRPCKit/RequestIdentifier.swift +++ b/JSONRPCKit/RequestIdentifier.swift @@ -8,27 +8,61 @@ import Foundation -public enum RequestIdentifier: Hashable { - case NumberType(Int) - case StringType(String) +public enum RequestIdentifier { + case NumberIdentifier(Int) + case StringIdentifier(String) +} + +extension RequestIdentifier { + + public init(string: String) { + self = .StringIdentifier(string) + } + + public init(number: Int) { + self = .NumberIdentifier(number) + } + + public init?(value: AnyObject) { + switch value { + case let string as String: + self = .StringIdentifier(string) + case let number as Int: + self = .NumberIdentifier(number) + default: + return nil + } + } + + public var value: AnyObject { + switch self { + case NumberIdentifier(let number): + return number + case StringIdentifier(let string): + return string + } + } +} + +extension RequestIdentifier: Hashable { public var hashValue: Int { switch self { - case NumberType(let number): + case NumberIdentifier(let number): return number - case StringType(let string): + case StringIdentifier(let string): return string.hashValue } } } public func ==(lhs: RequestIdentifier, rhs: RequestIdentifier) -> Bool { - if case let (.NumberType(lnumber), .NumberType(rnumber)) = (lhs, rhs) { - return lnumber == rnumber + if case let (.NumberIdentifier(left), .NumberIdentifier(right)) = (lhs, rhs) { + return left == right } - if case let (.StringType(lstring), .StringType(rstring)) = (lhs, rhs) { - return lstring == rstring + if case let (.StringIdentifier(left), .StringIdentifier(right)) = (lhs, rhs) { + return left == right } return false diff --git a/JSONRPCKit/RequestIdentifierGenerator.swift b/JSONRPCKit/RequestIdentifierGenerator.swift index 5788dae..7275bb5 100644 --- a/JSONRPCKit/RequestIdentifierGenerator.swift +++ b/JSONRPCKit/RequestIdentifierGenerator.swift @@ -9,5 +9,6 @@ import Foundation public protocol RequestIdentifierGenerator { + func next() -> RequestIdentifier }