Skip to content

Fixed linking problem #44

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

Open
wants to merge 3 commits 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
1 change: 1 addition & 0 deletions Classes/JSONAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- (instancetype)initWithString:(NSString*)string;

- (NSDictionary*)dictionary;
- (NSDictionary*)allResources;

- (id)includedResource:(id)ID withType:(NSString*)type;
- (BOOL)hasErrors;
Expand Down
18 changes: 16 additions & 2 deletions Classes/JSONAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ - (void)inflateWithDictionary:(NSDictionary*)dictionary {
// Link included with included
for (NSDictionary *typeIncluded in _includedResources.allValues) {
for (NSObject <JSONAPIResource> *resource in typeIncluded.allValues) {
[JSONAPIResourceParser link:resource withIncluded:self];
[JSONAPIResourceParser link:resource withAll:self];
}
}

// Link data with included
for (NSObject <JSONAPIResource> *resource in _resources) {
[JSONAPIResourceParser link:resource withIncluded:self];
[JSONAPIResourceParser link:resource withAll:self];
}

// Parse errors
Expand Down Expand Up @@ -205,4 +205,18 @@ - (NSArray *)parseRelatedResources:(NSArray *)relatedResources
return parsedResources;
}

- (NSDictionary *)allResources {
NSMutableDictionary *all = self.includedResources.mutableCopy;
[self.resources enumerateObjectsUsingBlock:^(NSObject<JSONAPIResource> * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSString *type = [[obj.class descriptor] type];
NSMutableDictionary *allValue = [all[type] mutableCopy];
if(!allValue) {
allValue = @{}.mutableCopy;
}
allValue[obj.ID] = obj;
all[type] = allValue;
}];
return all;
}

@end
2 changes: 1 addition & 1 deletion Classes/JSONAPIResourceParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
* @param resource model object
* @param jsonAPI JSON-API message object
*/
+ (void)link:(NSObject <JSONAPIResource>*)resource withIncluded:(JSONAPI*)jsonAPI;
+ (void)link:(NSObject <JSONAPIResource>*)resource withAll:(JSONAPI*)jsonAPI;

/**
* Get array of associated resource instances.
Expand Down
18 changes: 9 additions & 9 deletions Classes/JSONAPIResourceParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ + (id)jsonAPILink:(NSDictionary*)dictionary {
}


+ (void)link:(NSObject <JSONAPIResource>*)resource withIncluded:(JSONAPI*)jsonAPI {
+ (void)link:(NSObject <JSONAPIResource>*)resource withAll:(JSONAPI*)jsonAPI {

NSDictionary *included = jsonAPI.includedResources;
if (nil == included) return;
NSDictionary *resources = jsonAPI.allResources;
if (nil == resources) return;

JSONAPIResourceDescriptor *descriptor = [[resource class] descriptor];

Expand All @@ -307,9 +307,9 @@ + (void)link:(NSObject <JSONAPIResource>*)resource withIncluded:(JSONAPI*)jsonAP
[value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj conformsToProtocol:@protocol(JSONAPIResource)]) {
NSObject <JSONAPIResource> *res = obj;
id includedValue = included[[[res.class descriptor] type]];
if (includedValue) {
id v = includedValue[res.ID];
id resourceValue = resources[[[res.class descriptor] type]];
if (resourceValue) {
id v = resourceValue[res.ID];
if (v != nil) {
matched[idx] = v;
}
Expand All @@ -322,9 +322,9 @@ + (void)link:(NSObject <JSONAPIResource>*)resource withIncluded:(JSONAPI*)jsonAP
} else if (value != nil) {
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];
id resourcesValue = resources[[[res.class descriptor] type]];
if (resourcesValue) {
id v = resources[[[res.class descriptor] type]][res.ID];
if (v != nil) {
[resource setValue:v forKey:key];
}
Expand Down
1 change: 1 addition & 0 deletions JSONAPI.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Pod::Spec.new do |s|

s.platform = :ios, '7.0'
s.ios.deployment_target = '5.0'
s.tvos.deployment_target = '9.0'
s.requires_arc = true

s.public_header_files = 'Classes/*.h'
Expand Down