Add assertions to prevent attempts to create request descriptor objects with inappropriate mappings. fixes #976

This commit is contained in:
Blake Watters
2012-10-14 14:57:02 -04:00
parent 1beb11ac1b
commit d823217993
7 changed files with 122 additions and 14 deletions

View File

@@ -22,6 +22,25 @@
//
#import "RKRequestDescriptor.h"
#import "RKObjectMapping.h"
#import "RKDynamicMapping.h"
static void RKAssertValidMappingForRequestDescriptor(RKMapping *mapping)
{
if ([mapping isKindOfClass:[RKObjectMapping class]]) {
if (! [[(RKObjectMapping *)mapping objectClass] isEqual:[NSMutableDictionary class]]) {
[NSException raise:NSInvalidArgumentException format:@"`RKRequestDescriptor` objects must be initialized with a mapping whose target class is `NSMutableDictionary`, got '%@' (see `[RKObjectMapping requestMapping]`)", [(RKObjectMapping *)mapping objectClass]];
}
} else if ([mapping isKindOfClass:[RKDynamicMapping class]]) {
[[(RKDynamicMapping *)mapping objectMappings] enumerateObjectsUsingBlock:^(RKObjectMapping *objectMapping, NSUInteger idx, BOOL *stop) {
if (! [objectMapping.objectClass isEqual:[NSMutableDictionary class]]) {
[NSException raise:NSInvalidArgumentException format:@"`RKRequestDescriptor` objects may only be initialized with `RKDynamicMapping` objects containing `RKObjectMapping` objects whose target class is `NSMutableDictionary`, got '%@' (see `[RKObjectMapping requestMapping]`)", objectMapping.objectClass];
}
}];
} else {
[NSException raise:NSInvalidArgumentException format:@"Expected an instance of `RKObjectMapping` or `RKDynamicMapping`, instead got '%@'", [mapping class]];
}
}
@interface RKRequestDescriptor ()
@@ -37,6 +56,7 @@
{
NSParameterAssert(mapping);
NSParameterAssert(objectClass);
RKAssertValidMappingForRequestDescriptor(mapping);
RKRequestDescriptor *requestDescriptor = [self new];
requestDescriptor.mapping = mapping;

View File

@@ -55,6 +55,13 @@ typedef RKObjectMapping *(^RKDynamicMappingDelegateBlock)(id);
*/
- (void)setObjectMapping:(RKObjectMapping *)objectMapping whenValueOfKeyPath:(NSString *)keyPath isEqualTo:(id)value;
/**
Returns an array of object mappings that have been registered with the receiver.
@return An array of `RKObjectMapping` objects registered with the receiver.
*/
@property (nonatomic, readonly) NSArray *objectMappings;
///-----------------------------------------------------------------
/// @name Retrieving the Object Mapping for an Object Representation
///-----------------------------------------------------------------

View File

@@ -43,6 +43,11 @@
return self;
}
- (NSArray *)objectMappings
{
return [self.matchers valueForKey:@"objectMapping"];
}
- (void)setObjectMapping:(RKObjectMapping *)objectMapping whenValueOfKeyPath:(NSString *)keyPath isEqualTo:(id)expectedValue
{
RKLogDebug(@"Adding dynamic object mapping for key '%@' with value '%@' to destination class: %@", keyPath, expectedValue, NSStringFromClass(objectMapping.objectClass));