diff --git a/KeyValueObjectMapping.xcodeproj/xcshareddata/xcschemes/KeyValueObjectMappingTests.xcscheme b/KeyValueObjectMapping.xcodeproj/xcshareddata/xcschemes/KeyValueObjectMappingTests.xcscheme new file mode 100644 index 0000000..11c6f3a --- /dev/null +++ b/KeyValueObjectMapping.xcodeproj/xcshareddata/xcschemes/KeyValueObjectMappingTests.xcscheme @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/KeyValueObjectMapping/DCGenericConverter.m b/KeyValueObjectMapping/DCGenericConverter.m index 0fb3cf6..7c78a41 100644 --- a/KeyValueObjectMapping/DCGenericConverter.m +++ b/KeyValueObjectMapping/DCGenericConverter.m @@ -14,6 +14,7 @@ #import "DCNSSetConverter.h" #import "DCCustomParser.h" #import "DCKeyValueObjectMapping.h" +#import "DCArrayMapping.h" @interface DCGenericConverter() @property(nonatomic, strong) DCParserConfiguration *configuration; @@ -46,7 +47,13 @@ - (id)transformValue:(id)value forDynamicAttribute:(DCDynamicAttribute *)attribu if (parsedValue) { return parsedValue; } - + + //handle "implicit" arrays, a single instance of the element type represented as a dictionary + DCArrayMapping *mapper = [self.configuration arrayMapperForMapper:attribute.objectMapping]; + if (mapper) { + DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass:mapper.classForElementsOnArray andConfiguration:self.configuration]; + return [parser parseArray:[NSArray arrayWithObject:value] forParentObject:parentObject]; + } DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass:attribute.objectMapping.classReference andConfiguration:self.configuration]; value = [parser parseDictionary:(NSDictionary *) value forParentObject:parentObject]; diff --git a/KeyValueObjectMappingTests/DCArrayOfTweetsOnUserTests.m b/KeyValueObjectMappingTests/DCArrayOfTweetsOnUserTests.m index 11a2afa..4015c7b 100644 --- a/KeyValueObjectMappingTests/DCArrayOfTweetsOnUserTests.m +++ b/KeyValueObjectMappingTests/DCArrayOfTweetsOnUserTests.m @@ -53,6 +53,26 @@ - (void) testShouldCreateAnUserWithTweets { STAssertEquals((int)user.tweets.count, 2, @"Should have 2 tweets on array of tweets"); } +- (void) testShouldCreateAnUserWithOneImplicitTweet { + + DCArrayMapping *mapper = [DCArrayMapping mapperForClassElements:[Tweet class] + forAttribute:@"tweets" + onClass:[User class]]; + + DCParserConfiguration *configuration = [DCParserConfiguration configuration]; + [configuration addArrayMapper:mapper]; + + DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass:[User class] + andConfiguration:configuration]; + + NSArray *tweetArray = [jsonParsed valueForKey:@"tweets"]; + Tweet *tweet = [tweetArray objectAtIndex:0]; + [jsonParsed setValue:tweet forKey:@"tweets"]; + User *user = [parser parseDictionary:jsonParsed]; + STAssertNotNil(user.tweets, @"Tweets should not be nil"); + STAssertEquals((int)user.tweets.count, 1, @"Should have 1 tweet on array of tweets"); +} + - (void) testShouldCreateUserWithNilOnTweetsArray{ DCObjectMapping *objectMapping = [DCObjectMapping mapKeyPath:@"tweets_nullable" toAttribute:@"tweets"