-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Marshal mapping #3
Changes from 5 commits
f813ef8
edd1ffe
6c80443
f2301ef
72edb9b
486acf9
b29b078
8bb0cc8
9293211
387e485
8213c6f
6aa5a73
8fefcab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// | ||
// MarshalMapping.swift | ||
// ACKReactiveExtensions | ||
// | ||
// Created by Jakub Olejník on 06/04/2017. | ||
// Ackee | ||
// | ||
|
||
import Result | ||
import Marshal | ||
import ReactiveSwift | ||
|
||
/** | ||
* Protocol that allows creation of custom Marshal errors | ||
*/ | ||
public protocol MarshalErrorCreatable: Error { | ||
|
||
/** | ||
* Create error containing passed `MarshalError` | ||
* | ||
* - parameter marshalError: `MarshalError` which should be wrapped | ||
*/ | ||
static func createMarshalError(_ marshalError: MarshalError) -> Self | ||
} | ||
|
||
extension MarshalError: MarshalErrorCreatable { | ||
public static func createMarshalError(_ marshalError: MarshalError) -> MarshalError { | ||
return marshalError | ||
} | ||
} | ||
|
||
extension SignalProtocol where Value == Any, Error: MarshalErrorCreatable { | ||
|
||
/** | ||
* Map value as `Unmarshaling` object | ||
* | ||
* - parameter key: If your objects are contained within dictionary pass the key here | ||
*/ | ||
public func mapResponseMarshal<Model>(for key: KeyType? = nil) -> Signal<Model, Error> where Model: Unmarshaling { | ||
return attemptMap { json in | ||
Result { | ||
guard let marshaledJSON = json as? MarshaledObject | ||
else { | ||
assertionFailure("json isn't any of the known MarshaledObject types (Dictionary or Array)") | ||
throw NSError(domain: "", code: 0, userInfo: nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we get rid of this and throw some better error? |
||
} | ||
if let key = key { | ||
return try marshaledJSON.value(for: key) | ||
} else { | ||
return try Model.init(object: marshaledJSON) | ||
} | ||
} | ||
.mapError { Error.createMarshalError($0) } | ||
} | ||
} | ||
|
||
/** | ||
* Map value as `Unmarshaling` object | ||
* | ||
* - parameter key: If your objects are contained within dictionary pass the key here | ||
*/ | ||
public func mapResponseMarshal<Model>(for key: KeyType? = nil) -> Signal<[Model], Error> where Model: Unmarshaling { | ||
return attemptMap { json in | ||
Result { | ||
if let key = key, let marshaledJSON = json as? MarshaledObject { | ||
return try marshaledJSON.value(for: key) | ||
} | ||
else if let marshaledArray = json as? [MarshaledObject] { | ||
let dummyKey = "dummyKey" | ||
return try [dummyKey: marshaledArray].value(for: dummyKey) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
else { | ||
assertionFailure("json isn't any of the known MarshaledObject types (Dictionary or Array)") | ||
throw NSError(domain: "", code: 0, userInfo: nil) | ||
} | ||
} | ||
.mapError { Error.createMarshalError($0) } | ||
} | ||
} | ||
|
||
/** | ||
* Map value as `ValueType` | ||
* | ||
* - parameter key: If your objects are contained within dictionary pass the key here | ||
*/ | ||
public func mapResponseMarshal<Model>(for key: KeyType) -> Signal<Model, Error> where Model: ValueType { | ||
return attemptMap { json in | ||
Result { | ||
guard let marshaledJSON = json as? MarshaledObject | ||
else { | ||
assertionFailure("json isn't any of the known MarshaledObject types (Dictionary or Array)") | ||
throw NSError(domain: "", code: 0, userInfo: nil) | ||
} | ||
return try marshaledJSON.value(for: key) | ||
} | ||
.mapError { Error.createMarshalError($0) } | ||
} | ||
} | ||
} | ||
|
||
extension SignalProducerProtocol where Value == Any, Error: MarshalErrorCreatable { | ||
/** | ||
* Map value as `Unmarshaling` object | ||
* | ||
* - parameter key: If your objects are contained within dictionary pass the key here | ||
*/ | ||
public func mapResponseMarshal<Model>(for key: KeyType? = nil) -> SignalProducer<Model, Error> where Model: Unmarshaling { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would change it to |
||
return lift { $0.mapResponseMarshal(for: key) } | ||
} | ||
|
||
/** | ||
* Map value as `Unmarshaling` object | ||
* | ||
* - parameter key: If your objects are contained within dictionary pass the key here | ||
*/ | ||
public func mapResponseMarshal<Model>(for key: KeyType? = nil) -> SignalProducer<[Model], Error> where Model: Unmarshaling { | ||
return lift { $0.mapResponseMarshal(for: key) } | ||
} | ||
|
||
/** | ||
* Map value as `ValueType` | ||
* | ||
* - parameter key: If your objects are contained within dictionary pass the key here | ||
*/ | ||
public func mapResponseMarshal<Model>(for key: KeyType) -> SignalProducer<Model, Error> where Model: ValueType { | ||
return lift { $0.mapResponseMarshal(for: key) } | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be also nice to have some test to call the public API There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean something like this? |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be
_ marshalError: NSError
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't see any reason why? As far as Marshal is throwing
MarshalError
s.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aaaaa I see...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now when all errors thrown by the extension are really
MarshalError
s this is correct.