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

Commit

Permalink
Add optionalFrom for array of RawRepresentables (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith Smiley committed Nov 1, 2017
1 parent 5c88b9a commit c4eaff1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
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
- Add `optionalFrom` for arrays of `RawRepresentable`s
[Keith Smiley](https://github.com/keith)
[#125](https://github.com/lyft/mapper/pull/125)

# 7.2.0

Expand Down
18 changes: 18 additions & 0 deletions Sources/Mapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ public struct Mapper {
return rawValues.flatMap { T(rawValue: $0) ?? defaultValue }
}

/// Get an optional array of RawRepresentable values from a field in the the source data.
///
/// - note: If T.init(rawValue:) fails given the T.RawValue from the array of source data, that value will
/// be replaced by the passed defaultValue, which defaults to nil. The resulting array is
/// flatMapped and all nils are removed. This means that any unrecognized values will be removed
/// or replaced with the default. This ensures backwards compatibility if your source data has
/// keys that your mapping layer doesn't know about yet.
///
/// - parameter field: The field to use from the source data
/// - parameter defaultValue: The value to use if the rawValue initializer fails
///
/// - returns: An array of the RawRepresentable value, with all nils removed or nil if anything throws
public func optionalFrom<T: RawRepresentable>(_ field: String, defaultValue: T? = nil) ->
[T]? where T.RawValue: Convertible, T.RawValue == T.RawValue.ConvertedType
{
return try? self.from(field, defaultValue: defaultValue)
}

// MARK: - T: Mappable

/// Get a Mappable value from the given field in the source data
Expand Down
30 changes: 30 additions & 0 deletions Tests/MapperTests/RawRepresentibleValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,34 @@ final class RawRepresentibleValueTests: XCTestCase {
XCTFail("Expected no errors, got \(error)")
}
}

func testOptionalArrayRawValues() {
enum Value: String {
case first
}

let map = Mapper(JSON: ["values": ["first", "there"]])
let values: [Value]? = map.optionalFrom("values")
XCTAssertEqual(values ?? [], [.first])
}

func testOptionalArrayRawValuesMissingKey() {
enum Value: String {
case first
}

let map = Mapper(JSON: [:])
let values: [Value]? = map.optionalFrom("values")
XCTAssertNil(values)
}

func testOptionalArrayDefaultValues() {
enum Value: String {
case first
}

let map = Mapper(JSON: ["values": ["invalid"]])
let values: [Value]? = map.optionalFrom("values", defaultValue: .first)
XCTAssertEqual(values ?? [], [.first])
}
}

0 comments on commit c4eaff1

Please sign in to comment.