Skip to content

Commit dfe0025

Browse files
committed
Add nested key support in ImmutableMappable
1 parent a80e48a commit dfe0025

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

Sources/ImmutableMappable.swift

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,25 @@ public extension ImmutableMappable {
5454

5555
public extension Map {
5656

57-
fileprivate func currentValue(for key: String) -> Any? {
58-
return self[key].currentValue
57+
fileprivate func currentValue(for key: String, nested: Bool? = nil, delimiter: String = ".") -> Any? {
58+
let isNested = nested ?? key.contains(delimiter)
59+
return self[key, nested: isNested, delimiter: delimiter].currentValue
5960
}
6061

6162
// MARK: Basic
6263

6364
/// Returns a value or throws an error.
64-
public func value<T>(_ key: String, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> T {
65-
let currentValue = self.currentValue(for: key)
65+
public func value<T>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> T {
66+
let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
6667
guard let value = currentValue as? T else {
6768
throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '\(T.self)'", file: file, function: function, line: line)
6869
}
6970
return value
7071
}
7172

7273
/// Returns a transformed value or throws an error.
73-
public func value<Transform: TransformType>(_ key: String, using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> Transform.Object {
74-
let currentValue = self.currentValue(for: key)
74+
public func value<Transform: TransformType>(_ key: String, nested: Bool? = nil, delimiter: String = ".", using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> Transform.Object {
75+
let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
7576
guard let value = transform.transformFromJSON(currentValue) else {
7677
throw MapError(key: key, currentValue: currentValue, reason: "Cannot transform to '\(Transform.Object.self)' using \(transform)", file: file, function: function, line: line)
7778
}
@@ -81,16 +82,16 @@ public extension Map {
8182
// MARK: BaseMappable
8283

8384
/// Returns a `BaseMappable` object or throws an error.
84-
public func value<T: BaseMappable>(_ key: String) throws -> T {
85-
let currentValue = self.currentValue(for: key)
85+
public func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".") throws -> T {
86+
let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
8687
return try Mapper<T>().mapOrFail(JSONObject: currentValue)
8788
}
8889

8990
// MARK: [BaseMappable]
9091

9192
/// Returns a `[BaseMappable]` or throws an error.
92-
public func value<T: BaseMappable>(_ key: String, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [T] {
93-
let currentValue = self.currentValue(for: key)
93+
public func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [T] {
94+
let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
9495
guard let jsonArray = currentValue as? [Any] else {
9596
throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[Any]'", file: file, function: function, line: line)
9697
}
@@ -100,8 +101,8 @@ public extension Map {
100101
}
101102

102103
/// Returns a `[BaseMappable]` using transform or throws an error.
103-
public func value<Transform: TransformType>(_ key: String, using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [Transform.Object] {
104-
let currentValue = self.currentValue(for: key)
104+
public func value<Transform: TransformType>(_ key: String, nested: Bool? = nil, delimiter: String = ".", using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [Transform.Object] {
105+
let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
105106
guard let jsonArray = currentValue as? [Any] else {
106107
throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[Any]'", file: file, function: function, line: line)
107108
}
@@ -116,8 +117,8 @@ public extension Map {
116117
// MARK: [String: BaseMappable]
117118

118119
/// Returns a `[String: BaseMappable]` or throws an error.
119-
public func value<T: BaseMappable>(_ key: String, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [String: T] {
120-
let currentValue = self.currentValue(for: key)
120+
public func value<T: BaseMappable>(_ key: String, nested: Bool? = nil, delimiter: String = ".", file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [String: T] {
121+
let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
121122
guard let jsonDictionary = currentValue as? [String: Any] else {
122123
throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[String: Any]'", file: file, function: function, line: line)
123124
}
@@ -129,8 +130,8 @@ public extension Map {
129130
}
130131

131132
/// Returns a `[String: BaseMappable]` using transform or throws an error.
132-
public func value<Transform: TransformType>(_ key: String, using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [String: Transform.Object] {
133-
let currentValue = self.currentValue(for: key)
133+
public func value<Transform: TransformType>(_ key: String, nested: Bool? = nil, delimiter: String = ".", using transform: Transform, file: StaticString = #file, function: StaticString = #function, line: UInt = #line) throws -> [String: Transform.Object] {
134+
let currentValue = self.currentValue(for: key, nested: nested, delimiter: delimiter)
134135
guard let jsonDictionary = currentValue as? [String: Any] else {
135136
throw MapError(key: key, currentValue: currentValue, reason: "Cannot cast to '[String: Any]'", file: file, function: function, line: line)
136137
}

0 commit comments

Comments
 (0)