Skip to content

Commit

Permalink
Remove methods for signle key and use only one with keypath
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Dykan committed Jan 19, 2017
1 parent 3ef59e5 commit b062599
Showing 1 changed file with 16 additions and 38 deletions.
54 changes: 16 additions & 38 deletions SwiftyJSONModel/JSONObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,6 @@ public extension JSONObject where PropertyType.RawValue == String {
}

// MARK: - Throwable methods
public func value<T: JSONInitializable>(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<T: JSONInitializable>(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<PropertyType> {
do {
return try JSONObject<PropertyType>(json: self[key])
Expand All @@ -73,16 +57,17 @@ public extension JSONObject where PropertyType.RawValue == String {

public func value<T: JSONInitializable>(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..<keyPath.count])
do {
do {
if keyPath.count == 1 {
return try T(json: self[key])
} else {
let subPath: [PropertyType] = .init(keyPath[1..<keyPath.count])
return try JSONObject<PropertyType>(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)
}
}

Expand All @@ -92,28 +77,21 @@ public extension JSONObject where PropertyType.RawValue == String {

public func value<T: JSONInitializable>(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..<keyPath.count])
do {
do {
if keyPath.count == 1 {
return try self[key].arrayValue().map({ try T(json: $0) })
} else {
let subPath: [PropertyType] = .init(keyPath[1..<keyPath.count])
return try JSONObject<PropertyType>(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<T: JSONInitializable>(for key: PropertyType) -> T? {
return try? value(for: key)
}

public func value<T: JSONInitializable>(for key: PropertyType) -> [T]? {
return try? value(for: key)
}

public func value<T: JSONInitializable>(for keyPath: PropertyType...) -> T? {
return try? value(for: keyPath)
}
Expand Down

0 comments on commit b062599

Please sign in to comment.