@@ -54,24 +54,25 @@ public extension ImmutableMappable {
54
54
55
55
public extension Map {
56
56
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
59
60
}
60
61
61
62
// MARK: Basic
62
63
63
64
/// 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 )
66
67
guard let value = currentValue as? T else {
67
68
throw MapError ( key: key, currentValue: currentValue, reason: " Cannot cast to ' \( T . self) ' " , file: file, function: function, line: line)
68
69
}
69
70
return value
70
71
}
71
72
72
73
/// 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 )
75
76
guard let value = transform. transformFromJSON ( currentValue) else {
76
77
throw MapError ( key: key, currentValue: currentValue, reason: " Cannot transform to ' \( Transform . Object. self) ' using \( transform) " , file: file, function: function, line: line)
77
78
}
@@ -81,16 +82,16 @@ public extension Map {
81
82
// MARK: BaseMappable
82
83
83
84
/// 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 )
86
87
return try Mapper < T > ( ) . mapOrFail ( JSONObject: currentValue)
87
88
}
88
89
89
90
// MARK: [BaseMappable]
90
91
91
92
/// 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 )
94
95
guard let jsonArray = currentValue as? [ Any ] else {
95
96
throw MapError ( key: key, currentValue: currentValue, reason: " Cannot cast to '[Any]' " , file: file, function: function, line: line)
96
97
}
@@ -100,8 +101,8 @@ public extension Map {
100
101
}
101
102
102
103
/// 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 )
105
106
guard let jsonArray = currentValue as? [ Any ] else {
106
107
throw MapError ( key: key, currentValue: currentValue, reason: " Cannot cast to '[Any]' " , file: file, function: function, line: line)
107
108
}
@@ -116,8 +117,8 @@ public extension Map {
116
117
// MARK: [String: BaseMappable]
117
118
118
119
/// 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 )
121
122
guard let jsonDictionary = currentValue as? [ String : Any ] else {
122
123
throw MapError ( key: key, currentValue: currentValue, reason: " Cannot cast to '[String: Any]' " , file: file, function: function, line: line)
123
124
}
@@ -129,8 +130,8 @@ public extension Map {
129
130
}
130
131
131
132
/// 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 )
134
135
guard let jsonDictionary = currentValue as? [ String : Any ] else {
135
136
throw MapError ( key: key, currentValue: currentValue, reason: " Cannot cast to '[String: Any]' " , file: file, function: function, line: line)
136
137
}
0 commit comments