Skip to content

Commit

Permalink
Merge pull request #1056 from RomanPodymov/master
Browse files Browse the repository at this point in the history
More Swift standard library functions
  • Loading branch information
tristanhimmelman committed Jun 6, 2019
2 parents b659f81 + bcf759a commit 8e70976
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 26 deletions.
12 changes: 4 additions & 8 deletions Sources/ImmutableMappable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,9 @@ public extension Map {
guard let jsonDictionary = currentValue as? [String: Any] else {
throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[String: Any]'", file: file, function: function, line: line)
}
var value: [String: T] = [:]
for (key, json) in jsonDictionary {
value[key] = try Mapper<T>(context: context).mapOrFail(JSONObject: json)
return try jsonDictionary.mapValues { json in
return try Mapper<T>(context: context).mapOrFail(JSONObject: json)
}
return value
}

/// Returns a `[String: BaseMappable]` boxed in `Optional` or throws an error.
Expand All @@ -187,14 +185,12 @@ public extension Map {
guard let jsonDictionary = currentValue as? [String: Any] else {
throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[String: Any]'", file: file, function: function, line: line)
}
var value: [String: Transform.Object] = [:]
for (key, json) in jsonDictionary {
return try jsonDictionary.mapValues { json in
guard let object = transform.transformFromJSON(json) else {
throw MapError(key: key, currentValue: json, reason: "Cannot transform to '\(Transform.Object.self)' using \(transform)", file: file, function: function, line: line)
}
value[key] = object
return object
}
return value
}

/// Returns a `[String: BaseMappable]` using transform or throws an error.
Expand Down
12 changes: 5 additions & 7 deletions Sources/Mapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public final class Mapper<N: BaseMappable> {
public func mapDictionary(JSON: [String: [String: Any]]) -> [String: N]? {
// map every value in dictionary to type N
let result = JSON.filterMap(map)
if result.isEmpty == false {
if !result.isEmpty {
return result
}

Expand Down Expand Up @@ -235,7 +235,7 @@ public final class Mapper<N: BaseMappable> {
mapArray(JSONArray: $0)
}

if result.isEmpty == false {
if !result.isEmpty {
return result
}

Expand All @@ -245,13 +245,11 @@ public final class Mapper<N: BaseMappable> {
/// Maps an 2 dimentional array of JSON dictionaries to a 2 dimentional array of Mappable objects
public func mapArrayOfArrays(JSONObject: Any?) -> [[N]]? {
if let JSONArray = JSONObject as? [[[String: Any]]] {
var objectArray = [[N]]()
for innerJSONArray in JSONArray {
let array = mapArray(JSONArray: innerJSONArray)
objectArray.append(array)
let objectArray = JSONArray.map { innerJSONArray in
return mapArray(JSONArray: innerJSONArray)
}

if objectArray.isEmpty == false {
if !objectArray.isEmpty {
return objectArray
}
}
Expand Down
18 changes: 7 additions & 11 deletions Sources/ToJSON.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,20 @@ private func setValue(_ value: Any, key: String, checkForNestedKeys: Bool, delim
}

private func setValue(_ value: Any, forKeyPathComponents components: ArraySlice<String>, dictionary: inout [String : Any]) {
if components.isEmpty {
guard let head = components.first else {
return
}

let head = components.first!

let headAsString = String(head)
if components.count == 1 {
dictionary[String(head)] = value
dictionary[headAsString] = value
} else {
var child = dictionary[String(head)] as? [String : Any]
if child == nil {
child = [:]
}

var child = dictionary[headAsString] as? [String : Any] ?? [:]

let tail = components.dropFirst()
setValue(value, forKeyPathComponents: tail, dictionary: &child!)
setValue(value, forKeyPathComponents: tail, dictionary: &child)

dictionary[String(head)] = child
dictionary[headAsString] = child
}
}

Expand Down

0 comments on commit 8e70976

Please sign in to comment.