From b062599fcfac11bff02a8851f748f8f5797f28b7 Mon Sep 17 00:00:00 2001 From: Oleksii Dykan Date: Thu, 19 Jan 2017 12:41:58 +0100 Subject: [PATCH] Remove methods for signle key and use only one with keypath --- SwiftyJSONModel/JSONObject.swift | 54 ++++++++++---------------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/SwiftyJSONModel/JSONObject.swift b/SwiftyJSONModel/JSONObject.swift index 33e2c83..cc6fd69 100644 --- a/SwiftyJSONModel/JSONObject.swift +++ b/SwiftyJSONModel/JSONObject.swift @@ -43,22 +43,6 @@ public extension JSONObject where PropertyType.RawValue == String { } // MARK: - Throwable methods - public func value(for key: PropertyType) throws -> T { - do { - return try T(json: self[key]) - } catch let error as JSONModelError { - throw JSONModelError.invalidValueFor(key: key.rawValue, error) - } - } - - public func value(for key: PropertyType) throws -> [T] { - do { - return try self[key].arrayValue().map({ try T(json: $0) }) - } catch let error as JSONModelError { - throw JSONModelError.invalidValueFor(key: key.rawValue, error) - } - } - public func object(for key: PropertyType) throws -> JSONObject { do { return try JSONObject(json: self[key]) @@ -73,16 +57,17 @@ public extension JSONObject where PropertyType.RawValue == String { public func value(for keyPath: [PropertyType]) throws -> T { assert(keyPath.isEmpty == false, "KeyPath cannot be empty") + let key = keyPath[0] - if keyPath.count == 1 { - return try value(for: key) - } else { - let subPath: [PropertyType] = .init(keyPath[1..(json: self[key]).value(for: subPath) - } catch let error as JSONModelError { - throw JSONModelError.invalidValueFor(key: key.rawValue, error) } + } catch let error as JSONModelError { + throw JSONModelError.invalidValueFor(key: key.rawValue, error) } } @@ -92,28 +77,21 @@ public extension JSONObject where PropertyType.RawValue == String { public func value(for keyPath: [PropertyType]) throws -> [T] { assert(keyPath.isEmpty == false, "KeyPath cannot be empty") + let key = keyPath[0] - if keyPath.count == 1 { - return try value(for: key) - } else { - let subPath: [PropertyType] = .init(keyPath[1..(json: self[key]).value(for: subPath) - } catch let error as JSONModelError { - throw JSONModelError.invalidValueFor(key: key.rawValue, error) } + } catch let error as JSONModelError { + throw JSONModelError.invalidValueFor(key: key.rawValue, error) } } // MARK: - Optional methods - public func value(for key: PropertyType) -> T? { - return try? value(for: key) - } - - public func value(for key: PropertyType) -> [T]? { - return try? value(for: key) - } - public func value(for keyPath: PropertyType...) -> T? { return try? value(for: keyPath) }