Skip to content

Commit

Permalink
valueBlock should no longer be called if value in JSON is nil, and ig…
Browse files Browse the repository at this point in the history
…noreMissingFields property turned to YES. Fixes #126.
  • Loading branch information
DenTelezhkin committed Jul 1, 2016
1 parent b52e4d1 commit d301760
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 9 deletions.
3 changes: 2 additions & 1 deletion EasyMapping/EKManagedObjectMapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ - (id)fillObject:(id)object fromExternalRepresentation:(NSDictionary *)externalR
onObject:object
fromRepresentation:representation
inContext:self.importer.context
respectPropertyType:mapping.respectPropertyFoundationTypes];
respectPropertyType:mapping.respectPropertyFoundationTypes
ignoreMissingFields:mapping.ignoreMissingFields];
}];
[mapping.hasOneMappings enumerateKeysAndObjectsUsingBlock:^(id key, EKRelationshipMapping * relationship, BOOL * stop)
{
Expand Down
3 changes: 2 additions & 1 deletion EasyMapping/EKMapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ + (id)fillObject:(id)object fromExternalRepresentation:(NSDictionary *)externalR
[EKPropertyHelper setProperty:obj
onObject:object
fromRepresentation:representation
respectPropertyType:mapping.respectPropertyFoundationTypes];
respectPropertyType:mapping.respectPropertyFoundationTypes
ignoreMissingFields:mapping.ignoreMissingFields];
}];
[mapping.hasOneMappings enumerateKeysAndObjectsUsingBlock:^(id key, EKRelationshipMapping * valueMapping, BOOL *stop) {
NSDictionary * value = [valueMapping extractObjectFromRepresentation:representation];
Expand Down
9 changes: 6 additions & 3 deletions EasyMapping/EKPropertyHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@
+ (void) setProperty:(EKPropertyMapping *)propertyMapping
onObject:(id)object
fromRepresentation:(NSDictionary *)representation
respectPropertyType:(BOOL)respectPropertyType;
respectPropertyType:(BOOL)respectPropertyType
ignoreMissingFields:(BOOL)ignoreMissingFields;

+ (void) setProperty:(EKPropertyMapping *)propertyMapping
onObject:(id)object
fromRepresentation:(NSDictionary *)representation
inContext:(NSManagedObjectContext *)context
respectPropertyType:(BOOL)respectPropertyType;
respectPropertyType:(BOOL)respectPropertyType
ignoreMissingFields:(BOOL)ignoreMissingFields;

+ (id)getValueOfProperty:(EKPropertyMapping *)propertyMapping
fromRepresentation:(NSDictionary *)representation;
fromRepresentation:(NSDictionary *)representation
ignoreMissingFields:(BOOL)ignoreMissingFields;

+ (id)getValueOfManagedProperty:(EKPropertyMapping *)mapping
fromRepresentation:(NSDictionary *)representation
Expand Down
12 changes: 8 additions & 4 deletions EasyMapping/EKPropertyHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ + (id)propertyRepresentation:(id)value forObject:(id)object withPropertyName:(NS
#pragma mark Property accessor methods

+ (void)setProperty:(EKPropertyMapping *)propertyMapping onObject:(id)object
fromRepresentation:(NSDictionary *)representation respectPropertyType:(BOOL)respectPropertyType
fromRepresentation:(NSDictionary *)representation respectPropertyType:(BOOL)respectPropertyType ignoreMissingFields:(BOOL)ignoreMIssingFields
{
id value = [self getValueOfProperty:propertyMapping fromRepresentation:representation];
id value = [self getValueOfProperty:propertyMapping fromRepresentation:representation ignoreMissingFields:ignoreMIssingFields];
if (value == (id)[NSNull null]) {
if (![self propertyNameIsScalar:propertyMapping.property fromObject:object]) {
[self setValue:nil onObject:object forKeyPath:propertyMapping.property];
Expand All @@ -118,6 +118,7 @@ + (void) setProperty:(EKPropertyMapping *)propertyMapping
fromRepresentation:(NSDictionary *)representation
inContext:(NSManagedObjectContext *)context
respectPropertyType:(BOOL)respectPropertyType
ignoreMissingFields:(BOOL)ignoreMissingFields
{
id value = [self getValueOfManagedProperty:propertyMapping
fromRepresentation:representation
Expand Down Expand Up @@ -174,12 +175,15 @@ +(void)addValue:(id)value onObject:(id)object forKeyPath:(NSString *)keyPath
}
}

+ (id)getValueOfProperty:(EKPropertyMapping *)propertyMapping fromRepresentation:(NSDictionary *)representation
+ (id)getValueOfProperty:(EKPropertyMapping *)propertyMapping fromRepresentation:(NSDictionary *)representation ignoreMissingFields:(BOOL)ignoreMissingFields
{
id value = nil;

if (propertyMapping.valueBlock) {
value = propertyMapping.valueBlock(propertyMapping.keyPath, [representation valueForKeyPath:propertyMapping.keyPath]);
value = [representation valueForKeyPath:propertyMapping.keyPath];
if (value != nil || !ignoreMissingFields) {
value = propertyMapping.valueBlock(propertyMapping.keyPath, value);
}
}
else {
value = [representation valueForKeyPath:propertyMapping.keyPath];
Expand Down
1 change: 1 addition & 0 deletions EasyMappingExample/Classes/Providers/MappingProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
+ (EKObjectMapping *)personWithRelativeMapping;
+ (EKObjectMapping *)addressMapping;
+ (EKObjectMapping *)nativeMappingWithNullPropertie;
+ (EKObjectMapping *)personMappingThatAssertsOnNilInValueBlock;

@end
19 changes: 19 additions & 0 deletions EasyMappingExample/Classes/Providers/MappingProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,25 @@ + (EKObjectMapping *)personMapping
}];
}

+(EKObjectMapping *)personMappingThatAssertsOnNilInValueBlock
{
return [EKObjectMapping mappingForClass:[Person class] withBlock:^(EKObjectMapping *mapping) {
NSDictionary *genders = @{ @"male": @(GenderMale), @"female": @(GenderFemale) };
[mapping mapPropertiesFromArray:@[@"name", @"email"]];
[mapping mapKeyPath:@"gender" toProperty:@"gender" withValueBlock:^(NSString *key, id value) {
if (value == nil) { [[[NSException alloc] initWithName:@"Received nil value" reason:@"In value block when ignore missing fields is turned on" userInfo:nil] raise]; }
return genders[value];
} reverseBlock:^id(id value) {
return [genders allKeysForObject:value].lastObject;
}];
[mapping hasOne:[Car class] forKeyPath:@"car"];
[mapping hasMany:[Phone class] forKeyPath:@"phones"];
[mapping mapKeyPath:@"socialURL" toProperty:@"socialURL"
withValueBlock:[EKMappingBlocks urlMappingBlock]
reverseBlock:[EKMappingBlocks urlReverseMappingBlock]];
}];
}

+ (EKObjectMapping *)personWithCarMapping
{
return [EKObjectMapping mappingForClass:[Person class] withBlock:^(EKObjectMapping *mapping) {
Expand Down
19 changes: 19 additions & 0 deletions EasyMappingExample/Tests/XCTests/EKMappingTestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,23 @@ - (void)testIgnoreMissingFieldsProperty
XCTAssertNotNil(person.phones);
}

- (void) testIgnoreMissingFieldsWorksForProperties {
NSDictionary *externalRepresentation = [CMFixture buildUsingFixture:@"Person"];

[Person registerMapping:[MappingProvider personMappingThatAssertsOnNilInValueBlock]];
[Car registerMapping:[MappingProvider carMapping]];
[Phone registerMapping:[MappingProvider phoneMapping]];
EKObjectMapping *personMapping = [Person objectMapping];

Person *person = [EKMapper objectFromExternalRepresentation:externalRepresentation
withMapping:personMapping];

personMapping.ignoreMissingFields = YES;
XCTAssertNotNil(person.car);
XCTAssertNotNil(person.phones);
[EKMapper fillObject:person fromExternalRepresentation:@{@"id":@23,@"name":@"Lucas"} withMapping:personMapping];
XCTAssert(person.gender == GenderMale);
XCTAssert([person.socialURL.absoluteString isEqual:@"https://www.twitter.com/EasyMapping"]);
}

@end

0 comments on commit d301760

Please sign in to comment.