-
Notifications
You must be signed in to change notification settings - Fork 89
Conversation
Thanks for adding this right when I was having a conversation about this! There's one big non obvious issue we need to deal with here. Let me pull together an example. |
So the issue here is backwards compatibility. We've already gotten bitten by this once. So the case is pretty simple, but it works like this: Version 1 of your model: enum Value: String {
case A = "a"
}
let json: NSDictionary = ["values": ["a"]] Then you update to version 2 of your model: enum Value: String {
case A = "a"
case B = "b"
}
let json: NSDictionary = ["values": ["a", "b"]] But then, if you're forcing the values on your original model, the values you're getting from the server are now incompatible (version 1): enum Value: String {
case A = "a"
}
let json: NSDictionary = ["values": ["a", "b"]] I think that we need to expose this as much as possible to consumers so it makes it as difficult as possible to shoot yourself in the foot. Currently you can also have this problem with a single enum property as well, if you don't fallback to some default value. |
Hmm yes agree this is a great way to self-inflict foot wounds.
..to accept a default value? So the above becomes: ? |
I think we should push this concern to the implementer instead. I think that often (like the case in my example) you won't have a sensible default value for this. And really what you'll want to do is public func from<T: RawRepresentable>(field: String) throws -> [T?] |
Sounds good @keith. Would you like me to amend the PR with those changes? |
That would be fantastic! |
Prevents situtations where introducing new values will cause parsing to fail.
@keith pushed up the discussed changes. The only caveat is that if you don't specify that you are expecting an array of optionals for example by adding flatMap to the end:
So for example in this instance if you forgot to add the flat map:
Then this ends up getting called (and parsing fails):
|
Seems like we either: a) Add a new method public func from<T: RawRepresentable>(field: String) throws -> [T] or public func from<T: RawRepresentable>(field: String) throws -> [T] and we flatMap inside that fc |
I'm surprised that a function that returns |
@keith the issue is that it is calling |
Ah yes. That will be going away as soon as I finish my branch of #7 |
Yea sorry, I'll try to get to that soon. It's mostly done, just need some final testing |
Looping back here (a few months later than I would have liked). I have a PR up for the other change here: #59 |
Thanks for getting this started! I've submitted #61 to implement this after the new changes. |
Allow mapping from arrays of enums. For example: