krzysztofzablocki / PropertyMapper

Property mapping for Objective-C iOS apps.

Home Page:http://merowing.info

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ability to Propagate Errors when using Custom Boxing / Call

lazarev opened this issue · comments

Lets say I have a JSON object like this:

  { 
       success: false,
       error: {code: 1, message: "Something was wrong!"},
       data: nil
  }

And I wan't to map object like this:

@interface Response : NSObject
@property (assign, nonatomic) BOOL    isSuccess;
@property (strong, nonatomic) id      data;
@property (strong, nonatomic) NSError *error;
@end

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?

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:

User

@interface User : NSObject
@property (assign, nonatomic) NSString* name;
@property (strong, nonatomic) NSString* id;
@end

NSDictionary* userMapping = @{
@"name": KZProperty(name).isRequired(),
@"id": KZProperty(id).isRequired()
}

Server Response

@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