mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-06-17 05:29:58 +08:00
Allow numbers to map to strings
This commit is contained in:
@@ -150,13 +150,13 @@ BOOL RKObjectIsValueEqualToValue(id sourceValue, id destinationValue) {
|
||||
if ([destinationType isSubclassOfClass:[NSSet class]]) {
|
||||
return [NSSet setWithArray:value];
|
||||
}
|
||||
} else if ([sourceType isSubclassOfClass:[NSNumber class]]) {
|
||||
// Number -> Date
|
||||
if ([destinationType isSubclassOfClass:[NSDate class]]) {
|
||||
return [NSDate dateWithTimeIntervalSince1970:[(NSNumber*)value intValue]];
|
||||
} else if (([sourceType isSubclassOfClass:NSClassFromString(@"__NSCFBoolean")] || [sourceType isSubclassOfClass:NSClassFromString(@"NSCFBoolean")]) && [destinationType isSubclassOfClass:[NSString class]]) {
|
||||
return ([value boolValue] ? @"true" : @"false");
|
||||
}
|
||||
} else if ([sourceType isSubclassOfClass:[NSNumber class]] && [destinationType isSubclassOfClass:[NSDate class]]) {
|
||||
// Number -> Date
|
||||
return [NSDate dateWithTimeIntervalSince1970:[(NSNumber*)value intValue]];
|
||||
} else if ( ([sourceType isSubclassOfClass:NSClassFromString(@"__NSCFBoolean")] ||
|
||||
[sourceType isSubclassOfClass:NSClassFromString(@"NSCFBoolean")] ) &&
|
||||
[destinationType isSubclassOfClass:[NSString class]]) {
|
||||
return ([value boolValue] ? @"true" : @"false");
|
||||
} else if ([destinationType isSubclassOfClass:[NSString class]] && [value respondsToSelector:@selector(stringValue)]) {
|
||||
return [value stringValue];
|
||||
}
|
||||
|
||||
@@ -77,6 +77,21 @@
|
||||
[operation release];
|
||||
}
|
||||
|
||||
- (void)itShouldSuccessfullyMapNumbersToStrings {
|
||||
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[TestMappable class]];
|
||||
[mapping mapKeyPath:@"number" toAttribute:@"boolString"];
|
||||
TestMappable* object = [[[TestMappable alloc] init] autorelease];
|
||||
|
||||
id<RKParser> parser = [[RKParserRegistry sharedRegistry] parserForMIMEType:@"application/json"];
|
||||
id data = [parser objectFromString:@"{\"number\":123}" error:nil];
|
||||
|
||||
RKObjectMappingOperation* operation = [[RKObjectMappingOperation alloc] initWithSourceObject:data destinationObject:object mapping:mapping];
|
||||
BOOL success = [operation performMapping:nil];
|
||||
assertThatBool(success, is(equalToBool(YES)));
|
||||
assertThat(object.boolString, is(equalTo(@"123")));
|
||||
[operation release];
|
||||
}
|
||||
|
||||
- (void)itShouldFailTheMappingOperationIfKeyValueValidationSetsAnError {
|
||||
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[TestMappable class]];
|
||||
[mapping mapAttributes:@"boolString", nil];
|
||||
|
||||
Reference in New Issue
Block a user