Open lazarev opened 9 years ago
I'm unsure what you mean, mapping this response to the object is trivial, and you have a expandable system of validations and you can specify your own so they can fail if an object does not fullfill your requirements.
Can you elaborate more?
Looks like It wasn't very good example to explain.
Lets say we have data models described below:
@interface User : NSObject
@property (assign, nonatomic) NSString* name;
@property (strong, nonatomic) NSString* id;
@end
NSDictionary* userMapping = @{
@"name": KZProperty(name).isRequired(),
@"id": KZProperty(id).isRequired()
}
@interface AuthenticationResponse : NSObject
@property (assign, nonatomic) User* user;
@property (strong, nonatomic) NSString* session;
@end
NSDictionary* responseMapping = @{
@"user": KZCall(userFromObject:, user),
@"session": KZProperty(session)
}
- (id)userFromObject:(NSDictionary *)payload
{
User* user = [[User alloc] init];
NSArray* errors;
if ([KZPropertyMapper mapValuesFrom:payload toInstance:user usingMapping: userMapping errors:&errors]) {
return user;
} else {
// How to notify AuthenticationResponse mapping caller about problems in user mapping?
}
}
Now when I want to map some dictionary on server response, I want this process to be finished with error if error appeared during user property unboxing:
NSArray* errors;
AuthenticationResponse* response;
[KZPropertyMapper mapValuesFrom: payload
toInstance: response
usingMapping: responseMapping
errors: &errors]
Here I want to receive errors if something wrong with User object. How to solve this problem?
I get it now, you want to have ability to propagate errors up the chain, so we could add functionality to the mapper that when returning an NSError from a boxing/call it should treat that as failure and fail the whole mapping.
@lazarev Would you like to try implementing that? should be straighforward, if not I'll try to find some time this weekend / next week to do it
Lets say I have a JSON object like this:
And I wan't to map object like this:
I can map error with KZCall or by custom boxing. But this methods unable to specify any errors during certain class instantiation or mapping. Is there any way to fix it with current library implementation?