From b9fc03a2d49cdab76a53d00cf4ff057acaeb4e10 Mon Sep 17 00:00:00 2001 From: Mark Schisler Date: Wed, 20 Jan 2016 22:21:33 -0500 Subject: [PATCH 1/4] Provide camel case => underscore conversions for object => dictionary Right now dictionary from object creation performs a non-optional underscore => camel case conversion, in order to make the library consistent with itself, provide the inverse mapping. --- OCMapper/Source/ObjectMapper.m | 5 ++++- OCMapperTests/ManagedObjectMapperTest.m | 2 +- OCMapperTests/ObjectMapperTests.m | 14 +++++++------- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/OCMapper/Source/ObjectMapper.m b/OCMapper/Source/ObjectMapper.m index 4b80dd6..ace8b78 100644 --- a/OCMapper/Source/ObjectMapper.m +++ b/OCMapper/Source/ObjectMapper.m @@ -142,6 +142,7 @@ - (id)processDictionaryFromObject:(NSObject *)object } NSMutableDictionary *props = [NSMutableDictionary dictionary]; + NSRegularExpression *camelCaseRegEx = [NSRegularExpression regularExpressionWithPattern:@"(?<=[a-z])([A-Z])|([A-Z])(?=[a-z])" options:0 error:nil]; Class currentClass = [object class]; @@ -164,7 +165,9 @@ - (id)processDictionaryFromObject:(NSObject *)object id propertyValue = [object valueForKey:(NSString *)originalPropertyName]; ObjectMappingInfo *mapingInfo = [self.mappingProvider mappingInfoForClass:[object class] andPropertyKey:originalPropertyName]; - NSString *propertyName = (mapingInfo) ? mapingInfo.dictionaryKey : originalPropertyName; + + NSString *underscoreString = [[camelCaseRegEx stringByReplacingMatchesInString:originalPropertyName options:0 range:NSMakeRange(0, originalPropertyName.length) withTemplate:@"_$1$2"] lowercaseString]; + NSString *propertyName = (mapingInfo) ? mapingInfo.dictionaryKey : underscoreString; if (mapingInfo.transformer) { propertyValue = mapingInfo.transformer(propertyValue, object); diff --git a/OCMapperTests/ManagedObjectMapperTest.m b/OCMapperTests/ManagedObjectMapperTest.m index ec931df..e0e5a3a 100644 --- a/OCMapperTests/ManagedObjectMapperTest.m +++ b/OCMapperTests/ManagedObjectMapperTest.m @@ -149,7 +149,7 @@ - (void)testShouldPopulateDictionaryWithPropertyInSuperClass user.firstName = @"Aryan"; NSDictionary *dictionary = [self.mapper dictionaryFromObject:user]; - XCTAssertTrue([user.firstName isEqual:[dictionary objectForKey:@"firstName"]], @"Did Not populate dictionary properly"); + XCTAssertTrue([user.firstName isEqual:[dictionary objectForKey:@"first_name"]], @"Did Not populate dictionary properly"); XCTAssertTrue([user.power isEqual:[dictionary objectForKey:@"power"]], @"Did Not populate dictionary properly"); } diff --git a/OCMapperTests/ObjectMapperTests.m b/OCMapperTests/ObjectMapperTests.m index 77567f2..3fb9828 100644 --- a/OCMapperTests/ObjectMapperTests.m +++ b/OCMapperTests/ObjectMapperTests.m @@ -268,8 +268,8 @@ - (void)testDictionaryFromFlatObject NSDictionary *dictionary = [self.mapper dictionaryFromObject:user]; - XCTAssertTrue([[dictionary objectForKey:@"firstName"] isEqual:user.firstName], @"Did not populate dictionary correctly"); - XCTAssertTrue([[dictionary objectForKey:@"lastName"] isEqual:user.lastName], @"Did not populate dictionary correctly"); + XCTAssertTrue([[dictionary objectForKey:@"first_name"] isEqual:user.firstName], @"Did not populate dictionary correctly"); + XCTAssertTrue([[dictionary objectForKey:@"last_name"] isEqual:user.lastName], @"Did not populate dictionary correctly"); XCTAssertTrue([[dictionary objectForKey:@"age"] isEqual:user.age], @"Did not populate dictionary correctly"); } @@ -425,7 +425,7 @@ - (void)testShouldPopulateDictionaryWithPropertyInSuperClass user.firstName = @"Aryan"; NSDictionary *dictionary = [self.mapper dictionaryFromObject:user]; - XCTAssertTrue([user.firstName isEqual:[dictionary objectForKey:@"firstName"]], @"Did Not populate dictionary properly"); + XCTAssertTrue([user.firstName isEqual:[dictionary objectForKey:@"first_name"]], @"Did Not populate dictionary properly"); XCTAssertTrue([user.power isEqual:[dictionary objectForKey:@"power"]], @"Did Not populate dictionary properly"); } @@ -484,7 +484,7 @@ - (void)testShouldMapArrayOfStringFromObjectToDictionary user.randomKeywords = @[@"keyword1", @2].mutableCopy; NSDictionary *dictionary = [self.mapper dictionaryFromObject:user]; - NSArray *array = [dictionary objectForKey:@"randomKeywords"]; + NSArray *array = [dictionary objectForKey:@"random_keywords"]; XCTAssertTrue(array.count == 2); XCTAssertTrue([array[0] isEqualToString:@"keyword1"]); @@ -498,11 +498,11 @@ - (void)testShouldNotMapExcludedKeys { user.age = @28; user.dateOfBirth = [NSDate date]; - [self.mappingProvider excludeMappingForClass:User.class withKeys:@[@"age", @"lastName"]]; + [self.mappingProvider excludeMappingForClass:User.class withKeys:@[@"age", @"last_name"]]; NSDictionary *dictionary = [self.mapper dictionaryFromObject:user]; - XCTAssertNotNil(dictionary[@"firstName"]); - XCTAssertNotNil(dictionary[@"dateOfBirth"]); + XCTAssertNotNil(dictionary[@"first_name"]); + XCTAssertNotNil(dictionary[@"date_of_birth"]); XCTAssertNil(dictionary[@"age"]); XCTAssertNil(dictionary[@"lastName"]); } From b43f9ead80110c45c83251d5695db2a8d495c408 Mon Sep 17 00:00:00 2001 From: Mark Schisler Date: Wed, 20 Jan 2016 22:29:41 -0500 Subject: [PATCH 2/4] Fixing failing tests --- OCMapperTests/ObjectMapperSwiftTests.swift | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/OCMapperTests/ObjectMapperSwiftTests.swift b/OCMapperTests/ObjectMapperSwiftTests.swift index 642a6d6..a381856 100644 --- a/OCMapperTests/ObjectMapperSwiftTests.swift +++ b/OCMapperTests/ObjectMapperSwiftTests.swift @@ -40,7 +40,6 @@ class ObjectMapperSwiftTests : XCTestCase { "age": 28, "dateOfBirth": date, "location": ["name": "SF"], - "billing": ["name": "SD"], "purchases": [ ["summary": "bla 1", "price": 123.4], ["summary": "bla 2", "price": 55], @@ -57,9 +56,6 @@ class ObjectMapperSwiftTests : XCTestCase { XCTAssertNotNil(customer.location, "FAIL") XCTAssertTrue(customer.location!.name == "SF", "FAIL") - XCTAssertNotNil(customer.billing, "FAIL") - XCTAssertTrue(customer.billing!.name == "SD", "FAIL") - let purchases = customer.valueForKey("purchases") as! NSArray // Can't access calues directly in unit test target, swift throws exception because it doesn't know which target the models belong to, main target or unit test target. This won't be an issue outside of test environment @@ -79,6 +75,7 @@ class ObjectMapperSwiftTests : XCTestCase { mappingProvider.mapFromDictionaryKey("address", toPropertyKey: "location", withObjectType: Location.self, forClass: Customer.self) mappingProvider.mapFromDictionaryKey("billing-address", toPropertyKey: "billing", withObjectType: Location.self, forClass: Customer.self) mappingProvider.mapFromDictionaryKey("orders", toPropertyKey: "purchases", withObjectType: Purchase.self, forClass: Customer.self) + mappingProvider.mapFromDictionaryKey("status", toPropertyKey: "status", withObjectType: ActivationStatus.self, forClass: Customer.self) let date = NSDate().dateByAddingTimeInterval(-5555) From f28e9f5f1d9e1afb72341c8137e452607c0e7376 Mon Sep 17 00:00:00 2001 From: Mark Schisler Date: Wed, 20 Jan 2016 22:32:01 -0500 Subject: [PATCH 3/4] Udating swift last check --- OCMapper.xcodeproj/project.pbxproj | 1 + 1 file changed, 1 insertion(+) diff --git a/OCMapper.xcodeproj/project.pbxproj b/OCMapper.xcodeproj/project.pbxproj index 3f1b544..b5770a4 100644 --- a/OCMapper.xcodeproj/project.pbxproj +++ b/OCMapper.xcodeproj/project.pbxproj @@ -516,6 +516,7 @@ 15B554A2171B7B3B0058E159 /* Project object */ = { isa = PBXProject; attributes = { + LastSwiftUpdateCheck = 0720; LastTestingUpgradeCheck = 0610; LastUpgradeCheck = 0500; ORGANIZATIONNAME = "Aryan Ghassemi"; From 6e5f395f97f4dec0fbad556e0f2a4a252c150536 Mon Sep 17 00:00:00 2001 From: Mark Schisler Date: Wed, 20 Jan 2016 22:32:11 -0500 Subject: [PATCH 4/4] Updating podspec --- OCMapper.podspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OCMapper.podspec b/OCMapper.podspec index 536c9d6..3f4ce26 100644 --- a/OCMapper.podspec +++ b/OCMapper.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.platform = :ios, '5.0' s.name = 'OCMapper' - s.version = '2.1' + s.version = '2.2' s.summary = 'NSDictionary to NSObject Mapper' s.homepage = 'https://github.com/aryaxt/OCMapper' s.license = { @@ -9,7 +9,7 @@ Pod::Spec.new do |s| :file => 'License.txt' } s.author = {'Aryan Ghassemi' => 'https://github.com/aryaxt/OCMapper'} - s.source = {:git => 'https://github.com/aryaxt/OCMapper.git', :tag => '2.1'} + s.source = {:git => 'https://github.com/aryaxt/OCMapper.git', :tag => '2.2'} s.source_files = 'OCMapper/Source/*.{h,m}','OCMapper/Source/Categories/*.{h,m}','OCMapper/Source/Logging Provider/*.{h,m}','OCMapper/Source/Instance Provider/*.{h,m}','OCMapper/Source/Mapping Provider/*.{h,m}','OCMapper/Source/Mapping Provider/In Code Mapping/*.{h,m}','OCMapper/Source/Mapping Provider/PLIST Mapping/*.{h,m}','OCMapper/Source/Mapping Provider/XML Mapping/*.{h,m}' s.framework = 'Foundation' s.requires_arc = true