Skip to content
This repository has been archived by the owner on Sep 5, 2023. It is now read-only.

Commit

Permalink
Merge branch 'release/1.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
vilanovi committed Jul 9, 2015
2 parents 3a30645 + 0f9256e commit b94e167
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Motis.podspec.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Motis",
"version": "1.2.0",
"version": "1.3.0",
"summary": "Easy JSON to NSObject mapping",
"description": "Map and set your JSON objects to objective-C objects using Cocoa's KeyValueCoding. This category sets a minimalist set of methods to map the JSON keys into class properties and set your JSON objects into NSObjects subclasses.\n\t\t \nFor a basic overview of how Motis works checkout the Mobile Jazz blog post: http://blog.mobilejazz.cat/ios-using-kvc-to-parse-json\n\nSimple & Powerful.",
"homepage": "https://github.com/mobilejazz/Motis",
Expand All @@ -14,7 +14,7 @@
"social_media_url": "http://twitter.com/mobilejazzcat",
"source": {
"git": "https://github.com/mobilejazz/Motis.git",
"tag": "1.2.0"
"tag": "1.3.0"
},
"source_files": [
"Motis.h",
Expand Down
16 changes: 12 additions & 4 deletions NSObject+Motis.m
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,21 @@ - (void)mts_setValuesForKeysWithDictionary:(NSDictionary *)keyedValues
for (NSInteger i=0; i<count; ++i)
{
// For each keyPath component

NSString *path = keyPath.components[i];

// check if path is an index in an array
NSInteger index = [path integerValue];
if ((index > 0 || [path isEqualToString:@"0"]) && [value isKindOfClass:NSArray.class]) {
value = [((NSArray *) value) objectAtIndex:index];
} else {
if (index >= 0 && [value isKindOfClass:NSArray.class])
{
NSArray *array = value;

if (index < array.count)
value = array[index];
else
break;
}
else
{
value = [value valueForKey:path];
}

Expand Down
103 changes: 103 additions & 0 deletions SampleProject/Motis Tests/MJValidationTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -818,4 +818,107 @@ - (void)testKeyPathIncorrectAccess
XCTAssertNil(object.stringField, @"KeyPath acces failed");
}

- (void)testArrayKeyPathInteger
{
MJMotisObject *object = [MJMotisObject new];

NSDictionary *dictionary = @{@"array":@[@{@"integer":@0},
@{@"integer":@1},
@{@"integer":@2},
]};

[object mts_setValuesForKeysWithDictionary:dictionary];

XCTAssertEqual(object.array0Integer, 0, @"Array key path on [0] integer failed");
XCTAssertEqual(object.array1Integer, 1, @"Array key path on [1] integer failed");
XCTAssertEqual(object.array2Integer, 2, @"Array key path on [2] integer failed");
}

- (void)testArrayKeyPathString
{
MJMotisObject *object = [MJMotisObject new];

NSDictionary *dictionary = @{@"array":@[@{@"string":@"0"},
@{@"string":@"1"},
@{@"string":@"2"},
]};

[object mts_setValuesForKeysWithDictionary:dictionary];

XCTAssertEqualObjects(object.array0String, @"0", @"Array key path on [0] string failed");
XCTAssertEqualObjects(object.array1String, @"1", @"Array key path on [1] string failed");
XCTAssertEqualObjects(object.array2String, @"2", @"Array key path on [2] string failed");
}

- (void)testArrayKeyPathObject
{
MJMotisObject *object = [MJMotisObject new];
object.stringField = nil;

NSDictionary *dictionary = @{@"array":@[@{@"string": @"0",
@"integer": @0},
@{@"string": @"1",
@"integer": @1},
@{@"string": @"2",
@"integer": @2},
]};

[object mts_setValuesForKeysWithDictionary:dictionary];

XCTAssertEqualObjects(object.array0Object.stringField, @"0", @"Array key path on [0] object string failed");
XCTAssertEqualObjects(object.array1Object.stringField, @"1", @"Array key path on [1] object string failed");
XCTAssertEqualObjects(object.array2Object.stringField, @"2", @"Array key path on [2] object string failed");

XCTAssertEqual(object.array0Object.integerField, 0, @"Array key path on [0] object integer failed");
XCTAssertEqual(object.array1Object.integerField, 1, @"Array key path on [1] object integer failed");
XCTAssertEqual(object.array2Object.integerField, 2, @"Array key path on [2] object integer failed");
}

- (void)testArrayKeyPathOutOfBoundsNoneException
{
MJMotisObject *object = [MJMotisObject new];

object.array0Integer = -1;
object.array1Integer = -1;
object.array2Integer = -1;

NSDictionary *dictionary = @{@"array":@[@{@"integer":@0},
@{@"integer":@1},
]};

[object mts_setValuesForKeysWithDictionary:dictionary];

XCTAssertEqual(object.array0Integer, 0, @"Array key path on [0] integer failed");
XCTAssertEqual(object.array1Integer, 1, @"Array key path on [1] integer failed");
XCTAssertEqual(object.array2Integer, -1, @"Array key path on [2] integer failed");
}

- (void)testArrayKeyPathIntegerArray
{
MJMotisObject *object = [MJMotisObject new];
object.stringField = nil;

NSDictionary *dictionary = @{@"integerArray":@[@0,@1,@2]};

[object mts_setValuesForKeysWithDictionary:dictionary];

XCTAssertEqual(object.integerArray0, 0, @"Array key path on [0] integer failed");
XCTAssertEqual(object.integerArray1, 1, @"Array key path on [1] integer failed");
XCTAssertEqual(object.integerArray2, 2, @"Array key path on [2] integer failed");
}

- (void)testArrayKeyPathStringArray
{
MJMotisObject *object = [MJMotisObject new];
object.stringField = nil;

NSDictionary *dictionary = @{@"stringArray":@[@"0",@"1",@"2"]};

[object mts_setValuesForKeysWithDictionary:dictionary];

XCTAssertEqualObjects(object.stringArray0, @"0", @"Array key path on [0] string failed");
XCTAssertEqualObjects(object.stringArray1, @"1", @"Array key path on [1] string failed");
XCTAssertEqualObjects(object.stringArray2, @"2", @"Array key path on [2] string failed");
}

@end
22 changes: 22 additions & 0 deletions SampleProject/Motis Tests/Validation Test/MJMotisObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ + (NSDictionary*)mts_mapping

@"unsigned_enum": mts_key(unsignedEnum),
@"signed_enum": mts_key(signedEnum),

@"array.0.integer": mts_key(array0Integer),
@"array.1.integer": mts_key(array1Integer),
@"array.2.integer": mts_key(array2Integer),

@"array.0.string": mts_key(array0String),
@"array.1.string": mts_key(array1String),
@"array.2.string": mts_key(array2String),

@"array.0": mts_key(array0Object),
@"array.1": mts_key(array1Object),
@"array.2": mts_key(array2Object),

@"integerArray.0": mts_key(integerArray0),
@"integerArray.1": mts_key(integerArray1),
@"integerArray.2": mts_key(integerArray2),

@"stringArray.0": mts_key(stringArray0),
@"stringArray.1": mts_key(stringArray1),
@"stringArray.2": mts_key(stringArray2),


};
}

Expand Down
20 changes: 20 additions & 0 deletions SampleProject/Motis Tests/Validation Test/MJTestObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,24 @@ typedef NS_ENUM(NSInteger, MJSignedEnum)
@property (nonatomic, assign) MJUnsignedEnum unsignedEnum;
@property (nonatomic, assign) MJSignedEnum signedEnum;

@property (nonatomic, assign) NSInteger array0Integer;
@property (nonatomic, assign) NSInteger array1Integer;
@property (nonatomic, assign) NSInteger array2Integer;

@property (nonatomic, strong) NSString *array0String;
@property (nonatomic, strong) NSString *array1String;
@property (nonatomic, strong) NSString *array2String;

@property (nonatomic, strong) MJMotisObject *array0Object;
@property (nonatomic, strong) MJMotisObject *array1Object;
@property (nonatomic, strong) MJMotisObject *array2Object;

@property (nonatomic, assign) NSInteger integerArray0;
@property (nonatomic, assign) NSInteger integerArray1;
@property (nonatomic, assign) NSInteger integerArray2;

@property (nonatomic, strong) NSString *stringArray0;
@property (nonatomic, strong) NSString *stringArray1;
@property (nonatomic, strong) NSString *stringArray2;

@end
10 changes: 10 additions & 0 deletions SampleProject/Motis/MJVideo.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@
**/
@property (nonatomic, strong) NSArray *cast;

/**
* The second user cast name.
**/
@property (nonatomic, strong) MJUser *secondUserCast;

/**
* The second user cast name.
**/
@property (nonatomic, strong) NSString *secondUserCastName;

/** ********************************************************* **
* @name Other attributes
** ********************************************************* **/
Expand Down
2 changes: 2 additions & 0 deletions SampleProject/Motis/MJVideo.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ + (NSDictionary*)mts_mapping
@"uploader": mts_key(uploader),
@"uploader.user_name": mts_key(uploaderUsername), // <-- KeyPath Access
@"users_cast": mts_key(cast),
@"users_cast.1.user_name": mts_key(secondUserCastName),
@"users_cast.1": mts_key(secondUserCast),
@"likes_count": mts_key(likesCount),
@"stats.start_views": mts_key(startViews), // <-- KeyPath Access
@"stats.end_views": mts_key(endViews), // <-- KeyPath Access
Expand Down

0 comments on commit b94e167

Please sign in to comment.