Skip to content

Commit

Permalink
Merge pull request #11 from alickbass/remove-duplicate-methods
Browse files Browse the repository at this point in the history
Remove methods for signle key and use only one with keypath
  • Loading branch information
Oleksii Dykan authored Jan 19, 2017
2 parents 3ef59e5 + b062599 commit 7142ae5
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 7142ae5

Please sign in to comment.