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

Add from supporting array of keys with custom transformation #131

Merged
merged 1 commit into from
Dec 4, 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 `from` Array of Fields for Custom Transformation
[Brett Jones](https://github.com/brodney)
[#131](https://github.com/lyft/mapper/pull/131)

## Bug Fixes

Expand Down
21 changes: 21 additions & 0 deletions Sources/Mapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,27 @@ public struct Mapper {
throw MapperError.missingFieldError(field: fields.joined(separator: ", "))
}

/// Get a Convertible value from the specified list of fields. This returns the first value produced in
/// order based on the array of fields.
///
/// - parameter fields: The array of fields to retrieve from the source data
/// - parameter transformation: The transformation function used to create the expected value
///
/// - throws: MapperError.missingFieldError if none of the fields have an acceptable value.
///
/// - returns: The value of type T for the given field
public func from<T: Convertible>(_ fields: [String], transformation: (Any) throws -> T)
throws -> T where T == T.ConvertedType
{
for field in fields {
if let value: T = try? self.from(field, transformation: transformation) {
return value
}
}

throw MapperError.missingFieldError(field: fields.joined(separator: ", "))
}

// MARK: - Custom Transformation

/// Get a typed value from the given field by using the given transformation
Expand Down
52 changes: 52 additions & 0 deletions Tests/MapperTests/CustomTransformationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,58 @@ final class CustomTransformationTests: XCTestCase {
}
}

func testCustomTransformationArrayOfKeys() {
struct Test: Mappable {
let value: Int
init(map: Mapper) throws {
value = try map.from(["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 testCustomTransformationArrayOfKeysThrows() {
struct Test: Mappable {
let value: Int
init(map: Mapper) throws {
value = try map.from(["a", "b"], transformation: { _ in
throw MapperError.customError(field: nil, message: "")
})
}
}

let test = try? Test(map: Mapper(JSON: ["a": "##", "b": 1]))
XCTAssertNil(test)
}

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

let test = try? Test(map: Mapper(JSON: [:]))
XCTAssertNil(test)
}

func testOptionalCustomTransformationArrayOfKeys() {
struct Test: Mappable {
let value: Int?
Expand Down