From 792bd4d68cae8de14b891486571f35b00344dfaf Mon Sep 17 00:00:00 2001 From: AntonTheDev Date: Mon, 31 Mar 2014 13:24:11 -0400 Subject: [PATCH 1/5] Changed the CSMappingArraySubTypeKey to CSMappingCollectionSubTypeKey, the mapping value has been changed to collection_subtype for the plist / json mapping, and can now handle NSDictionaries --- Classes/NSObject+CSAPI.m | 30 +++++++++++++++++++----------- Tests/CSAPIMapperTests.m | 15 ++++++++++++++- Tests/TestTestObject.plist | 11 ++++++++++- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/Classes/NSObject+CSAPI.m b/Classes/NSObject+CSAPI.m index ad67e19..96fd236 100644 --- a/Classes/NSObject+CSAPI.m +++ b/Classes/NSObject+CSAPI.m @@ -19,7 +19,7 @@ static NSString * const CSMappingKeyKey = @"key"; 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"; @@ -165,22 +165,31 @@ - (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]; + NSLog(@"SUBType %@", arraySubTypeValue); + 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)mapperClass transformValue:inputValue]; } @@ -256,7 +265,6 @@ - (NSNumber *)NSNumberValue { } } - /** Converts an object into an NSString */ diff --git a/Tests/CSAPIMapperTests.m b/Tests/CSAPIMapperTests.m index 3b7c948..41d8af9 100644 --- a/Tests/CSAPIMapperTests.m +++ b/Tests/CSAPIMapperTests.m @@ -52,6 +52,7 @@ @interface TestTestObject : NSObject @property (nonatomic, readwrite) BOOL falseBool; @property (nonatomic, strong) NSString *subSubValue; @property (nonatomic, strong) NSArray *subArrayValue; +@property (nonatomic, strong) NSArray *subDictionaryValue; @end @@ -211,7 +212,19 @@ - (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", nil); + STAssertEqualObjects([[o.subDictionaryValue objectAtIndex:1] testTrivial], @"Test2", nil); + STAssertEqualObjects([[o.subDictionaryValue objectAtIndex:2] testTrivial], @"Test3", nil); +} + + +- (void)testCollectionAsArraySubtype { TestTestObject *o = [[TestTestObject alloc] init]; [o mapAttributesWithDictionary:@{ @"test_subarray" : @[ @{@"test_trivial" : @"Test1"}, @{@"test_trivial" : @"Test2"}, @{@"test_trivial" : @"Test3"}] }]; diff --git a/Tests/TestTestObject.plist b/Tests/TestTestObject.plist index d19de79..ec0606d 100644 --- a/Tests/TestTestObject.plist +++ b/Tests/TestTestObject.plist @@ -97,7 +97,16 @@ NSArray key test_subarray - array_subtype + collection_subtype + TestTestSubtype + + subDictionaryValue + + type + NSDictionary + key + test_subdictionary + collection_subtype TestTestSubtype From f6b9bd7800396ea930b1eee540dff6f2adabc69b Mon Sep 17 00:00:00 2001 From: Anton Date: Thu, 15 Jan 2015 17:33:10 -0500 Subject: [PATCH 2/5] Updated the built-in conversion --- .../xcshareddata/CSMapperTestApp.xccheckout | 16 ++++++++-------- Classes/NSObject+CSAPI.m | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/CSMapperTestApp/CSMapperTestApp.xcodeproj/project.xcworkspace/xcshareddata/CSMapperTestApp.xccheckout b/CSMapperTestApp/CSMapperTestApp.xcodeproj/project.xcworkspace/xcshareddata/CSMapperTestApp.xccheckout index a09fd2a..f601a31 100644 --- a/CSMapperTestApp/CSMapperTestApp.xcodeproj/project.xcworkspace/xcshareddata/CSMapperTestApp.xccheckout +++ b/CSMapperTestApp/CSMapperTestApp.xcodeproj/project.xcworkspace/xcshareddata/CSMapperTestApp.xccheckout @@ -10,29 +10,29 @@ CSMapperTestApp IDESourceControlProjectOriginsDictionary - 58B62EAD-450F-433B-9448-4E0A05497FBF - ssh://github.com/marcammann/CSMapper.git + 7B4F58B70F1D66ED172BF588CBF60BF38A1FA730 + github.com:AntonTheDev/CSMapper.git IDESourceControlProjectPath - CSMapperTestApp/CSMapperTestApp.xcodeproj/project.xcworkspace + CSMapperTestApp/CSMapperTestApp.xcodeproj IDESourceControlProjectRelativeInstallPathDictionary - 58B62EAD-450F-433B-9448-4E0A05497FBF + 7B4F58B70F1D66ED172BF588CBF60BF38A1FA730 ../../.. IDESourceControlProjectURL - ssh://github.com/marcammann/CSMapper.git + github.com:AntonTheDev/CSMapper.git IDESourceControlProjectVersion - 110 + 111 IDESourceControlProjectWCCIdentifier - 58B62EAD-450F-433B-9448-4E0A05497FBF + 7B4F58B70F1D66ED172BF588CBF60BF38A1FA730 IDESourceControlProjectWCConfigurations IDESourceControlRepositoryExtensionIdentifierKey public.vcs.git IDESourceControlWCCIdentifierKey - 58B62EAD-450F-433B-9448-4E0A05497FBF + 7B4F58B70F1D66ED172BF588CBF60BF38A1FA730 IDESourceControlWCCName CSMapper diff --git a/Classes/NSObject+CSAPI.m b/Classes/NSObject+CSAPI.m index 96fd236..bc5d8d5 100644 --- a/Classes/NSObject+CSAPI.m +++ b/Classes/NSObject+CSAPI.m @@ -155,7 +155,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]; From 31fb6a31ae9d35d1994a7390f3ba36d480ab4eb5 Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 23 Feb 2015 18:51:10 -0500 Subject: [PATCH 3/5] Removed NSLog Statement --- Classes/NSObject+CSAPI.m | 1 - 1 file changed, 1 deletion(-) diff --git a/Classes/NSObject+CSAPI.m b/Classes/NSObject+CSAPI.m index bc5d8d5..1a57f0b 100644 --- a/Classes/NSObject+CSAPI.m +++ b/Classes/NSObject+CSAPI.m @@ -166,7 +166,6 @@ - (void)mapAttributesWithDictionary:(NSDictionary *)aDictionary groups:(NSArray //check to see if there is a type for the objects in an array arraySubTypeValue = [propertyMapping objectForKey:CSMappingCollectionSubTypeKey]; - NSLog(@"SUBType %@", arraySubTypeValue); if (arraySubTypeValue) { forcedClassString = arraySubTypeValue; forcedClass = NSClassFromString(arraySubTypeValue); From 47e304138b6b141100b2a7cd9d65b91a8a41a46b Mon Sep 17 00:00:00 2001 From: Anton Date: Mon, 23 Feb 2015 18:57:18 -0500 Subject: [PATCH 4/5] Added backwards compatibility for array subtype key --- Classes/NSObject+CSAPI.m | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Classes/NSObject+CSAPI.m b/Classes/NSObject+CSAPI.m index 1a57f0b..4d15198 100644 --- a/Classes/NSObject+CSAPI.m +++ b/Classes/NSObject+CSAPI.m @@ -19,6 +19,7 @@ static NSString * const CSMappingKeyKey = @"key"; 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"; @@ -166,6 +167,13 @@ - (void)mapAttributesWithDictionary:(NSDictionary *)aDictionary groups:(NSArray //check to see if there is a type for the objects in an array 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); From c2350c1eb94de404d99b03a58c63ea33469ead85 Mon Sep 17 00:00:00 2001 From: Anton Date: Wed, 25 Feb 2015 19:59:05 -0500 Subject: [PATCH 5/5] Updated Test Cases for Legacy Key --- Classes/NSObject+CSAPI.m | 6 +++++- Tests/CSAPIMapperTests.m | 17 +++++++++++++---- Tests/TestTestObject.plist | 9 +++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/Classes/NSObject+CSAPI.m b/Classes/NSObject+CSAPI.m index 4d15198..42a3d75 100644 --- a/Classes/NSObject+CSAPI.m +++ b/Classes/NSObject+CSAPI.m @@ -201,6 +201,11 @@ - (void)mapAttributesWithDictionary:(NSDictionary *)aDictionary groups:(NSArray outputValue = [(id)mapperClass transformValue:inputValue]; } + if([propertyName isEqualToString:@"subDictionaryValue"]) + { + NSLog(@"STOP"); + } + [self setValue:outputValue forKey:propertyName]; } } @@ -267,7 +272,6 @@ - (NSNumber *)NSNumberValue { return (NSNumber *)self; } else { - return nil; } } diff --git a/Tests/CSAPIMapperTests.m b/Tests/CSAPIMapperTests.m index 41d8af9..b9be05a 100644 --- a/Tests/CSAPIMapperTests.m +++ b/Tests/CSAPIMapperTests.m @@ -53,6 +53,7 @@ @interface TestTestObject : NSObject @property (nonatomic, strong) NSString *subSubValue; @property (nonatomic, strong) NSArray *subArrayValue; @property (nonatomic, strong) NSArray *subDictionaryValue; +@property (nonatomic, strong) NSArray *subArrayLegacyValue; @end @@ -218,9 +219,9 @@ - (void)testCollectionAsDictionarySubtype [o mapAttributesWithDictionary:@{ @"test_subdictionary" : @{@"Object1" : @{@"test_trivial" : @"Test1"}, @"Object2" : @{@"test_trivial" : @"Test2"}, @"Object3" : @{@"test_trivial" : @"Test3"}}}]; - STAssertEqualObjects([[o.subDictionaryValue objectAtIndex:0] testTrivial], @"Test1", nil); - STAssertEqualObjects([[o.subDictionaryValue objectAtIndex:1] testTrivial], @"Test2", nil); - STAssertEqualObjects([[o.subDictionaryValue objectAtIndex:2] testTrivial], @"Test3", nil); + STAssertEqualObjects([[o.subDictionaryValue objectAtIndex:0] testTrivial], @"Test1", @"1"); + STAssertEqualObjects([[o.subDictionaryValue objectAtIndex:1] testTrivial], @"Test2", @"2"); + STAssertEqualObjects([[o.subDictionaryValue objectAtIndex:2] testTrivial], @"Test3", @"3"); } @@ -228,12 +229,20 @@ - (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]; diff --git a/Tests/TestTestObject.plist b/Tests/TestTestObject.plist index ec0606d..9523b2e 100644 --- a/Tests/TestTestObject.plist +++ b/Tests/TestTestObject.plist @@ -109,5 +109,14 @@ collection_subtype TestTestSubtype + subArrayLegacyValue + + type + NSArray + key + test_subarray + array_subtype + TestTestSubtype +