Skip to content

Changed the CSMappingArraySubTypeKey to CSMappingCollectionSubTypeKey, t... #4

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@
<string>CSMapperTestApp</string>
<key>IDESourceControlProjectOriginsDictionary</key>
<dict>
<key>58B62EAD-450F-433B-9448-4E0A05497FBF</key>
<string>ssh://github.com/marcammann/CSMapper.git</string>
<key>7B4F58B70F1D66ED172BF588CBF60BF38A1FA730</key>
<string>github.com:AntonTheDev/CSMapper.git</string>
</dict>
<key>IDESourceControlProjectPath</key>
<string>CSMapperTestApp/CSMapperTestApp.xcodeproj/project.xcworkspace</string>
<string>CSMapperTestApp/CSMapperTestApp.xcodeproj</string>
<key>IDESourceControlProjectRelativeInstallPathDictionary</key>
<dict>
<key>58B62EAD-450F-433B-9448-4E0A05497FBF</key>
<key>7B4F58B70F1D66ED172BF588CBF60BF38A1FA730</key>
<string>../../..</string>
</dict>
<key>IDESourceControlProjectURL</key>
<string>ssh://github.com/marcammann/CSMapper.git</string>
<string>github.com:AntonTheDev/CSMapper.git</string>
<key>IDESourceControlProjectVersion</key>
<integer>110</integer>
<integer>111</integer>
<key>IDESourceControlProjectWCCIdentifier</key>
<string>58B62EAD-450F-433B-9448-4E0A05497FBF</string>
<string>7B4F58B70F1D66ED172BF588CBF60BF38A1FA730</string>
<key>IDESourceControlProjectWCConfigurations</key>
<array>
<dict>
<key>IDESourceControlRepositoryExtensionIdentifierKey</key>
<string>public.vcs.git</string>
<key>IDESourceControlWCCIdentifierKey</key>
<string>58B62EAD-450F-433B-9448-4E0A05497FBF</string>
<string>7B4F58B70F1D66ED172BF588CBF60BF38A1FA730</string>
<key>IDESourceControlWCCName</key>
<string>CSMapper</string>
</dict>
Expand Down
43 changes: 31 additions & 12 deletions Classes/NSObject+CSAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
static NSString * const CSMappingClassKey = @"type";
static NSString * const CSMappingGroupsKey = @"groups";
static NSString * const CSMappingArraySubTypeKey = @"array_subtype";
static NSString * const CSMappingCollectionSubTypeKey = @"collection_subtype";
static NSString * const CSMappingMapperKey = @"mapper";
static NSString * const CSMappingDefaultKey = @"default";

Expand Down Expand Up @@ -155,7 +156,7 @@ - (void)mapAttributesWithDictionary:(NSDictionary *)aDictionary groups:(NSArray
selector = NSSelectorFromString([NSString stringWithFormat:@"%@Value", forcedClass]);
if ([inputValue respondsToSelector:selector]) {
// Try to use the built in conversion features for known types
outputValue = objc_msgSend(inputValue, selector);
outputValue = ((id (*)(id, SEL))objc_msgSend)(inputValue, selector);
} else {
// Try to map unknown type with same technique.
id newValue = [[forcedClass alloc] init];
Expand All @@ -165,26 +166,46 @@ - (void)mapAttributesWithDictionary:(NSDictionary *)aDictionary groups:(NSArray
}

//check to see if there is a type for the objects in an array
arraySubTypeValue = [propertyMapping objectForKey:CSMappingArraySubTypeKey];

if ([inputValue isKindOfClass:[NSArray class]] && arraySubTypeValue) {
arraySubTypeValue = [propertyMapping objectForKey:CSMappingCollectionSubTypeKey];

if(!arraySubTypeValue)
{
// Backwards compatibility prior to the addition of dictionary type mapping
arraySubTypeValue = [propertyMapping objectForKey:CSMappingArraySubTypeKey];
}

if (arraySubTypeValue) {
forcedClassString = arraySubTypeValue;
forcedClass = NSClassFromString(arraySubTypeValue);

NSMutableArray *newSubObjectArray = [NSMutableArray new];

for (id subobjectDict in inputValue) {
id newValue = [[forcedClass alloc] init];
[newValue mapAttributesWithDictionary:subobjectDict];
[newSubObjectArray addObject:newValue];

if ([inputValue isKindOfClass:[NSArray class]] ){
for (id subobjectDict in inputValue) {
id newValue = [[forcedClass alloc] init];
[newValue mapAttributesWithDictionary:subobjectDict];
[newSubObjectArray addObject:newValue];
}
} else if ([inputValue isKindOfClass:[NSDictionary class]] ){
for (id subobjectDict in [inputValue allValues]) {
id newValue = [[forcedClass alloc] init];
[newValue mapAttributesWithDictionary:subobjectDict];
[newSubObjectArray addObject:newValue];
}
}

outputValue = newSubObjectArray;
}

if (mapperClass && mapperClass) {
outputValue = [(id<CSMapper>)mapperClass transformValue:inputValue];
}

if([propertyName isEqualToString:@"subDictionaryValue"])
{
NSLog(@"STOP");
}

[self setValue:outputValue forKey:propertyName];
}
}
Expand Down Expand Up @@ -251,12 +272,10 @@ - (NSNumber *)NSNumberValue {

return (NSNumber *)self;
} else {

return nil;
}
}


/**
Converts an object into an NSString
*/
Expand Down
26 changes: 24 additions & 2 deletions Tests/CSAPIMapperTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ @interface TestTestObject : NSObject
@property (nonatomic, readwrite) BOOL falseBool;
@property (nonatomic, strong) NSString *subSubValue;
@property (nonatomic, strong) NSArray *subArrayValue;
@property (nonatomic, strong) NSArray *subDictionaryValue;
@property (nonatomic, strong) NSArray *subArrayLegacyValue;
@end


Expand Down Expand Up @@ -211,16 +213,36 @@ - (void)testCompoundSubtype
}


- (void)testArraySubtype
- (void)testCollectionAsDictionarySubtype
{
TestTestObject *o = [[TestTestObject alloc] init];

[o mapAttributesWithDictionary:@{ @"test_subdictionary" : @{@"Object1" : @{@"test_trivial" : @"Test1"}, @"Object2" : @{@"test_trivial" : @"Test2"}, @"Object3" : @{@"test_trivial" : @"Test3"}}}];

STAssertEqualObjects([[o.subDictionaryValue objectAtIndex:0] testTrivial], @"Test1", @"1");
STAssertEqualObjects([[o.subDictionaryValue objectAtIndex:1] testTrivial], @"Test2", @"2");
STAssertEqualObjects([[o.subDictionaryValue objectAtIndex:2] testTrivial], @"Test3", @"3");
}


- (void)testCollectionAsArraySubtype
{
TestTestObject *o = [[TestTestObject alloc] init];
[o mapAttributesWithDictionary:@{ @"test_subarray" : @[ @{@"test_trivial" : @"Test1"}, @{@"test_trivial" : @"Test2"}, @{@"test_trivial" : @"Test3"}] }];
STAssertEqualObjects([[o.subArrayValue objectAtIndex:0] testTrivial], @"Test1", @"1");
STAssertEqualObjects([[o.subArrayValue objectAtIndex:1] testTrivial], @"Test2", @"2");
STAssertEqualObjects([[o.subArrayValue objectAtIndex:2] testTrivial], @"Test3", @"3");
}

- (void)testCollectionAsLegacyArraySubtype
{
TestTestObject *o = [[TestTestObject alloc] init];
[o mapAttributesWithDictionary:@{ @"test_subarray" : @[ @{@"test_trivial" : @"Test1"}, @{@"test_trivial" : @"Test2"}, @{@"test_trivial" : @"Test3"}] }];
STAssertEqualObjects([[o.subArrayValue objectAtIndex:0] testTrivial], @"Test1", nil);
STAssertEqualObjects([[o.subArrayValue objectAtIndex:1] testTrivial], @"Test2", nil);
STAssertEqualObjects([[o.subArrayValue objectAtIndex:2] testTrivial], @"Test3", nil);
}


- (void)testJsonMapping
{
TestJsonMappedObject *o = [TestJsonMappedObject new];
Expand Down
18 changes: 18 additions & 0 deletions Tests/TestTestObject.plist
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@
</array>
</dict>
<key>subArrayValue</key>
<dict>
<key>type</key>
<string>NSArray</string>
<key>key</key>
<string>test_subarray</string>
<key>collection_subtype</key>
<string>TestTestSubtype</string>
</dict>
<key>subDictionaryValue</key>
<dict>
<key>type</key>
<string>NSDictionary</string>
<key>key</key>
<string>test_subdictionary</string>
<key>collection_subtype</key>
<string>TestTestSubtype</string>
</dict>
<key>subArrayLegacyValue</key>
<dict>
<key>type</key>
<string>NSArray</string>
Expand Down