Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for mapping child attribute to a parent attribute #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions BWObjectMapping/BWObjectMapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,43 @@ - (id)objectFromJSON:(id)JSON existingObject:(id)object {
////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)mapDictionary:(NSDictionary *)dict toObject:(id)object withMapping:(BWObjectMapping *)mapping {
[mapping.attributeMappings enumerateKeysAndObjectsUsingBlock:^(id key, BWObjectAttributeMapping *attributeMapping, BOOL *stop) {
[[BWObjectValueMapper shared] setValue:[dict objectForKey:attributeMapping.keyPath]
forKeyPath:attributeMapping.attribute
withAttributeMapping:attributeMapping
forObject:object];

NSRange containsDotString = [attributeMapping.keyPath rangeOfString:@"."];
if (containsDotString.location == NSNotFound) {
[self setValue:[dict objectForKey:attributeMapping.keyPath] forMapping:attributeMapping forObject:object];
} else {
NSArray *keys = [attributeMapping.keyPath componentsSeparatedByString:@"."];
[self setNestedValueForKeys:keys fromDictionary:dict forMapping:attributeMapping forObject:object];
}

}];

if (nil != self.didMapObjectBlock) {
self.didMapObjectBlock(object);
}
}

- (void)setValue:(NSString *)value forMapping:(BWObjectAttributeMapping *)attributeMapping forObject:(id)object
{
[[BWObjectValueMapper shared] setValue:value
forKeyPath:attributeMapping.attribute
withAttributeMapping:attributeMapping
forObject:object];
}

- (void)setNestedValueForKeys:(NSArray *)keys fromDictionary:(NSDictionary *)dictionary forMapping:(BWObjectAttributeMapping *)attributeMapping forObject:(id)object
{
id valuesContainer = dictionary;
id value;
for (NSInteger i = 0; i < [keys count]; i++) {
NSString *key = [keys objectAtIndex:i];
if (i == ([keys count] -1 )) {
value = [valuesContainer objectForKey:key];
} else {
valuesContainer = [valuesContainer objectForKey:key];
}
}
[self setValue:value forMapping:attributeMapping forObject:object];
}

@end
6 changes: 6 additions & 0 deletions BWObjectMappingDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
C69C256416D3F1C30008038E /* Person.m in Sources */ = {isa = PBXBuildFile; fileRef = C69C256316D3F1C30008038E /* Person.m */; };
E7018D4E15C6492600D3ADEE /* BWObjectValueMapper.m in Sources */ = {isa = PBXBuildFile; fileRef = E7018D4D15C6492600D3ADEE /* BWObjectValueMapper.m */; };
E7018D5515C65FB000D3ADEE /* Entity.m in Sources */ = {isa = PBXBuildFile; fileRef = E7018D5415C65FB000D3ADEE /* Entity.m */; };
E7B6880F15BAA9E700002F08 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E7B6880E15BAA9E700002F08 /* UIKit.framework */; };
Expand Down Expand Up @@ -42,6 +43,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
C69C256216D3F1C30008038E /* Person.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Person.h; sourceTree = "<group>"; };
C69C256316D3F1C30008038E /* Person.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Person.m; sourceTree = "<group>"; };
E7018D4C15C6492600D3ADEE /* BWObjectValueMapper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BWObjectValueMapper.h; sourceTree = "<group>"; };
E7018D4D15C6492600D3ADEE /* BWObjectValueMapper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BWObjectValueMapper.m; sourceTree = "<group>"; };
E7018D5315C65FB000D3ADEE /* Entity.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Entity.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -150,6 +153,8 @@
E7018D5315C65FB000D3ADEE /* Entity.h */,
E7018D5415C65FB000D3ADEE /* Entity.m */,
E7B6881715BAA9E700002F08 /* Supporting Files */,
C69C256216D3F1C30008038E /* Person.h */,
C69C256316D3F1C30008038E /* Person.m */,
);
path = BWObjectMappingDemo;
sourceTree = "<group>";
Expand Down Expand Up @@ -314,6 +319,7 @@
E7B68A2A15BC98A100002F08 /* User.m in Sources */,
E7018D4E15C6492600D3ADEE /* BWObjectValueMapper.m in Sources */,
E7018D5515C65FB000D3ADEE /* Entity.m in Sources */,
C69C256416D3F1C30008038E /* Person.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
19 changes: 19 additions & 0 deletions BWObjectMappingDemo/Person.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Person.h
// BWObjectMappingDemo
//
// Created by Lucas Medeiros on 19/02/13.
//
//

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *email;
@property (nonatomic, copy) NSString *skype;
@property (nonatomic, strong) NSArray *phones;
@property (nonatomic, strong) NSDictionary *location;

@end
13 changes: 13 additions & 0 deletions BWObjectMappingDemo/Person.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Person.m
// BWObjectMappingDemo
//
// Created by Lucas Medeiros on 19/02/13.
//
//

#import "Person.h"

@implementation Person

@end
74 changes: 72 additions & 2 deletions BWObjectMappingDemoTests/BWObjectMapperSpecs.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#import "User.h"
#import "Comment.h"
#import "Entity.h"
#import "Person.h"
#import "AppDelegate.h"

#define CUSTOM_VALUE_VALUE @"customValue"
Expand Down Expand Up @@ -54,7 +55,7 @@

NSDictionary *JSON = [NSDictionary dictionaryWithObject:userJSON forKey:@"user"];

NSArray *objects = [[BWObjectMapper shared] objectsFromJSON:JSON];
NSArray *objects = [[BWObjectMapper shared] objectsFromJSON:JSON withObjectClass:[User class]];
User *user = [objects lastObject];
Class class = [[objects lastObject] class];

Expand Down Expand Up @@ -92,7 +93,7 @@
[JSON addObject:dict];
}

int objectCount = [[[BWObjectMapper shared] objectsFromJSON:JSON] count];
int objectCount = [[[BWObjectMapper shared] objectsFromJSON:JSON withObjectClass:[User class]] count];
[[theValue(objectCount) should] equal:theValue(expectedNumberOfObjects)];
});

Expand Down Expand Up @@ -122,6 +123,75 @@

});

context(@"Nested Attributes", ^{

__block Person *person;
__block NSDictionary *JSON;

beforeAll(^{

[BWObjectMapping mappingForObject:[Person class] block:^(BWObjectMapping *mapping) {
[mapping mapKeyPath:@"name" toAttribute:@"name"];
[mapping mapKeyPath:@"contact.email" toAttribute:@"email"];
[mapping mapKeyPath:@"contact.others.skype" toAttribute:@"skype"];
[mapping mapKeyPath:@"contact.phones" toAttribute:@"phones"];
[mapping mapKeyPath:@"address.location" toAttribute:@"location"];
[[BWObjectMapper shared] registerMapping:mapping withRootKeyPath:@"person"];
}];

[[BWObjectMapper shared] objectWithBlock:^id(Class objectClass, NSString *primaryKey, id primaryKeyValue, id JSON) {
return [[objectClass alloc] init];
}];

});

beforeEach(^{

JSON = @{ @"person" : @{ @"name" : @"Lucas",
@"contact" : @{
@"email" : @"[email protected]",
@"phones" : @[ @"(12)1233-1333", @"(85)12331233" ],
@"others" : @{ @"skype" : @"aspmedeiros"}
},
@"address" : @{
@"location" : @{ @"lat": @(-18.123123123), @"long" : @(3.1123123123) }
}
}
};

person = [[BWObjectMapper shared] objectFromJSON:JSON withObjectClass:[Person class]];

});

specify(^{
[[person should] beNonNil];
});

specify(^{
[[person.name should] equal:[[JSON objectForKey:@"person"] objectForKey:@"name"]];
});

specify(^{
[[person.email should] equal:[[[JSON objectForKey:@"person"] objectForKey:@"contact"] objectForKey:@"email"]];
});

specify(^{
[[person.skype should] equal:[[[[JSON objectForKey:@"person"] objectForKey:@"contact"] objectForKey:@"others"] objectForKey:@"skype"]];
});

specify(^{
int phonesCount = [person.phones count];
int expectedPhoneCount = [[[[JSON objectForKey:@"person"] objectForKey:@"contact"] objectForKey:@"phones"] count];
[[theValue(phonesCount) should] equal:theValue(expectedPhoneCount)];
});

specify(^{
[[person.location should] equal:[[[JSON objectForKey:@"person"] objectForKey:@"address"] objectForKey:@"location"]];
});


});

context(@"Core data object", ^{

beforeAll(^{
Expand Down