Skip to content

Commit

Permalink
Fix #167 by recognising struct properties as native properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianludwig authored and DenTelezhkin committed Jul 18, 2018
1 parent 15a39f3 commit 164169b
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Sources/EasyMapping/EKPropertyHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
10 changes: 6 additions & 4 deletions Sources/EasyMapping/EKPropertyHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}
}
Expand All @@ -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];
}
}
Expand Down
2 changes: 2 additions & 0 deletions TestFixtures/TestModels/Models/NSObject/Native.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <CoreLocation/CoreLocation.h>
#import "BaseTestModel.h"

@interface Native : BaseTestModel
Expand All @@ -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

Expand Down
3 changes: 2 additions & 1 deletion TestFixtures/TestModels/Models/NSObject/Native.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]];
}];
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/EasyMappingTests/EKPropertyHelperTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 164169b

Please sign in to comment.