Skip to content

Commit 17b2c51

Browse files
committed
feat(*): Add default argument to Map's value(_)
1 parent 5ab8f3c commit 17b2c51

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ init(map: Map) throws {
248248
createdAt = try map.value("createdAt", using: DateTransform()) // throws an error when it fails
249249
updatedAt = try? map.value("updatedAt", using: DateTransform()) // optional
250250
posts = (try? map.value("posts")) ?? [] // optional + default value
251+
surname = try? map.value("surname", default: "DefaultSurname") // optional + default value as an argument
251252
}
252253
```
253254

Sources/Map.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,45 @@ private func valueFor(_ keyPathComponents: ArraySlice<String>, array: [Any]) ->
202202

203203
return (false, nil)
204204
}
205+
206+
// MARK: - Default Value
207+
208+
public extension Map {
209+
210+
/// Returns `default` value if there is nothing to parse.
211+
func value<T>(_ key: String, default: T.Object, using transform: T) throws -> T.Object where T: TransformType {
212+
if let value: T.Object = try? self.value(key, using: transform) {
213+
return value
214+
} else {
215+
return `default`
216+
}
217+
}
218+
219+
/// Returns `default` value if there is nothing to parse.
220+
func value<T>(_ key: String, default: T) throws -> T {
221+
if let value: T = try? self.value(key) {
222+
return value
223+
} else {
224+
return `default`
225+
}
226+
}
227+
228+
/// Returns `default` value if there is nothing to parse.
229+
func value<T: BaseMappable>(_ key: String, default: [T]) -> [T] {
230+
do {
231+
let value: [T] = try self.value(key)
232+
return value
233+
} catch {
234+
return `default`
235+
}
236+
}
237+
238+
/// Returns `default` value if there is nothing to parse.
239+
func value<T>(_ key: String, default: T) throws -> T where T: BaseMappable {
240+
if let value: T = try? self.value(key) as T {
241+
return value
242+
} else {
243+
return `default`
244+
}
245+
}
246+
}

Tests/ObjectMapperTests/MapContextTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,32 @@ class MapContextTests: XCTestCase {
242242

243243
XCTFail()
244244
}
245+
246+
func testDefaultArgumentWithoutValue() {
247+
let JSON: [String: Any] = [:]
248+
let dog = try? Mapper<ImmutableDog>().map(JSON: JSON) as ImmutableDog
249+
250+
XCTAssertNotNil(dog)
251+
XCTAssertTrue(dog!.name == "Sasha")
252+
}
253+
254+
func testDefaultArgumentWithValue() {
255+
let JSON = ["name": "Sofie"]
256+
let dog = try? Mapper<ImmutableDog>().map(JSON: JSON) as ImmutableDog
257+
258+
XCTAssertNotNil(dog)
259+
XCTAssertTrue(dog!.name == "Sofie")
260+
}
261+
262+
// MARK: - Default Argument
263+
public struct ImmutableDog: ImmutableMappable {
264+
public let name: String
265+
266+
/// Define `default` value to use if it is nothing to parse in `name`
267+
public init(map: Map) throws {
268+
name = try map.value("name", default: "Sasha")
269+
}
270+
}
245271

246272
// MARK: - Nested Types
247273
// MARK: BaseMappable

0 commit comments

Comments
 (0)