Skip to content

Commit

Permalink
Supports user provided JSONDecoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gunter Hager committed Aug 2, 2017
1 parent c247d1e commit e6eaedf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion ReactiveCodable/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0.2</string>
<string>1.0.3</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
22 changes: 12 additions & 10 deletions ReactiveCodable/ReactiveCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ public enum ReactiveCodableError: Error {
extension SignalProtocol where Value == Data {
/// Maps the given JSON object within the stream to an object of given `type`.
///
/// - parameter type: The type of the object that should be returned
///
/// - returns: A new Signal emitting the decoded object.
public func mapToType<T: Decodable>(_ type: T.Type) -> Signal<T, ReactiveCodableError> {
/// - Parameters:
/// - type: The type of the object that should be returned
/// - decoder: A `JSONDecoder` to use. This allows configuring the decoder.
/// - Returns: A new Signal emitting the decoded object.
public func mapToType<T: Decodable>(_ type: T.Type, decoder: JSONDecoder = JSONDecoder()) -> Signal<T, ReactiveCodableError> {
return signal
.mapError { ReactiveCodableError.underlying($0) }
.attemptMap { json -> Result<T, ReactiveCodableError> in
return unwrapThrowableResult { try JSONDecoder().decode(type.self, from: json) }
return unwrapThrowableResult { try decoder.decode(type.self, from: json) }

// let info = [NSLocalizedFailureReasonErrorKey: "The provided `Value` could not be cast to `Data` or there is no value at the given `rootKeys`: \(String(describing: rootKeys))"]
// let error = NSError(domain: ReactiveCodableErrorDomain, code: -1, userInfo: info)
Expand All @@ -55,11 +56,12 @@ extension SignalProducerProtocol where Value == Data {

/// Maps the given JSON object within the stream to an object of given `type`
///
/// - parameter type: The type of the object that should be returned
///
/// - returns: A new SignalProducer emitting the decoded object.
public func mapToType<T: Decodable>(_ type: T.Type) -> SignalProducer<T, ReactiveCodableError> {
return producer.lift { $0.mapToType(type) }
/// - Parameters:
/// - type: The type of the object that should be returned
/// - decoder: A `JSONDecoder` to use. This allows configuring the decoder.
/// - Returns: A new SignalProducer emitting the decoded object.
public func mapToType<T: Decodable>(_ type: T.Type, decoder: JSONDecoder = JSONDecoder()) -> SignalProducer<T, ReactiveCodableError> {
return producer.lift { $0.mapToType(type, decoder: decoder) }
}

}
Expand Down
15 changes: 14 additions & 1 deletion ReactiveCodableTests/ReactiveCodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ class ReactiveCodableTests: XCTestCase {

XCTAssertNotNil(error, "error should not be nil")
XCTAssertEqual(error?.nsError, sentError, "the sent error should be wrapped in an .Underlying error")
}
}

func testOwnDecoder() {
var tasks: [Task]?
let decoder = JSONDecoder()
mockData.load("tasks")
.mapToType([Task].self, decoder: decoder)
.startWithResult { tasks = $0.value }

XCTAssertNotNil(tasks, "mapToType should not return nil tasks")
XCTAssertTrue((tasks!).count == 3, "mapJSON returned wrong number of tasks")
}



}

0 comments on commit e6eaedf

Please sign in to comment.