From 7bf3e0f2731a599dce0499eab0ffcbd22aee73e7 Mon Sep 17 00:00:00 2001 From: Blake Watters Date: Wed, 8 Jun 2011 19:07:56 -0400 Subject: [PATCH] Added support for mapping boolean strings to NSNumber with values t or f in addition to true and false. refs #130 --- Code/ObjectMapping/RKObjectMappingOperation.m | 6 ++-- .../RKObjectMappingNextGenSpec.m | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Code/ObjectMapping/RKObjectMappingOperation.m b/Code/ObjectMapping/RKObjectMappingOperation.m index ccd71895..2def9fb9 100644 --- a/Code/ObjectMapping/RKObjectMappingOperation.m +++ b/Code/ObjectMapping/RKObjectMappingOperation.m @@ -85,9 +85,11 @@ } else if ([destinationType isSubclassOfClass:[NSNumber class]]) { // String -> Number NSString* lowercasedString = [(NSString*)value lowercaseString]; - if ([lowercasedString isEqualToString:@"true"] || [lowercasedString isEqualToString:@"false"]) { + NSSet* trueStrings = [NSSet setWithObjects:@"true", @"t", nil]; + NSSet* booleanStrings = [trueStrings setByAddingObjectsFromSet:[NSSet setWithObjects:@"false", @"f", nil]]; + if ([booleanStrings containsObject:lowercasedString]) { // Handle booleans encoded as Strings - return [NSNumber numberWithBool:[lowercasedString isEqualToString:@"true"]]; + return [NSNumber numberWithBool:[trueStrings containsObject:lowercasedString]]; } else { return [NSNumber numberWithDouble:[(NSString*)value doubleValue]]; } diff --git a/Specs/ObjectMapping/RKObjectMappingNextGenSpec.m b/Specs/ObjectMapping/RKObjectMappingNextGenSpec.m index 92c9c3d3..3c0a51c5 100644 --- a/Specs/ObjectMapping/RKObjectMappingNextGenSpec.m +++ b/Specs/ObjectMapping/RKObjectMappingNextGenSpec.m @@ -746,6 +746,37 @@ [expectThat([[user isDeveloper] boolValue]) should:be(YES)]; } +- (void)itShouldMapAShortTrueStringToANumberBool { + RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[RKExampleUser class]]; + RKObjectAttributeMapping* websiteMapping = [RKObjectAttributeMapping mappingFromKeyPath:@"is_developer" toKeyPath:@"isDeveloper"]; + [mapping addAttributeMapping:websiteMapping]; + + NSDictionary* dictionary = [RKSpecParseFixtureJSON(@"user.json") mutableCopy]; + RKExampleUser* user = [RKExampleUser user]; + [dictionary setValue:@"T" forKey:@"is_developer"]; + RKObjectMappingOperation* operation = [[RKObjectMappingOperation alloc] initWithSourceObject:dictionary destinationObject:user objectMapping:mapping]; + NSError* error = nil; + [operation performMapping:&error]; + + [expectThat([[user isDeveloper] boolValue]) should:be(YES)]; +} + + +- (void)itShouldMapAShortFalseStringToANumberBool { + RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[RKExampleUser class]]; + RKObjectAttributeMapping* websiteMapping = [RKObjectAttributeMapping mappingFromKeyPath:@"is_developer" toKeyPath:@"isDeveloper"]; + [mapping addAttributeMapping:websiteMapping]; + + NSDictionary* dictionary = [RKSpecParseFixtureJSON(@"user.json") mutableCopy]; + RKExampleUser* user = [RKExampleUser user]; + [dictionary setValue:@"f" forKey:@"is_developer"]; + RKObjectMappingOperation* operation = [[RKObjectMappingOperation alloc] initWithSourceObject:dictionary destinationObject:user objectMapping:mapping]; + NSError* error = nil; + [operation performMapping:&error]; + + [expectThat([[user isDeveloper] boolValue]) should:be(NO)]; +} + - (void)itShouldMapAStringToANumber { RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[RKExampleUser class]]; RKObjectAttributeMapping* websiteMapping = [RKObjectAttributeMapping mappingFromKeyPath:@"lucky_number" toKeyPath:@"luckyNumber"];