Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Support Array Of Fields For Custom Transformation #120

Merged
merged 1 commit into from
Oct 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

## Enhancements

- None
- Support Array Of Fields For Custom Transformation
[Daniel Duan](https://github.com/dduan)
[#120](https://github.com/lyft/mapper/pull/120)

# 7.1.0

Expand Down
18 changes: 18 additions & 0 deletions Sources/Mapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,24 @@ public struct Mapper {
return (try? transformation(try self.JSONFromField(field))).flatMap { $0 }
}

/// Get an optional typed value from the given fields by using the given transformation
///
/// - parameter fields: The array of fields to retrieve from the source data, can be an empty
/// string to return the entire data set
/// - parameter transformation: The transformation function used to create the expected value
///
/// - returns: The value of type T for the given field, if the transformation function doesn't throw
/// otherwise nil
public func optionalFrom<T>(_ fields: [String], transformation: (Any) throws -> T?) -> T? {
for field in fields {
if let value = try? transformation(try self.JSONFromField(field)) {
return value
}
}

return nil
}

// MARK: - Private

/// Get the object for a given field. If an empty string is passed, return the entire data source. This
Expand Down
60 changes: 60 additions & 0 deletions Tests/MapperTests/CustomTransformationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,64 @@ final class CustomTransformationTests: XCTestCase {
XCTFail("Shouldn't have failed to create Test")
}
}

func testOptionalCustomTransformationArrayOfKeys() {
struct Test: Mappable {
let value: Int?
init(map: Mapper) throws {
value = map.optionalFrom(["a", "b"], transformation: { thing in
if let a = thing as? Int {
return a + 1
}
throw MapperError.customError(field: nil, message: "")
})
}
}

do {
let test = try Test(map: Mapper(JSON: ["a": "##", "b": 1]))
XCTAssertEqual(test.value, 2)
} catch {
XCTFail("Shouldn't have failed to create Test")
}
}

func testOptionalCustomTransformationArrayOfKeysFails() {
struct Test: Mappable {
let value: Int?
init(map: Mapper) throws {
value = map.optionalFrom(["a", "b"], transformation: { _ in
throw MapperError.customError(field: nil, message: "")
})
}
}

do {
let test = try Test(map: Mapper(JSON: ["a": "##", "b": 1]))
XCTAssertNil(test.value)
} catch {
XCTFail("Shouldn't have failed to create Test")
}
}

func testOptionalCustomTransformationArrayOfKeysReturnsNil() {
struct Test: Mappable {
let value: Int?
init(map: Mapper) throws {
value = map.optionalFrom(["a", "b"], transformation: { thing in
if let a = thing as? Int {
return a + 1
}
throw MapperError.customError(field: nil, message: "")
})
}
}

do {
let test = try Test(map: Mapper(JSON: [:]))
XCTAssertNil(test.value)
} catch {
XCTFail("Shouldn't have failed to create Test")
}
}
}