mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-23 04:20:21 +08:00
Merge branch 'numbers-can-map-to-strings' of https://github.com/crayment/RestKit into crayment-numbers-can-map-to-strings
Conflicts: Code/ObjectMapping/RKObjectMappingOperation.m Specs/ObjectMapping/RKObjectMappingOperationSpec.m
This commit is contained in:
@@ -150,17 +150,16 @@ BOOL RKObjectIsValueEqualToValue(id sourceValue, id destinationValue) {
|
||||
if ([destinationType isSubclassOfClass:[NSSet class]]) {
|
||||
return [NSSet setWithArray:value];
|
||||
}
|
||||
} else if ([sourceType isSubclassOfClass:[NSNumber class]]) {
|
||||
if ([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]]) {
|
||||
// Boolean -> String
|
||||
return ([value boolValue] ? @"true" : @"false");
|
||||
} else if ([destinationType isSubclassOfClass:[NSDecimalNumber class]]) {
|
||||
// Number -> Decimal Number
|
||||
return [NSDecimalNumber decimalNumberWithDecimal:[value decimalValue]];
|
||||
}
|
||||
} else if ([sourceType isSubclassOfClass:[NSNumber class]] && [destinationType isSubclassOfClass:[NSDate class]]) {
|
||||
// Number -> Date
|
||||
return [NSDate dateWithTimeIntervalSince1970:[(NSNumber*)value intValue]];
|
||||
} else if ([sourceType isSubclassOfClass:[NSNumber class]] && [destinationType isSubclassOfClass:[NSDecimalNumber class]]) {
|
||||
// Number -> Decimal Number
|
||||
return [NSDecimalNumber decimalNumberWithDecimal:[value decimalValue]];
|
||||
} 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];
|
||||
}
|
||||
|
||||
@@ -109,6 +109,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