From 044f17c2e1823a240bf27d1c26eeeaffe89d98df Mon Sep 17 00:00:00 2001 From: Blake Watters Date: Thu, 25 Feb 2010 11:03:25 -0500 Subject: [PATCH] More nasty ass hacks to the model mapper to get GG out of dry dock --- Code/RKModelMapper.m | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/Code/RKModelMapper.m b/Code/RKModelMapper.m index 2bb605fb..a82b9f31 100644 --- a/Code/RKModelMapper.m +++ b/Code/RKModelMapper.m @@ -178,17 +178,41 @@ [self setRelationshipsOfModel:object fromJSONDictionary:dict]; } +- (NSString*)selectorFromMapping:(id)mapping forMappingFormat:(RKMappingFormat)format { + if ([mapping isKindOfClass:[NSArray class]]) { + if (RKMappingFormatXML == format) { + return [(NSArray*)mapping objectAtIndex:0]; + } else if (RKMappingFormatJSON == format) { + return [(NSArray*)mapping objectAtIndex:1]; + } + } else { + return mapping; + } +} + - (void)setPropertiesOfModel:(id)model fromJSONDictionary:(NSDictionary*)dict { - for (NSString* selector in [[model class] elementToPropertyMappings]) { - NSString* propertyName = [[[model class] elementToPropertyMappings] objectForKey:selector]; + for (id mapping in [[model class] elementToPropertyMappings]) { + NSString* propertyName = [[[model class] elementToPropertyMappings] objectForKey:mapping]; + NSString* selector = [self selectorFromMapping:mapping forMappingFormat:RKMappingFormatJSON]; NSString* propertyType = [self typeNameForProperty:propertyName ofClass:[model class] typeHint:nil]; NSString* elementName = nil; - if ([[model class] respondsToSelector:@selector(formatElementName:forMappingFormat:)]) { + + // TODO: This shit needs to go... + if ([mapping isKindOfClass:[NSString class]] && [[model class] respondsToSelector:@selector(formatElementName:forMappingFormat:)]) { elementName = [[model class] formatElementName:selector forMappingFormat:RKMappingFormatJSON]; } else { elementName = selector; - } - id propertyValue = [dict objectForKey:elementName]; + } + +// id propertyValue = [dict objectForKey:elementName]; + id propertyValue = nil; + @try { + propertyValue = [dict valueForKeyPath:elementName]; + } + @catch (NSException * e) { + NSLog(@"Encountered exception %@ when asking %@ for valueForKeyPath %@", e, dict, elementName); + } + //NSLog(@"Asked JSON dictionary %@ for object with key %@. Got %@", dict, elementName, propertyValue); // Types of objects SBJSON does not handle: if ([propertyType isEqualToString:@"NSDate"]) { @@ -213,7 +237,7 @@ // If the collection key doesn't appear, we will not set the collection to nil. NSString* collectionKey = [self containingElementNameForSelector:selector]; // Used to figure out what class to map to, since we don't have element names for the dictionaries in the array - NSString* objectKey = [self childElementNameForSelelctor:selector]; + NSString* objectKey = [self childElementNameForSelector:selector]; NSArray* objects = [dict objectForKey:collectionKey]; if (objects != nil) { for (NSDictionary* childDict in objects) { @@ -328,11 +352,11 @@ } - (void)setPropertiesOfModel:(id)model fromXML:(Element*)XML { - for (NSString* selector in [[model class] elementToPropertyMappings]) { - NSString* propertyName = [[[model class] elementToPropertyMappings] objectForKey:selector]; + for (id mapping in [[model class] elementToPropertyMappings]) { + NSString* selector = [self selectorFromMapping:mapping forMappingFormat:RKMappingFormatXML]; + NSString* propertyName = [[[model class] elementToPropertyMappings] objectForKey:mapping]; NSString* typeHint; - // TODO: Figure out subselectors here... Element* propertyElement = nil; if ([self isSelectorGrouped:selector]) { selector = [selector stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"[]"]]; @@ -401,7 +425,7 @@ return [[selector componentsSeparatedByString:@" > "] objectAtIndex:0]; } -- (NSString*)childElementNameForSelelctor:(NSString*)selector { +- (NSString*)childElementNameForSelector:(NSString*)selector { return [[selector componentsSeparatedByString:@" > "] objectAtIndex:1]; }