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

Add deep mapping support for to-many relationships #385

Merged
merged 3 commits into from
Mar 20, 2017
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
36 changes: 31 additions & 5 deletions Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,12 @@
14867F861E7AF4D2001D228A /* JSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14867DD21E7AF4D2001D228A /* JSON.swift */; };
14867F871E7AF4D2001D228A /* JSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14867DD21E7AF4D2001D228A /* JSON.swift */; };
14867F881E7AF4D2001D228A /* JSON.swift in Sources */ = {isa = PBXBuildFile; fileRef = 14867DD21E7AF4D2001D228A /* JSON.swift */; };
149FF9EA1E808C4400617A63 /* 375.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 149FF9E81E808C4400617A63 /* 375.xcdatamodeld */; };
149FF9EB1E808C4400617A63 /* 375.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 149FF9E81E808C4400617A63 /* 375.xcdatamodeld */; };
149FF9EC1E808C4400617A63 /* 375.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 149FF9E81E808C4400617A63 /* 375.xcdatamodeld */; };
149FF9EE1E808DFB00617A63 /* 375.json in Resources */ = {isa = PBXBuildFile; fileRef = 149FF9ED1E808DFB00617A63 /* 375.json */; };
149FF9EF1E808DFB00617A63 /* 375.json in Resources */ = {isa = PBXBuildFile; fileRef = 149FF9ED1E808DFB00617A63 /* 375.json */; };
149FF9F01E808DFB00617A63 /* 375.json in Resources */ = {isa = PBXBuildFile; fileRef = 149FF9ED1E808DFB00617A63 /* 375.json */; };
14A84E951E4E70B900701B8A /* Sync.swift in Headers */ = {isa = PBXBuildFile; fileRef = 44968BF91DA0021300E71A53 /* Sync.swift */; settings = {ATTRIBUTES = (Public, ); }; };
14A84E961E4E70B900701B8A /* Sync+DataStack.swift in Headers */ = {isa = PBXBuildFile; fileRef = 142CD2A31DEF39AE002FDABE /* Sync+DataStack.swift */; settings = {ATTRIBUTES = (Public, ); }; };
14A84E971E4E70B900701B8A /* Sync+NSPersistentContainer.swift in Headers */ = {isa = PBXBuildFile; fileRef = 142CD2AC1DEF3A01002FDABE /* Sync+NSPersistentContainer.swift */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -829,6 +835,8 @@
14867DD21E7AF4D2001D228A /* JSON.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSON.swift; sourceTree = "<group>"; };
14975BE81DBC368B0024901A /* tvOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = tvOSTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
14975BF41DBC36960024901A /* macOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = macOSTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
149FF9E91E808C4400617A63 /* 113.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = 113.xcdatamodel; sourceTree = "<group>"; };
149FF9ED1E808DFB00617A63 /* 375.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = 375.json; sourceTree = "<group>"; };
14AF6AFA1DFC3D88009E5BC4 /* Result.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Result.swift; sourceTree = "<group>"; };
14C0AF811BD6D4230009ECBE /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = "<group>"; };
14C0AF821BD6D4230009ECBE /* CONTRIBUTING.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CONTRIBUTING.md; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1090,6 +1098,7 @@
14867D1F1E7AF4D2001D228A /* JSONs */ = {
isa = PBXGroup;
children = (
14867D341E7AF4D2001D228A /* 3ca82a0.json */,
14867D201E7AF4D2001D228A /* 151-many-to-many-notes-update.json */,
14867D211E7AF4D2001D228A /* 151-many-to-many-notes.json */,
14867D221E7AF4D2001D228A /* 151-many-to-many-tags.json */,
Expand All @@ -1110,7 +1119,7 @@
14867D311E7AF4D2001D228A /* 280.json */,
14867D321E7AF4D2001D228A /* 283.json */,
14867D331E7AF4D2001D228A /* 320.json */,
14867D341E7AF4D2001D228A /* 3ca82a0.json */,
149FF9ED1E808DFB00617A63 /* 375.json */,
14867D351E7AF4D2001D228A /* bug-113-comments-no-id.json */,
14867D361E7AF4D2001D228A /* bug-113-custom_relationship_key_to_one.json */,
14867D371E7AF4D2001D228A /* bug-113-stories-comments-no-ids.json */,
Expand All @@ -1132,10 +1141,10 @@
14867D471E7AF4D2001D228A /* images.json */,
14867D481E7AF4D2001D228A /* markets_items.json */,
14867D491E7AF4D2001D228A /* notes_for_user_a.json */,
14867D4A1E7AF4D2001D228A /* notes_with_user_id.json */,
14867D4B1E7AF4D2001D228A /* notes_with_user_id_custom.json */,
14867D4C1E7AF4D2001D228A /* numbers.json */,
14867D4A1E7AF4D2001D228A /* notes_with_user_id.json */,
14867D4D1E7AF4D2001D228A /* numbers_in_collection.json */,
14867D4C1E7AF4D2001D228A /* numbers.json */,
14867D4E1E7AF4D2001D228A /* operation-types-users-a.json */,
14867D4F1E7AF4D2001D228A /* operation-types-users-b.json */,
14867D501E7AF4D2001D228A /* organizations-tree.json */,
Expand All @@ -1158,6 +1167,8 @@
14867D5D1E7AF4D2001D228A /* Models */ = {
isa = PBXGroup;
children = (
14867D841E7AF4D2001D228A /* 3ca82a0.xcdatamodeld */,
14867D861E7AF4D2001D228A /* 84.xcdatamodeld */,
14867D5E1E7AF4D2001D228A /* 113.xcdatamodeld */,
14867D601E7AF4D2001D228A /* 125.xcdatamodeld */,
14867D621E7AF4D2001D228A /* 151-many-to-many.xcdatamodeld */,
Expand All @@ -1177,8 +1188,7 @@
14867D7E1E7AF4D2001D228A /* 280.xcdatamodeld */,
14867D801E7AF4D2001D228A /* 283.xcdatamodeld */,
14867D821E7AF4D2001D228A /* 320.xcdatamodeld */,
14867D841E7AF4D2001D228A /* 3ca82a0.xcdatamodeld */,
14867D861E7AF4D2001D228A /* 84.xcdatamodeld */,
149FF9E81E808C4400617A63 /* 375.xcdatamodeld */,
14867D881E7AF4D2001D228A /* Camelcase.xcdatamodeld */,
14867D8A1E7AF4D2001D228A /* Contacts.xcdatamodeld */,
14867D8C1E7AF4D2001D228A /* CustomRelationshipKey.xcdatamodeld */,
Expand Down Expand Up @@ -1727,6 +1737,7 @@
14867E2A1E7AF4D2001D228A /* 157-locations.json in Resources */,
14867E241E7AF4D2001D228A /* 157-cities.json in Resources */,
14867E451E7AF4D2001D228A /* 280.json in Resources */,
149FF9EE1E808DFB00617A63 /* 375.json in Resources */,
14867E391E7AF4D2001D228A /* 233.json in Resources */,
14867E4E1E7AF4D2001D228A /* 3ca82a0.json in Resources */,
14867E1B1E7AF4D2001D228A /* 151-to-many-notes.json in Resources */,
Expand Down Expand Up @@ -1802,6 +1813,7 @@
14867E2B1E7AF4D2001D228A /* 157-locations.json in Resources */,
14867E251E7AF4D2001D228A /* 157-cities.json in Resources */,
14867E461E7AF4D2001D228A /* 280.json in Resources */,
149FF9EF1E808DFB00617A63 /* 375.json in Resources */,
14867E3A1E7AF4D2001D228A /* 233.json in Resources */,
14867E4F1E7AF4D2001D228A /* 3ca82a0.json in Resources */,
14867E1C1E7AF4D2001D228A /* 151-to-many-notes.json in Resources */,
Expand Down Expand Up @@ -1877,6 +1889,7 @@
14867E2C1E7AF4D2001D228A /* 157-locations.json in Resources */,
14867E261E7AF4D2001D228A /* 157-cities.json in Resources */,
14867E471E7AF4D2001D228A /* 280.json in Resources */,
149FF9F01E808DFB00617A63 /* 375.json in Resources */,
14867E3B1E7AF4D2001D228A /* 233.json in Resources */,
14867E501E7AF4D2001D228A /* 3ca82a0.json in Resources */,
14867E1D1E7AF4D2001D228A /* 151-to-many-notes.json in Resources */,
Expand Down Expand Up @@ -2127,6 +2140,7 @@
14867DEE1E7AF4D2001D228A /* LightweightMigrationModel.xcdatamodel in Sources */,
14867F1D1E7AF4D2001D228A /* NotesB.xcdatamodeld in Sources */,
14867F5C1E7AF4D2001D228A /* 121.xcdatamodeld in Sources */,
149FF9EA1E808C4400617A63 /* 375.xcdatamodeld in Sources */,
14867F7D1E7AF4D2001D228A /* DateStringTransformer.m in Sources */,
14867F4D1E7AF4D2001D228A /* UpdateTests.swift in Sources */,
14867F231E7AF4D2001D228A /* Organizations.xcdatamodeld in Sources */,
Expand Down Expand Up @@ -2212,6 +2226,7 @@
14867DEF1E7AF4D2001D228A /* LightweightMigrationModel.xcdatamodel in Sources */,
14867F1E1E7AF4D2001D228A /* NotesB.xcdatamodeld in Sources */,
14867F5D1E7AF4D2001D228A /* 121.xcdatamodeld in Sources */,
149FF9EB1E808C4400617A63 /* 375.xcdatamodeld in Sources */,
14867F7E1E7AF4D2001D228A /* DateStringTransformer.m in Sources */,
14867F4E1E7AF4D2001D228A /* UpdateTests.swift in Sources */,
14867F241E7AF4D2001D228A /* Organizations.xcdatamodeld in Sources */,
Expand Down Expand Up @@ -2297,6 +2312,7 @@
14867DF01E7AF4D2001D228A /* LightweightMigrationModel.xcdatamodel in Sources */,
14867F1F1E7AF4D2001D228A /* NotesB.xcdatamodeld in Sources */,
14867F5E1E7AF4D2001D228A /* 121.xcdatamodeld in Sources */,
149FF9EC1E808C4400617A63 /* 375.xcdatamodeld in Sources */,
14867F7F1E7AF4D2001D228A /* DateStringTransformer.m in Sources */,
14867F4F1E7AF4D2001D228A /* UpdateTests.swift in Sources */,
14867F251E7AF4D2001D228A /* Organizations.xcdatamodeld in Sources */,
Expand Down Expand Up @@ -3394,6 +3410,16 @@
sourceTree = "<group>";
versionGroupType = wrapper.xcdatamodel;
};
149FF9E81E808C4400617A63 /* 375.xcdatamodeld */ = {
isa = XCVersionGroup;
children = (
149FF9E91E808C4400617A63 /* 113.xcdatamodel */,
);
currentVersion = 149FF9E91E808C4400617A63 /* 113.xcdatamodel */;
path = 375.xcdatamodeld;
sourceTree = "<group>";
versionGroupType = wrapper.xcdatamodel;
};
44B5481C1DCF3E18009215FA /* iOSDemo.xcdatamodeld */ = {
isa = XCVersionGroup;
children = (
Expand Down
12 changes: 11 additions & 1 deletion Source/Sync/NSManagedObject+Sync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,17 @@ extension NSManagedObject {
}
} else {
if let customRelationshipName = relationship.userInfo?[SyncCustomRemoteKey] as? String {
children = dictionary[customRelationshipName] as? [[String: Any]]
if customRelationshipName.contains(".") {
if let deepMapingRootKey = customRelationshipName.components(separatedBy: ".").first {
if let rootObject = dictionary[deepMapingRootKey] as? [String: Any] {
if let deepMapingLeaveKey = customRelationshipName.components(separatedBy: ".").last {
children = rootObject[deepMapingLeaveKey] as? [[String: Any]]
}
}
}
} else {
children = dictionary[customRelationshipName] as? [[String: Any]]
}
} else if let result = dictionary[relationship.name.hyp_snakeCase()] as? [[String: Any]] {
children = result
} else if let result = dictionary[relationship.name] as? [[String: Any]] {
Expand Down
12 changes: 12 additions & 0 deletions Tests/Sync/JSONs/375.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"id": 4865,
"custom": {
"series": [
{
"id": 123
}
]
}
}
]
8 changes: 8 additions & 0 deletions Tests/Sync/Models/375.xcdatamodeld/.xccurrentversion
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>_XCCurrentVersionName</key>
<string>113.xcdatamodel</string>
</dict>
</plist>
19 changes: 19 additions & 0 deletions Tests/Sync/Models/375.xcdatamodeld/113.xcdatamodel/contents
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="11759" systemVersion="16D32" minimumToolsVersion="Xcode 7.3" sourceLanguage="Objective-C" userDefinedModelVersionIdentifier="">
<entity name="Serie" syncable="YES">
<attribute name="id" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="NO" syncable="YES"/>
<relationship name="speeches" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Speech" inverseName="series" inverseEntity="Speech" syncable="YES"/>
</entity>
<entity name="Speech" syncable="YES">
<attribute name="id" optional="YES" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="NO" syncable="YES"/>
<relationship name="series" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="Serie" inverseName="speeches" inverseEntity="Serie" syncable="YES">
<userInfo>
<entry key="hyper.remoteKey" value="custom.series"/>
</userInfo>
</relationship>
</entity>
<elements>
<element name="Speech" positionX="-63" positionY="-18" width="128" height="73"/>
<element name="Serie" positionX="135" positionY="-18" width="128" height="73"/>
</elements>
</model>
11 changes: 11 additions & 0 deletions Tests/Sync/SyncTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1410,4 +1410,15 @@ class SyncTests: XCTestCase {

dataStack.drop()
}

// https://github.com/SyncDB/Sync/issues/375
func test375() {
let speeches = Helper.objectsFromJSON("375.json") as! [[String: Any]]
let dataStack = Helper.dataStackWithModelName("375")
dataStack.sync(speeches, inEntityNamed: "Speech", completion: nil)
XCTAssertEqual(Helper.countForEntity("Speech", inContext: dataStack.mainContext), 1)
XCTAssertEqual(Helper.countForEntity("Serie", inContext: dataStack.mainContext), 1)

dataStack.drop()
}
}