Skip to content

Commit

Permalink
See #7. Complete normalizeRelationships in the SLSerializer so that t…
Browse files Browse the repository at this point in the history
…he relationship IDs resolve to records.
  • Loading branch information
Glavin001 committed Jul 25, 2014
1 parent ac76431 commit ddeba76
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
60 changes: 31 additions & 29 deletions Common/SLSerializer.m
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ - (NSDictionary *)normalizeIdWithPayload:(NSDictionary *)payload

- (NSDictionary *)normalizeAttributes:(Class)modelClass withPayload:(NSDictionary *)payload
{
// NSLog(@"normalizeAttributes Payload: %@", payload);
// NSLog(@"normalizeAttributes Payload: %@", payload);
NSMutableDictionary *results = [NSMutableDictionary dictionaryWithDictionary:payload];
NSDictionary *attributes = [modelClass attributesByName];
for (NSString *attributeKey in attributes)
Expand Down Expand Up @@ -131,40 +131,42 @@ - (NSDictionary *)normalizeRelationships:(Class)modelClass withPayload:(NSDictio
NSLog(@"relationshipKey: %@", relationshipKey);
NSString *payloadKey = [modelClass keyForAttribute:relationshipKey];
NSLog(@"payloadKey: %@", payloadKey);
id origVal = [payload objectForKey:payloadKey];
if (origVal == nil)
{
continue;
}
//
id val = origVal;
NSLog(@"OriginVal: %@", val);
if ([relationship isToMany])
{
// Has Many
NSLog(@"Has Many");
NSMutableArray *r = [NSMutableArray array];
for (NSDictionary *i in (NSArray *)origVal )
{
SLNid j = [SLObjectIdTransform deserialize:(NSDictionary *)i];
NSLog(@"i: %@, j: %@", i, j);
[r addObject:j];
}
val = [NSArray arrayWithArray:r];
}
else
{
// Belongs-To / Has-One
NSLog(@"Belongs To / Has One");
SLNid nid = [SLObjectIdTransform deserialize:(NSDictionary *)origVal];
val = nid;
}
NSLog(@"Val: %@", val);
[results setValue:val forKey:relationshipKey];
if (![relationshipKey isEqualToString:payloadKey])
{
// Attribute's Key is different
// Rename it.
id origVal = [payload objectForKey:payloadKey];
if (origVal == nil)
{
continue;
}
//
id val = origVal;
NSLog(@"OriginVal: %@", val);
if ([relationship isToMany])
{
// Has Many
NSMutableArray *r = [NSMutableArray array];
for (NSDictionary *i in (NSArray *)origVal )
{
SLNid j = [SLObjectIdTransform deserialize:(NSDictionary *)i];
[r addObject:j];
}
val = [NSArray arrayWithArray:r];
}
else
{
// Belongs-To / Has-One
SLNid nid = [SLObjectIdTransform deserialize:(NSDictionary *)origVal];
val = nid;
}
//NSLog(@"Val: %@", val);
[results setValue:val forKey:relationshipKey];
[results removeObjectForKey:payloadKey];
}

}
return [NSDictionary dictionaryWithDictionary:results];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<entity name="SLAttributeCollection" representedClassName="SLAttributeCollection" parentEntity="SLModel" syncable="YES">
<attribute name="desc" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="name" optional="YES" attributeType="String" syncable="YES"/>
<relationship name="attributes" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="SLAttribute" syncable="YES"/>
<relationship name="attributes" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="SLAttribute" syncable="YES"/>
</entity>
<entity name="SLGroup" representedClassName="SLGroup" parentEntity="SLModel" syncable="YES">
<attribute name="desc" optional="YES" attributeType="String" syncable="YES"/>
Expand Down Expand Up @@ -56,7 +56,7 @@
<elements>
<element name="SLAsset" positionX="0" positionY="0" width="128" height="150"/>
<element name="SLAttribute" positionX="0" positionY="0" width="128" height="103"/>
<element name="SLAttributeCollection" positionX="9" positionY="63" width="128" height="88"/>
<element name="SLAttributeCollection" positionX="9" positionY="63" width="128" height="90"/>
<element name="SLGroup" positionX="0" positionY="0" width="128" height="75"/>
<element name="SLModel" positionX="0" positionY="0" width="128" height="105"/>
<element name="SLOrganization" positionX="0" positionY="0" width="128" height="60"/>
Expand Down
15 changes: 10 additions & 5 deletions Streamlyne-iOS-SDKTests/Streamlyne_iOS_SDKTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -333,20 +333,25 @@ - (void) testFindAllAttributeCollections
- (void) testPushAssetWithRelationships
{

NSDictionary *pushData = @{
@"nid": @"538770ab2fb05c514e6cb340",
NSDictionary *payload = @{
@"_id": [SLObjectIdTransform serialize:@"538770ab2fb05c514e6cb340"],
@"attributes": @[
[SLObjectIdTransform serialize:@"abc"],
[SLObjectIdTransform serialize:@"def"]
]
};

SLSerializer *serializer = [[SLSerializer alloc] init];
NSDictionary *pushData = [serializer extractSingle:[SLAsset class] withPayload:payload withStore:[SLStore sharedStore]];
// NSLog(@"pushData: %@", pushData);
SLAsset *a1 = (SLAsset *)[[SLStore sharedStore] push:[SLAsset class] withData:pushData];
// NSLog(@"a1: %@", a1);
XCTAssertStringEqual(pushData[@"nid"], a1.nid, @"`nid`s should match.");

SLAttribute *attr = [SLAttribute initWithId:@"abc"];

NSLog(@"Attr: %@", attr);

NSSet *attrs = a1.attributes;
NSLog(@"Attrs: %@", attrs);
XCTAssert([attrs containsObject:attr], @"Asset's `attributes` relationship should contain this attribute.");
}


Expand Down

0 comments on commit ddeba76

Please sign in to comment.