Skip to content

Add ability to map different types of objects #32

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

Merged
merged 6 commits into from
Dec 21, 2015
Merged
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: objective-c
osx_image: xcode7.1
before_install:
- brew update
- brew uninstall xctool && brew install xctool --HEAD
Expand Down
45 changes: 33 additions & 12 deletions Classes/JSONAPIResourceParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ + (NSDictionary*)dictionaryFor:(NSObject <JSONAPIResource>*)resource {
if (valueArray.count > 0) {
NSMutableArray *dictionaryArray = [[NSMutableArray alloc] initWithCapacity:valueArray.count];

if ([property resourceType]) {
if ([property resourceType] || [((NSArray *)value).firstObject conformsToProtocol:@protocol(JSONAPIResource)]) {
if (linkage == nil) {
linkage = [[NSMutableDictionary alloc] init];
}
Expand All @@ -138,7 +138,7 @@ + (NSDictionary*)dictionaryFor:(NSObject <JSONAPIResource>*)resource {
}
}
} else {
if ([property resourceType]) {
if ([property resourceType] || [value conformsToProtocol:@protocol(JSONAPIResource)]) {
if (linkage == nil) {
linkage = [[NSMutableDictionary alloc] init];
}
Expand Down Expand Up @@ -203,6 +203,11 @@ + (void)set:(NSObject <JSONAPIResource> *)resource withDictionary:dictionary {
[resource setValue:[JSONAPIResourceParser jsonAPILink:value] forKey:key];
}

} else if (relationships[key]) {
if (relationships) {
id value = relationships[key];
[resource setValue:[JSONAPIResourceParser jsonAPILink:value] forKey:key];
}
} else {
id value = [attributes objectForKey:[property jsonName]];;
if ((id)[NSNull null] == value) {
Expand Down Expand Up @@ -270,29 +275,45 @@ + (void)link:(NSObject <JSONAPIResource>*)resource withIncluded:(JSONAPI*)jsonAP
for (NSString *key in properties) {
JSONAPIPropertyDescriptor *propertyDescriptor = [properties objectForKey:key];
id value = [resource valueForKey:key];
id includedValue = included[[[propertyDescriptor.resourceType descriptor] type]];

Class valueClass = nil;
if (propertyDescriptor.resourceType) {
valueClass = propertyDescriptor.resourceType;
} else if ([value conformsToProtocol:@protocol(JSONAPIResource)] || [value isKindOfClass:[NSArray class]]) {
valueClass = [value class];
}

// ordinary attribute
if (propertyDescriptor.resourceType == nil) {
if (valueClass == nil) {
continue;
// has many
} else if ([value isKindOfClass:[NSArray class]]) {
NSMutableArray *matched = [value mutableCopy];
[value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSObject <JSONAPIResource> *res = obj;
id v = includedValue[res.ID];
if (v != nil) {
matched[idx] = v;
if ([obj conformsToProtocol:@protocol(JSONAPIResource)]) {
NSObject <JSONAPIResource> *res = obj;
id includedValue = included[[[res.class descriptor] type]];
if (includedValue) {
id v = includedValue[res.ID];
if (v != nil) {
matched[idx] = v;
}
}
}
}];

[resource setValue:matched forKey:key];
// has one
} else if (value != nil) {
NSObject <JSONAPIResource> *res = value;
id v = includedValue[res.ID];
if (v != nil) {
[resource setValue:v forKey:key];
if ([value conformsToProtocol:@protocol(JSONAPIResource)]) {
id <JSONAPIResource> res = value;
id includedValue = included[[[res.class descriptor] type]];
if (includedValue) {
id v = included[[[res.class descriptor] type]][res.ID];
if (v != nil) {
[resource setValue:v forKey:key];
}
}
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions Project/JSONAPI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@
685481EB1AE4161900D3A633 /* JSONAPIPropertyDescriptor.m in Sources */ = {isa = PBXBuildFile; fileRef = 685481EA1AE4161900D3A633 /* JSONAPIPropertyDescriptor.m */; };
685481EF1AE419CB00D3A633 /* JSONAPIResourceDescriptor.m in Sources */ = {isa = PBXBuildFile; fileRef = 685481EE1AE419CB00D3A633 /* JSONAPIResourceDescriptor.m */; };
68A469941AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m in Sources */ = {isa = PBXBuildFile; fileRef = 68A469931AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m */; };
781CF6CA1C1ECC260052D755 /* generic_relationships_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 781CF6C91C1ECC260052D755 /* generic_relationships_example.json */; };
781CF6CB1C1ECC710052D755 /* generic_relationships_example.json in Resources */ = {isa = PBXBuildFile; fileRef = 781CF6C91C1ECC260052D755 /* generic_relationships_example.json */; };
78A5C1CA1C1E1336008C8632 /* NewsFeedPostResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 78A5C1C91C1E1336008C8632 /* NewsFeedPostResource.m */; };
78A5C1CD1C1E13A8008C8632 /* UserResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 78A5C1CC1C1E13A8008C8632 /* UserResource.m */; };
78A5C1D01C1E147B008C8632 /* SocialCommunityResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 78A5C1CF1C1E147B008C8632 /* SocialCommunityResource.m */; };
78A5C1D31C1E14D6008C8632 /* MediaResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 78A5C1D21C1E14D6008C8632 /* MediaResource.m */; };
78A5C1D61C1E154C008C8632 /* WebPageResource.m in Sources */ = {isa = PBXBuildFile; fileRef = 78A5C1D51C1E154C008C8632 /* WebPageResource.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -109,6 +116,17 @@
68A469921AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDateFormatter+JSONAPIDateFormatter.h"; sourceTree = "<group>"; };
68A469931AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDateFormatter+JSONAPIDateFormatter.m"; sourceTree = "<group>"; };
68DF9E951B06B4C500FA6429 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
781CF6C91C1ECC260052D755 /* generic_relationships_example.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = generic_relationships_example.json; sourceTree = "<group>"; };
78A5C1C81C1E1336008C8632 /* NewsFeedPostResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NewsFeedPostResource.h; sourceTree = "<group>"; };
78A5C1C91C1E1336008C8632 /* NewsFeedPostResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NewsFeedPostResource.m; sourceTree = "<group>"; };
78A5C1CB1C1E13A8008C8632 /* UserResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UserResource.h; sourceTree = "<group>"; };
78A5C1CC1C1E13A8008C8632 /* UserResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UserResource.m; sourceTree = "<group>"; };
78A5C1CE1C1E147B008C8632 /* SocialCommunityResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SocialCommunityResource.h; sourceTree = "<group>"; };
78A5C1CF1C1E147B008C8632 /* SocialCommunityResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SocialCommunityResource.m; sourceTree = "<group>"; };
78A5C1D11C1E14D6008C8632 /* MediaResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MediaResource.h; sourceTree = "<group>"; };
78A5C1D21C1E14D6008C8632 /* MediaResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MediaResource.m; sourceTree = "<group>"; };
78A5C1D41C1E154C008C8632 /* WebPageResource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WebPageResource.h; sourceTree = "<group>"; };
78A5C1D51C1E154C008C8632 /* WebPageResource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WebPageResource.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -144,6 +162,16 @@
03866450186A909200985CEC /* PeopleResource.m */,
03866452186A94B700985CEC /* CommentResource.h */,
03866453186A94B700985CEC /* CommentResource.m */,
78A5C1C81C1E1336008C8632 /* NewsFeedPostResource.h */,
78A5C1C91C1E1336008C8632 /* NewsFeedPostResource.m */,
78A5C1CB1C1E13A8008C8632 /* UserResource.h */,
78A5C1CC1C1E13A8008C8632 /* UserResource.m */,
78A5C1CE1C1E147B008C8632 /* SocialCommunityResource.h */,
78A5C1CF1C1E147B008C8632 /* SocialCommunityResource.m */,
78A5C1D11C1E14D6008C8632 /* MediaResource.h */,
78A5C1D21C1E14D6008C8632 /* MediaResource.m */,
78A5C1D41C1E154C008C8632 /* WebPageResource.h */,
78A5C1D51C1E154C008C8632 /* WebPageResource.m */,
);
name = ResourceModels;
sourceTree = "<group>";
Expand Down Expand Up @@ -253,6 +281,7 @@
03FBD8D81AB879E800789DF3 /* Assets */ = {
isa = PBXGroup;
children = (
781CF6C91C1ECC260052D755 /* generic_relationships_example.json */,
03FBD8D91AB879FD00789DF3 /* main_example.json */,
03FBD8E11AB8E46E00789DF3 /* error_example.json */,
);
Expand Down Expand Up @@ -339,6 +368,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
781CF6CB1C1ECC710052D755 /* generic_relationships_example.json in Resources */,
03A055D31868E038004807F0 /* Images.xcassets in Resources */,
03866459186CCE6600985CEC /* CHANGELOG.md in Resources */,
03A055C51868E038004807F0 /* InfoPlist.strings in Resources */,
Expand All @@ -355,6 +385,7 @@
files = (
03FBD8E31AB8E46E00789DF3 /* error_example.json in Resources */,
03A055E41868E038004807F0 /* InfoPlist.strings in Resources */,
781CF6CA1C1ECC260052D755 /* generic_relationships_example.json in Resources */,
03FBD8DA1AB879FD00789DF3 /* main_example.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -382,16 +413,21 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
78A5C1CD1C1E13A8008C8632 /* UserResource.m in Sources */,
78A5C1D61C1E154C008C8632 /* WebPageResource.m in Sources */,
03866451186A909200985CEC /* PeopleResource.m in Sources */,
681B47761B08EA9800A99D76 /* JSONAPIResourceBase.m in Sources */,
78A5C1CA1C1E1336008C8632 /* NewsFeedPostResource.m in Sources */,
680E14F51B08146E004FF8CD /* JSONAPIResourceParser.m in Sources */,
03866457186A94C200985CEC /* ArticleResource.m in Sources */,
03A055D11868E038004807F0 /* ViewController.m in Sources */,
03A055F71868E08A004807F0 /* JSONAPI.m in Sources */,
78A5C1D31C1E14D6008C8632 /* MediaResource.m in Sources */,
68A469941AE47E0000E7BBC8 /* NSDateFormatter+JSONAPIDateFormatter.m in Sources */,
685481EB1AE4161900D3A633 /* JSONAPIPropertyDescriptor.m in Sources */,
03866454186A94B700985CEC /* CommentResource.m in Sources */,
03A055CB1868E038004807F0 /* AppDelegate.m in Sources */,
78A5C1D01C1E147B008C8632 /* SocialCommunityResource.m in Sources */,
685481EF1AE419CB00D3A633 /* JSONAPIResourceDescriptor.m in Sources */,
03A055C71868E038004807F0 /* main.m in Sources */,
03FBD8DF1AB8DF8E00789DF3 /* JSONAPIErrorResource.m in Sources */,
Expand Down
16 changes: 16 additions & 0 deletions Project/JSONAPI/MediaResource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// MediaResource.h
// JSONAPI
//
// Created by Rafael Kayumov on 14.12.15.
// Copyright © 2015 Josh Holtz. All rights reserved.
//

#import "JSONAPIResourceBase.h"

@interface MediaResource : JSONAPIResourceBase

@property (nonatomic, strong) NSString *mimeType;
@property (nonatomic, strong) NSString *fileUrl;

@end
32 changes: 32 additions & 0 deletions Project/JSONAPI/MediaResource.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// MediaResource.m
// JSONAPI
//
// Created by Rafael Kayumov on 14.12.15.
// Copyright © 2015 Josh Holtz. All rights reserved.
//

#import "MediaResource.h"

#import "JSONAPIPropertyDescriptor.h"
#import "JSONAPIResourceDescriptor.h"

@implementation MediaResource

static JSONAPIResourceDescriptor *__descriptor = nil;

+ (JSONAPIResourceDescriptor*)descriptor {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"Media"];

[__descriptor setIdProperty:@"ID"];

[__descriptor addProperty:@"mimeType"];
[__descriptor addProperty:@"fileUrl"];
});

return __descriptor;
}

@end
24 changes: 24 additions & 0 deletions Project/JSONAPI/NewsFeedPostResource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// NewsFeedPostResource.h
// JSONAPI
//
// Created by Rafael Kayumov on 13.12.15.
// Copyright © 2015 Josh Holtz. All rights reserved.
//

#import "JSONAPIResourceBase.h"
#import "UserResource.h"
#import "SocialCommunityResource.h"
#import "MediaResource.h"
#import "WebPageResource.h"

@interface NewsFeedPostResource : JSONAPIResourceBase

@property (nonatomic, strong) NSDate *createdAt;
@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *text;

@property (nonatomic, strong) id<JSONAPIResource> publisher;
@property (nonatomic, strong) NSArray *attachments;

@end
38 changes: 38 additions & 0 deletions Project/JSONAPI/NewsFeedPostResource.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// NewsFeedPostResource.m
// JSONAPI
//
// Created by Rafael Kayumov on 13.12.15.
// Copyright © 2015 Josh Holtz. All rights reserved.
//

#import "NewsFeedPostResource.h"

#import "JSONAPIPropertyDescriptor.h"
#import "JSONAPIResourceDescriptor.h"
#import "NSDateFormatter+JSONAPIDateFormatter.h"

@implementation NewsFeedPostResource

static JSONAPIResourceDescriptor *__descriptor = nil;

+ (JSONAPIResourceDescriptor*)descriptor {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"NewsFeedPost"];

[__descriptor setIdProperty:@"ID"];

[__descriptor addProperty:@"createdAt"
withDescription:[[JSONAPIPropertyDescriptor alloc] initWithJsonName:@"createdAt" withFormat:[NSDateFormatter RFC3339DateFormatter]]];
[__descriptor addProperty:@"title"];
[__descriptor addProperty:@"text"];

[__descriptor hasOne:nil withName:@"publisher"];
[__descriptor hasMany:nil withName:@"attachments"];
});

return __descriptor;
}

@end
16 changes: 16 additions & 0 deletions Project/JSONAPI/SocialCommunityResource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// SocialCommunityResource.h
// JSONAPI
//
// Created by Rafael Kayumov on 14.12.15.
// Copyright © 2015 Josh Holtz. All rights reserved.
//

#import "JSONAPIResourceBase.h"

@interface SocialCommunityResource : JSONAPIResourceBase

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *homePage;

@end
32 changes: 32 additions & 0 deletions Project/JSONAPI/SocialCommunityResource.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// SocialCommunityResource.m
// JSONAPI
//
// Created by Rafael Kayumov on 14.12.15.
// Copyright © 2015 Josh Holtz. All rights reserved.
//

#import "SocialCommunityResource.h"

#import "JSONAPIPropertyDescriptor.h"
#import "JSONAPIResourceDescriptor.h"

@implementation SocialCommunityResource

static JSONAPIResourceDescriptor *__descriptor = nil;

+ (JSONAPIResourceDescriptor*)descriptor {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__descriptor = [[JSONAPIResourceDescriptor alloc] initWithClass:[self class] forLinkedType:@"SocialCommunity"];

[__descriptor setIdProperty:@"ID"];

[__descriptor addProperty:@"title"];
[__descriptor addProperty:@"homePage"];
});

return __descriptor;
}

@end
16 changes: 16 additions & 0 deletions Project/JSONAPI/UserResource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// UserResource.h
// JSONAPI
//
// Created by Rafael Kayumov on 13.12.15.
// Copyright © 2015 Josh Holtz. All rights reserved.
//

#import "JSONAPIResourceBase.h"

@interface UserResource : JSONAPIResourceBase

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *email;

@end
Loading