From 164169ba7466ac5770fff61da5c68d70ae998b37 Mon Sep 17 00:00:00 2001 From: Sebastian Ludwig Date: Wed, 18 Jul 2018 12:32:54 +0200 Subject: [PATCH] Fix #167 by recognising struct properties as native properties. --- Sources/EasyMapping/EKPropertyHelper.h | 2 +- Sources/EasyMapping/EKPropertyHelper.m | 10 ++++++---- TestFixtures/TestModels/Models/NSObject/Native.h | 2 ++ TestFixtures/TestModels/Models/NSObject/Native.m | 3 ++- Tests/EasyMappingTests/EKPropertyHelperTestCase.swift | 6 +++--- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Sources/EasyMapping/EKPropertyHelper.h b/Sources/EasyMapping/EKPropertyHelper.h index 0e490ba..a8e4e40 100644 --- a/Sources/EasyMapping/EKPropertyHelper.h +++ b/Sources/EasyMapping/EKPropertyHelper.h @@ -34,7 +34,7 @@ NS_ASSUME_NONNULL_BEGIN */ @interface EKPropertyHelper : NSObject -+ (BOOL)propertyNameIsScalar:(NSString *)propertyName fromObject:(id)object; ++ (BOOL)propertyNameIsNativeProperty:(NSString *)propertyName fromObject:(id)object; + (nullable id)propertyRepresentation:(NSArray *)array forObject:(id)object withPropertyName:(NSString *)propertyName; diff --git a/Sources/EasyMapping/EKPropertyHelper.m b/Sources/EasyMapping/EKPropertyHelper.m index 920249d..6e461bb 100644 --- a/Sources/EasyMapping/EKPropertyHelper.m +++ b/Sources/EasyMapping/EKPropertyHelper.m @@ -39,12 +39,14 @@ @implementation EKPropertyHelper #pragma mark - Property introspection -+ (BOOL)propertyNameIsScalar:(NSString *)propertyName fromObject:(id)object ++ (BOOL)propertyNameIsNativeProperty:(NSString *)propertyName fromObject:(id)object { objc_property_t property = class_getProperty(object_getClass(object), [propertyName UTF8String]); NSString *type = property ? [self propertyTypeStringRepresentationFromProperty:property] : nil; - return (type.length == 1) && memchr(scalarTypes, type.UTF8String[0], sizeof(scalarTypes)); + BOOL isScalar = (type.length == 1) && memchr(scalarTypes, type.UTF8String[0], sizeof(scalarTypes)); + BOOL isStruct = (type.length > 2) && [type characterAtIndex:0] == '{' && [type characterAtIndex:type.length - 1] == '}'; + return isScalar || isStruct; } + (NSString *) propertyTypeStringRepresentationFromProperty:(objc_property_t)property @@ -110,7 +112,7 @@ + (void)setProperty:(EKPropertyMapping *)propertyMapping onObject:(id)object } else { if (!value && ignoreMissingFields) return; - if (![self propertyNameIsScalar:propertyMapping.property fromObject:object]) { + if (![self propertyNameIsNativeProperty:propertyMapping.property fromObject:object]) { [self setValue:nil onObject:object forKeyPath:propertyMapping.property]; } } @@ -136,7 +138,7 @@ + (void) setProperty:(EKPropertyMapping *)propertyMapping } else { if (!value && ignoreMissingFields) return; - if (![self propertyNameIsScalar:propertyMapping.property fromObject:object]) { + if (![self propertyNameIsNativeProperty:propertyMapping.property fromObject:object]) { [self setValue:nil onObject:object forKeyPath:propertyMapping.property]; } } diff --git a/TestFixtures/TestModels/Models/NSObject/Native.h b/TestFixtures/TestModels/Models/NSObject/Native.h index ef0d707..225bcff 100644 --- a/TestFixtures/TestModels/Models/NSObject/Native.h +++ b/TestFixtures/TestModels/Models/NSObject/Native.h @@ -8,6 +8,7 @@ #import #import +#import #import "BaseTestModel.h" @interface Native : BaseTestModel @@ -29,6 +30,7 @@ @property (nonatomic, readwrite) double doubleProperty; @property (nonatomic, readwrite) BOOL boolProperty; @property (nonatomic, readwrite) bool smallBoolProperty; +@property (nonatomic, readwrite) CLLocationCoordinate2D locationCoordinateProperty; @end diff --git a/TestFixtures/TestModels/Models/NSObject/Native.m b/TestFixtures/TestModels/Models/NSObject/Native.m index f39a235..156697e 100644 --- a/TestFixtures/TestModels/Models/NSObject/Native.m +++ b/TestFixtures/TestModels/Models/NSObject/Native.m @@ -16,7 +16,8 @@ +(EKObjectMapping *)objectMapping [mapping mapPropertiesFromArray:@[ @"charProperty", @"unsignedCharProperty", @"shortProperty", @"unsignedShortProperty", @"intProperty", @"unsignedIntProperty", @"integerProperty", @"unsignedIntegerProperty", @"longProperty", @"unsignedLongProperty", @"longLongProperty", - @"unsignedLongLongProperty", @"floatProperty", @"cgFloatProperty", @"doubleProperty", @"boolProperty", @"smallBoolProperty" + @"unsignedLongLongProperty", @"floatProperty", @"cgFloatProperty", @"doubleProperty", @"boolProperty", @"smallBoolProperty", + @"locationCoordinateProperty" ]]; }]; } diff --git a/Tests/EasyMappingTests/EKPropertyHelperTestCase.swift b/Tests/EasyMappingTests/EKPropertyHelperTestCase.swift index c326fe8..920fc0e 100644 --- a/Tests/EasyMappingTests/EKPropertyHelperTestCase.swift +++ b/Tests/EasyMappingTests/EKPropertyHelperTestCase.swift @@ -38,15 +38,15 @@ class EKPropertyHelperTestCase: XCTestCase { let mapping = Native.objectMapping() let properties = mapping.propertyMappings as? [String:EKPropertyMapping] ?? [:] for property in properties.values { - XCTAssert(EKPropertyHelper.propertyNameIsScalar(property.property, from: sut)) + XCTAssert(EKPropertyHelper.propertyNameIsNativeProperty(property.property, from: sut)) } - XCTAssert(EKPropertyHelper.propertyNameIsScalar("boolProperty", from: sut)) + XCTAssert(EKPropertyHelper.propertyNameIsNativeProperty("boolProperty", from: sut)) } func testIdIdentification() { let object = MutableFoundationClass() - XCTAssertFalse(EKPropertyHelper.propertyNameIsScalar("idObject", from: object)) + XCTAssertFalse(EKPropertyHelper.propertyNameIsNativeProperty("idObject", from: object)) let id = EKPropertyHelper.propertyRepresentation([1,2,3], for: object, withPropertyName: "idObject")