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

Only update resource id if it is nil or not the same as value to be updated #54

Open
wants to merge 4 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
6 changes: 3 additions & 3 deletions Classes/JSONAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ - (void)inflateWithDictionary:(NSDictionary*)dictionary {
JSONAPIResourceDescriptor *desc = [JSONAPIResourceDescriptor forLinkedType:data[@"type"]];

NSMutableDictionary *typeDict = includedResources[desc.type] ?: @{}.mutableCopy;
typeDict[resource.ID] = resource;
typeDict[resource.iD] = resource;

includedResources[desc.type] = typeDict;
}
Expand All @@ -136,7 +136,7 @@ - (void)inflateWithDictionary:(NSDictionary*)dictionary {
JSONAPIResourceDescriptor *desc = [JSONAPIResourceDescriptor forLinkedType:data[@"type"]];

NSMutableDictionary *typeDict = includedResources[desc.type] ?: @{}.mutableCopy;
typeDict[resource.ID] = resource;
typeDict[resource.iD] = resource;

includedResources[desc.type] = typeDict;
}
Expand Down Expand Up @@ -190,7 +190,7 @@ - (NSDictionary *)mapIncludedResources:(NSArray *)relatedResources forResource:(
for (NSObject <JSONAPIResource> *linked in relatedResources) {
JSONAPIResourceDescriptor *desc = [[linked class] descriptor];
NSMutableDictionary *typeDict = includedResources[desc.type] ?: @{}.mutableCopy;
typeDict[linked.ID] = resource;
typeDict[linked.iD] = resource;
includedResources[desc.type] = typeDict;
}
return includedResources;
Expand Down
2 changes: 1 addition & 1 deletion Classes/JSONAPIResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
*
* @return The record identifier for a resource instance.
*/
- (id)ID;
- (id)iD;

/**
* Set the API record identifier for a resource instance. Required for resources that come
Expand Down
16 changes: 16 additions & 0 deletions Classes/JSONAPIResourceFactory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// JSONAPIResourceFactory.h
// JSONAPI
//
// Created by V-Ken Chin on 11/4/18.
// Copyright © 2018 Josh Holtz. All rights reserved.
//

#ifndef JSONAPIResourceFactory_h
#define JSONAPIResourceFactory_h

#import "JSONAPIResource.h"
@protocol JSONAPIResourceFactory <JSONAPIResource>
+ (id<JSONAPIResource>)resourceObjectFor:(NSDictionary *)dictionary;
@end
#endif /* JSONAPIResourceFactory_h */
36 changes: 22 additions & 14 deletions Classes/JSONAPIResourceParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "JSONAPI.h"
#import "JSONAPIResourceDescriptor.h"
#import "JSONAPIPropertyDescriptor.h"
#import "JSONAPIResourceFactory.h"

#pragma mark - JSONAPIResourceParser

Expand Down Expand Up @@ -56,7 +57,12 @@ @implementation JSONAPIResourceParser
NSString *type = dictionary[@"type"] ?: @"";
JSONAPIResourceDescriptor *descriptor = [JSONAPIResourceDescriptor forLinkedType:type];

NSObject <JSONAPIResource> *resource = [[[descriptor resourceClass] alloc] init];
NSObject <JSONAPIResource> *resource;
if ([[descriptor resourceClass] conformsToProtocol:@protocol(JSONAPIResourceFactory)]) {
resource = [[descriptor resourceClass] resourceObjectFor:dictionary];
} else {
resource = [[[descriptor resourceClass] alloc] init];
}
[self set:resource withDictionary:dictionary];

return resource;
Expand Down Expand Up @@ -118,7 +124,7 @@ + (NSDictionary*)dictionaryFor:(NSObject <JSONAPIResource>*)resource {
}

for (id valueElement in valueArray) {
[dictionaryArray addObject:[self link:valueElement from:resource withKey:[property jsonName]]];
[dictionaryArray addObject:[self link:valueElement from:resource withKey:key]];
}

NSDictionary *dataDictionary = @{@"data" : dictionaryArray};
Expand All @@ -144,7 +150,7 @@ + (NSDictionary*)dictionaryFor:(NSObject <JSONAPIResource>*)resource {
}

NSObject <JSONAPIResource> *attribute = value;
[linkage setValue:[self link:attribute from:resource withKey:[property jsonName]] forKey:[property jsonName]];
[linkage setValue:[self link:attribute from:resource withKey:key] forKey:[property jsonName]];
} else {
format = [property formatter];
if (format) {
Expand Down Expand Up @@ -185,15 +191,17 @@ + (void)set:(NSObject <JSONAPIResource> *)resource withDictionary:dictionary {

id ID = [dictionary objectForKey:@"id"];
NSFormatter *format = [descriptor idFormatter];
if (format) {
id xformed;
if ([format getObjectValue:&xformed forString:ID errorDescription:&error]) {
[resource setValue:xformed forKey:[descriptor idProperty]];
if (resource.iD == nil || ![resource.iD isEqualToString:ID]) {
if (format) {
id xformed;
if ([format getObjectValue:&xformed forString:ID errorDescription:&error]) {
[resource setValue:xformed forKey:[descriptor idProperty]];
}
} else {
[resource setValue:ID forKey:[descriptor idProperty]];
}
} else {
[resource setValue:ID forKey:[descriptor idProperty]];
}

if (descriptor.selfLinkProperty) {
NSString *selfLink = links[@"self"];
[resource setValue:selfLink forKey:descriptor.selfLinkProperty];
Expand Down Expand Up @@ -309,7 +317,7 @@ + (void)link:(NSObject <JSONAPIResource>*)resource withIncluded:(JSONAPI*)jsonAP
NSObject <JSONAPIResource> *res = obj;
id includedValue = included[[[res.class descriptor] type]];
if (includedValue) {
id v = includedValue[res.ID];
id v = includedValue[res.iD];
if (v != nil) {
matched[idx] = v;
}
Expand All @@ -324,7 +332,7 @@ + (void)link:(NSObject <JSONAPIResource>*)resource withIncluded:(JSONAPI*)jsonAP
id <JSONAPIResource> res = value;
id includedValue = included[[[res.class descriptor] type]];
if (includedValue) {
id v = included[[[res.class descriptor] type]][res.ID];
id v = included[[[res.class descriptor] type]][res.iD];
if (v != nil) {
[resource setValue:v forKey:key];
}
Expand Down Expand Up @@ -371,10 +379,10 @@ + (NSDictionary*)link:(NSObject <JSONAPIResource>*)resource from:(NSObject <JSON
[reference setValue:related forKey:@"related"];
}

if (resource.ID) {
if (resource.iD) {
NSDictionary *referenceObject = @{
@"type" : descriptor.type,
@"id" : resource.ID
@"id" : resource.iD
};
if ([[owner valueForKey:key] isKindOfClass:[NSArray class]]) {
reference = referenceObject.mutableCopy;
Expand Down
2 changes: 2 additions & 0 deletions Project/JSONAPI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
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>"; };
86CE9D90207E168300D76280 /* JSONAPIResourceFactory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JSONAPIResourceFactory.h; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -268,6 +269,7 @@
685481E91AE4161900D3A633 /* JSONAPIPropertyDescriptor.h */,
685481EA1AE4161900D3A633 /* JSONAPIPropertyDescriptor.m */,
033A6C5D18695CD2001CF9FA /* JSONAPIResource.h */,
86CE9D90207E168300D76280 /* JSONAPIResourceFactory.h */,
03FBD8DD1AB8DF8E00789DF3 /* JSONAPIErrorResource.h */,
03FBD8DE1AB8DF8E00789DF3 /* JSONAPIErrorResource.m */,
685481ED1AE419CB00D3A633 /* JSONAPIResourceDescriptor.h */,
Expand Down