Skip to content

Commit

Permalink
* auto-ignore block properties
Browse files Browse the repository at this point in the history
* auto-ignore readonly properties via property attributes (speed gain)
* unit tests for auto-ignored properties
  • Loading branch information
icanzilb committed Apr 18, 2014
1 parent 12a67c4 commit 2673637
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 20 deletions.
24 changes: 9 additions & 15 deletions JSONModel/JSONModel/JSONModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,14 @@ -(void)__inspectProperties
//get property attributes
const char *attrs = property_getAttributes(property);
NSString* propertyAttributes = @(attrs);
NSArray* attributeItems = [propertyAttributes componentsSeparatedByString:@","];

//ignore read-only properties
if ([attributeItems containsObject:@"R"]) {
continue; //to next property
}

//check for 64b BOOLs
if ([propertyAttributes hasPrefix:@"Tc,"]) {
//mask BOOLs as structs so they can have custom convertors
p.structName = @"BOOL";
Expand Down Expand Up @@ -597,8 +604,6 @@ -(void)__inspectProperties
p.convertsOnDemand = YES;
} else if([protocolName isEqualToString:@"Ignore"]) {
p = nil;
} else if ([self propertyIsReadOnly:p.name]) {
p = nil;
} else {
p.protocol = protocolName;
}
Expand Down Expand Up @@ -645,7 +650,8 @@ -(void)__inspectProperties
p = nil;
}

if([self propertyIsReadOnly:nsPropertyName]) {
//few cases where JSONModel will ignore properties automatically
if ([propertyType isEqualToString:@"Block"]) {
p = nil;
}

Expand Down Expand Up @@ -1224,18 +1230,6 @@ +(BOOL)propertyIsIgnored:(NSString *)propertyName
return NO;
}

-(BOOL)propertyIsReadOnly: (NSString*)key
{
NSString *setterString = [NSString stringWithFormat:@"set%@%@:",
[[key substringToIndex:1] capitalizedString],
[key substringFromIndex:1]];

if ([self respondsToSelector:NSSelectorFromString(setterString)]) {
return NO;
}
return YES;
}

#pragma mark - working with incomplete models
-(void)mergeFromDictionary:(NSDictionary*)dict useKeyMapping:(BOOL)useKeyMapping
{
Expand Down
68 changes: 68 additions & 0 deletions JSONModelDemoTests/UnitTests/SpecialPropertiesTests.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// SpecialPropertiesTests.m
// JSONModelDemo_iOS
//
// Created by Marin Todorov on 4/18/14.
// Copyright (c) 2014 Underplot ltd. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "JSONModel.h"

#pragma mark - model with block property
@interface BModel: JSONModel
@property (assign, nonatomic) int id;
@property (nonatomic, copy) void(^userLocationCompleted)();
@end

@implementation BModel
@end

#pragma mark - model with read-only properties
@interface RModel: JSONModel
@property (assign, nonatomic) int id;
@property (assign, nonatomic, readonly) int rId;
@property (strong, nonatomic, readonly) NSNumber* nId;
@end

@implementation RModel
@end

#pragma mark - test suite

@interface SpecialPropertiesTests : XCTestCase

@end

@implementation SpecialPropertiesTests

- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}

- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}

//test autoignoring block properties
- (void)testBlocks
{
NSString* json = @"{\"id\":1}";
BModel* bm = [[BModel alloc] initWithString:json error:nil];
XCTAssertNotNil(bm, @"model failed to crate");
}

//test autoignoring read-only properties
- (void)testReadOnly
{
NSString* json = @"{\"id\":1}";
RModel* rm = [[RModel alloc] initWithString:json error:nil];
XCTAssertNotNil(rm, @"model failed to crate");
}

@end

6 changes: 5 additions & 1 deletion JSONModelDemo_OSX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
9CC2FCB9168CE7340059FE67 /* KivaFeed.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CC2FCB4168CE7340059FE67 /* KivaFeed.m */; };
9CC2FCBA168CE7340059FE67 /* LoanModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CC2FCB6168CE7340059FE67 /* LoanModel.m */; };
9CC2FCBB168CE7340059FE67 /* LocationModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CC2FCB8168CE7340059FE67 /* LocationModel.m */; };
9CCAFD941901B7F500314886 /* SpecialPropertiesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CCAFD931901B7F500314886 /* SpecialPropertiesTests.m */; };
9CD22C8618FF32F7003E66AD /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9CBD6D5118FF2FDD00DE66EC /* XCTest.framework */; };
9CD425861702224F00A42AA1 /* HTTPClientSuite.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CD425851702224F00A42AA1 /* HTTPClientSuite.m */; };
9CD4258B170222AF00A42AA1 /* MockNSURLConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CD42588170222AF00A42AA1 /* MockNSURLConnection.m */; };
Expand Down Expand Up @@ -254,6 +255,7 @@
9CC2FCB6168CE7340059FE67 /* LoanModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LoanModel.m; sourceTree = "<group>"; };
9CC2FCB7168CE7340059FE67 /* LocationModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LocationModel.h; sourceTree = "<group>"; };
9CC2FCB8168CE7340059FE67 /* LocationModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = LocationModel.m; sourceTree = "<group>"; };
9CCAFD931901B7F500314886 /* SpecialPropertiesTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpecialPropertiesTests.m; sourceTree = "<group>"; };
9CD425841702224F00A42AA1 /* HTTPClientSuite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTTPClientSuite.h; sourceTree = "<group>"; };
9CD425851702224F00A42AA1 /* HTTPClientSuite.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HTTPClientSuite.m; sourceTree = "<group>"; };
9CD42587170222AF00A42AA1 /* MockNSURLConnection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MockNSURLConnection.h; path = JSONModelDemoTests/MockNSURLConnection.h; sourceTree = SOURCE_ROOT; };
Expand All @@ -266,7 +268,7 @@
D5F918E9172ADA3F00AC2C8E /* SpecialPropertyModel.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SpecialPropertyModel.h; sourceTree = "<group>"; };
D5F918EA172ADA3F00AC2C8E /* SpecialPropertyModel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpecialPropertyModel.m; sourceTree = "<group>"; };
D5F918EC172ADAF800AC2C8E /* SpecialPropertyNameTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SpecialPropertyNameTests.h; sourceTree = "<group>"; };
D5F918ED172ADAF800AC2C8E /* SpecialPropertyNameTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpecialPropertyNameTests.m; sourceTree = "<group>"; };
D5F918ED172ADAF800AC2C8E /* SpecialPropertyNameTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpecialPropertyNameTests.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -354,6 +356,7 @@
9C735D66170B717F00FF96F5 /* JSONAPITests.m */,
9C735D71170C048C00FF96F5 /* InitFromWebTests.h */,
9C735D72170C048C00FF96F5 /* InitFromWebTests.m */,
9CCAFD931901B7F500314886 /* SpecialPropertiesTests.m */,
);
path = UnitTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -780,6 +783,7 @@
9C66E003168CF09A0015CCDF /* JSONHTTPClient.m in Sources */,
9C66E005168CF09A0015CCDF /* JSONModel+networking.m in Sources */,
9C66E007168CF09A0015CCDF /* JSONKeyMapper.m in Sources */,
9CCAFD941901B7F500314886 /* SpecialPropertiesTests.m in Sources */,
9C66E009168CF09A0015CCDF /* JSONValueTransformer.m in Sources */,
D5F918EB172ADA3F00AC2C8E /* SpecialPropertyModel.m in Sources */,
D5F918EE172ADAF900AC2C8E /* SpecialPropertyNameTests.m in Sources */,
Expand Down
4 changes: 4 additions & 0 deletions JSONModelDemo_iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
9CBBBFB5166BBB21008B4326 /* StorageViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CBBBFB3166BBB21008B4326 /* StorageViewController.m */; };
9CBBBFB6166BBB21008B4326 /* StorageViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9CBBBFB4166BBB21008B4326 /* StorageViewController.xib */; };
9CC2FD2B168CE7830059FE67 /* JSONModelDemo_iOSTests-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 9CC2FCD5168CE7830059FE67 /* JSONModelDemo_iOSTests-Info.plist */; };
9CCAFD921901B44300314886 /* SpecialPropertiesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CCAFD911901B44300314886 /* SpecialPropertiesTests.m */; };
9CD425751701FE0000A42AA1 /* HTTPClientSuite.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CD425731701FDE500A42AA1 /* HTTPClientSuite.m */; };
9CD425781701FF2100A42AA1 /* MTTestSemaphor.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CD425771701FF2100A42AA1 /* MTTestSemaphor.m */; };
9CD4257B1702002900A42AA1 /* MockNSURLConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CD4257A1702002900A42AA1 /* MockNSURLConnection.m */; };
Expand Down Expand Up @@ -296,6 +297,7 @@
9CBD6D4F18FF2D7D00DE66EC /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
9CC2FCD2168CE7830059FE67 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
9CC2FCD5168CE7830059FE67 /* JSONModelDemo_iOSTests-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "JSONModelDemo_iOSTests-Info.plist"; sourceTree = "<group>"; };
9CCAFD911901B44300314886 /* SpecialPropertiesTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SpecialPropertiesTests.m; sourceTree = "<group>"; };
9CD425721701FDE500A42AA1 /* HTTPClientSuite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTTPClientSuite.h; sourceTree = "<group>"; };
9CD425731701FDE500A42AA1 /* HTTPClientSuite.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HTTPClientSuite.m; sourceTree = "<group>"; };
9CD425761701FF2100A42AA1 /* MTTestSemaphor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MTTestSemaphor.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -444,6 +446,7 @@
9C735D63170B716300FF96F5 /* JSONAPITests.m */,
9C735D6E170C007900FF96F5 /* InitFromWebTests.h */,
9C735D6F170C007900FF96F5 /* InitFromWebTests.m */,
9CCAFD911901B44300314886 /* SpecialPropertiesTests.m */,
);
path = UnitTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -973,6 +976,7 @@
9C66E027168CF0AA0015CCDF /* JSONModelArray.m in Sources */,
9C66E029168CF0AA0015CCDF /* JSONModelClassProperty.m in Sources */,
9C66E02B168CF0AA0015CCDF /* JSONModelError.m in Sources */,
9CCAFD921901B44300314886 /* SpecialPropertiesTests.m in Sources */,
9C66E02D168CF0AA0015CCDF /* NSArray+JSONModel.m in Sources */,
9C66E02F168CF0AA0015CCDF /* JSONAPI.m in Sources */,
9C55AF1D1890494E004EBD8A /* GitHubRepoEntity.m in Sources */,
Expand Down
8 changes: 4 additions & 4 deletions JSONModelDemo_iOS/MasterViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,16 @@ @implementation JSONAnswer
@end

@interface TopModel : JSONModel
//@property (assign, nonatomic) int id;
//@property (strong, nonatomic) JSONAnswer<Optional>* answer;

@property (assign, nonatomic) int id;
@property (strong, nonatomic) JSONAnswer<Optional>* answer;
@property (assign, nonatomic, readonly) int rId;
@property (nonatomic, copy) void(^userLocationCompleted)();
@end

@implementation TopModel
+(BOOL)propertyIsIgnored:(NSString *)propertyName
{
return YES;
return NO;
}
@end

Expand Down

0 comments on commit 2673637

Please sign in to comment.