Skip to content

Commit 62e962e

Browse files
authored
Merge pull request #301 from ianbytchek/develop
Resolve source entity and managed object attributes by name
2 parents 42d1f41 + 46ab70b commit 62e962e

4 files changed

Lines changed: 93 additions & 85 deletions

File tree

.travis.yml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: objective-c
2-
osx_image: xcode9
2+
osx_image: xcode10
33
sudo: false
44
git:
55
submodules: false
@@ -10,17 +10,17 @@ env:
1010
- LC_CTYPE=en_US.UTF-8
1111
- LANG=en_US.UTF-8
1212
matrix:
13-
- DESTINATION="OS=11.0,name=iPhone 8" SCHEME="CoreStore iOS" SDK=iphonesimulator11.0 RUN_TESTS="YES" POD_LINT="NO"
14-
- DESTINATION="OS=10.3,name=iPhone 7" SCHEME="CoreStore iOS" SDK=iphonesimulator11.0 RUN_TESTS="YES" POD_LINT="NO"
15-
- DESTINATION="OS=10.1,name=iPhone 7" SCHEME="CoreStore iOS" SDK=iphonesimulator11.0 RUN_TESTS="YES" POD_LINT="NO"
16-
- DESTINATION="OS=9.0,name=iPhone 6 Plus" SCHEME="CoreStore iOS" SDK=iphonesimulator11.0 RUN_TESTS="YES" POD_LINT="NO"
17-
- DESTINATION="arch=x86_64" SCHEME="CoreStore OSX" SDK=macosx10.13 RUN_TESTS="YES" POD_LINT="NO"
18-
- DESTINATION="OS=4.0,name=Apple Watch - 42mm" SCHEME="CoreStore watchOS" SDK=watchsimulator4.0 RUN_TESTS="NO" POD_LINT="NO"
19-
- DESTINATION="OS=3.2,name=Apple Watch - 42mm" SCHEME="CoreStore watchOS" SDK=watchsimulator4.0 RUN_TESTS="NO" POD_LINT="NO"
20-
- DESTINATION="OS=2.2,name=Apple Watch - 42mm" SCHEME="CoreStore watchOS" SDK=watchsimulator4.0 RUN_TESTS="NO" POD_LINT="NO"
21-
- DESTINATION="OS=11.0,name=Apple TV 1080p" SCHEME="CoreStore tvOS" SDK=appletvsimulator11.0 RUN_TESTS="YES" POD_LINT="NO"
22-
- DESTINATION="OS=10.2,name=Apple TV 1080p" SCHEME="CoreStore tvOS" SDK=appletvsimulator11.0 RUN_TESTS="YES" POD_LINT="NO"
23-
- DESTINATION="OS=9.2,name=Apple TV 1080p" SCHEME="CoreStore tvOS" SDK=appletvsimulator11.0 RUN_TESTS="YES" POD_LINT="NO"
13+
- DESTINATION="arch=x86_64" SCHEME="CoreStore OSX" SDK=macosx10.14 RUN_TESTS="YES" POD_LINT="NO"
14+
- DESTINATION="OS=12.0,name=iPhone XS" SCHEME="CoreStore iOS" SDK=iphonesimulator12.0 RUN_TESTS="YES" POD_LINT="NO"
15+
- DESTINATION="OS=11.0.1,name=iPhone 8" SCHEME="CoreStore iOS" SDK=iphonesimulator12.0 RUN_TESTS="YES" POD_LINT="NO"
16+
- DESTINATION="OS=10.3.1,name=iPhone 7" SCHEME="CoreStore iOS" SDK=iphonesimulator12.0 RUN_TESTS="YES" POD_LINT="NO"
17+
- DESTINATION="OS=10.1,name=iPhone 7" SCHEME="CoreStore iOS" SDK=iphonesimulator12.0 RUN_TESTS="YES" POD_LINT="NO"
18+
- DESTINATION="OS=4.0,name=Apple Watch - 42mm" SCHEME="CoreStore watchOS" SDK=watchsimulator5.0 RUN_TESTS="NO" POD_LINT="NO"
19+
- DESTINATION="OS=3.2,name=Apple Watch - 42mm" SCHEME="CoreStore watchOS" SDK=watchsimulator5.0 RUN_TESTS="NO" POD_LINT="NO"
20+
- DESTINATION="OS=2.2,name=Apple Watch - 42mm" SCHEME="CoreStore watchOS" SDK=watchsimulator5.0 RUN_TESTS="NO" POD_LINT="NO"
21+
- DESTINATION="OS=12.0,name=Apple TV 4K" SCHEME="CoreStore tvOS" SDK=appletvsimulator12.0 RUN_TESTS="YES" POD_LINT="NO"
22+
- DESTINATION="OS=11.0,name=Apple TV 4K (at 1080p)" SCHEME="CoreStore tvOS" SDK=appletvsimulator12.0 RUN_TESTS="YES" POD_LINT="NO"
23+
- DESTINATION="OS=10.2,name=Apple TV 1080p" SCHEME="CoreStore tvOS" SDK=appletvsimulator12.0 RUN_TESTS="YES" POD_LINT="NO"
2424
before_install:
2525
- gem install cocoapods --no-rdoc --no-ri --no-document --quiet
2626
- gem install xcpretty --no-rdoc --no-ri --no-document --quiet
@@ -39,8 +39,8 @@ script:
3939
xcodebuild -workspace CoreStore.xcworkspace -scheme "$SCHEME" -sdk "$SDK" -destination "$DESTINATION" -configuration Debug ONLY_ACTIVE_ARCH=NO clean test | xcpretty -c;
4040
xcodebuild -workspace CoreStore.xcworkspace -scheme "$SCHEME" -sdk "$SDK" -destination "$DESTINATION" -configuration Release ONLY_ACTIVE_ARCH=NO clean test | xcpretty -c;
4141
fi
42-
- xcodebuild -workspace "CoreStore.xcworkspace" -scheme "CoreStoreDemo" -sdk "iphonesimulator11.0" -destination "OS=11.0,name=iPhone 8" -configuration Debug ONLY_ACTIVE_ARCH=NO build | xcpretty -c;
43-
- xcodebuild -workspace "CoreStore.xcworkspace" -scheme "CoreStoreDemo" -sdk "iphonesimulator11.0" -destination "OS=11.0,name=iPhone 8" -configuration Release ONLY_ACTIVE_ARCH=NO build | xcpretty -c;
42+
- xcodebuild -workspace "CoreStore.xcworkspace" -scheme "CoreStoreDemo" -sdk "iphonesimulator12.0" -destination "OS=11.0.1,name=iPhone 8" -configuration Debug ONLY_ACTIVE_ARCH=NO build | xcpretty -c;
43+
- xcodebuild -workspace "CoreStore.xcworkspace" -scheme "CoreStoreDemo" -sdk "iphonesimulator12.0" -destination "OS=11.0.1,name=iPhone 8" -configuration Release ONLY_ACTIVE_ARCH=NO build | xcpretty -c;
4444
- if [ $POD_LINT == "YES" ]; then
4545
pod lib lint --quick;
4646
fi

Sources/CustomSchemaMappingProvider.swift

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -121,49 +121,49 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
121121

122122
// MARK: Equatable
123123

124-
public static func == (lhs: CustomMapping, rhs: CustomMapping) -> Bool {
124+
public static func ==(lhs: CustomMapping, rhs: CustomMapping) -> Bool {
125125

126126
switch (lhs, rhs) {
127-
127+
128128
case (.deleteEntity(let sourceEntity1), .deleteEntity(let sourceEntity2)):
129129
return sourceEntity1 == sourceEntity2
130-
130+
131131
case (.insertEntity(let destinationEntity1), .insertEntity(let destinationEntity2)):
132132
return destinationEntity1 == destinationEntity2
133-
133+
134134
case (.copyEntity(let sourceEntity1, let destinationEntity1), .copyEntity(let sourceEntity2, let destinationEntity2)):
135135
return sourceEntity1 == sourceEntity2
136136
&& destinationEntity1 == destinationEntity2
137-
137+
138138
case (.transformEntity(let sourceEntity1, let destinationEntity1, _), .transformEntity(let sourceEntity2, let destinationEntity2, _)):
139139
return sourceEntity1 == sourceEntity2
140140
&& destinationEntity1 == destinationEntity2
141-
141+
142142
default:
143143
return false
144144
}
145145
}
146146

147147

148148
// MARK: Hashable
149-
149+
150150
public func hash(into hasher: inout Hasher) {
151151

152152
switch self {
153-
153+
154154
case .deleteEntity(let sourceEntity):
155155
hasher.combine(0)
156156
hasher.combine(sourceEntity)
157-
157+
158158
case .insertEntity(let destinationEntity):
159159
hasher.combine(1)
160160
hasher.combine(destinationEntity)
161-
161+
162162
case .copyEntity(let sourceEntity, let destinationEntity):
163163
hasher.combine(2)
164164
hasher.combine(sourceEntity)
165165
hasher.combine(destinationEntity)
166-
166+
167167
case .transformEntity(let sourceEntity, let destinationEntity, _):
168168
hasher.combine(3)
169169
hasher.combine(sourceEntity)
@@ -177,12 +177,12 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
177177
fileprivate var entityMappingSourceEntity: EntityName? {
178178

179179
switch self {
180-
180+
181181
case .deleteEntity(let sourceEntity),
182182
.copyEntity(let sourceEntity, _),
183183
.transformEntity(let sourceEntity, _, _):
184184
return sourceEntity
185-
185+
186186
case .insertEntity:
187187
return nil
188188
}
@@ -191,12 +191,12 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
191191
fileprivate var entityMappingDestinationEntity: EntityName? {
192192

193193
switch self {
194-
194+
195195
case .insertEntity(let destinationEntity),
196196
.copyEntity(_, let destinationEntity),
197197
.transformEntity(_, let destinationEntity, _):
198198
return destinationEntity
199-
199+
200200
case .deleteEntity:
201201
return nil
202202
}
@@ -324,7 +324,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
324324

325325
// MARK: Equatable
326326

327-
public static func == (lhs: CustomSchemaMappingProvider, rhs: CustomSchemaMappingProvider) -> Bool {
327+
public static func ==(lhs: CustomSchemaMappingProvider, rhs: CustomSchemaMappingProvider) -> Bool {
328328

329329
return lhs.sourceVersion == rhs.sourceVersion
330330
&& lhs.destinationVersion == rhs.destinationVersion
@@ -333,9 +333,9 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
333333

334334

335335
// MARK: Hashable
336-
336+
337337
public func hash(into hasher: inout Hasher) {
338-
338+
339339
hasher.combine(self.sourceVersion)
340340
hasher.combine(self.destinationVersion)
341341
hasher.combine(ObjectIdentifier(cs_dynamicType(of: self)))
@@ -422,8 +422,8 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
422422
entityMapping.sourceExpression = expression(forSource: sourceEntity)
423423
entityMapping.attributeMappings = autoreleasepool { () -> [NSPropertyMapping] in
424424

425-
let sourceAttributes = sourceEntity.cs_resolvedAttributeRenamingIdentities()
426-
let destinationAttributes = destinationEntity.cs_resolvedAttributeRenamingIdentities()
425+
let sourceAttributes = sourceEntity.cs_resolveAttributeNames()
426+
let destinationAttributes = destinationEntity.cs_resolveAttributeRenamingIdentities()
427427

428428
var attributeMappings: [NSPropertyMapping] = []
429429
for (renamingIdentifier, destination) in destinationAttributes {
@@ -439,15 +439,15 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
439439
}
440440
entityMapping.relationshipMappings = autoreleasepool { () -> [NSPropertyMapping] in
441441

442-
let sourceRelationships = sourceEntity.cs_resolvedRelationshipRenamingIdentities()
443-
let destinationRelationships = destinationEntity.cs_resolvedRelationshipRenamingIdentities()
442+
let sourceRelationships = sourceEntity.cs_resolveRelationshipNames()
443+
let destinationRelationships = destinationEntity.cs_resolveRelationshipRenamingIdentities()
444444
var relationshipMappings: [NSPropertyMapping] = []
445445
for (renamingIdentifier, destination) in destinationRelationships {
446446

447447
let sourceRelationship = sourceRelationships[renamingIdentifier]!.relationship
448448
let destinationRelationship = destination.relationship
449449
let sourceRelationshipName = sourceRelationship.name
450-
450+
451451
let propertyMapping = NSPropertyMapping()
452452
propertyMapping.name = destinationRelationship.name
453453
propertyMapping.valueExpression = NSExpression(format: "FUNCTION($\(NSMigrationManagerKey), \"destinationInstancesForSourceRelationshipNamed:sourceInstances:\", \"\(sourceRelationshipName)\", FUNCTION($\(NSMigrationSourceObjectKey), \"\(#selector(NSManagedObject.value(forKey:)))\", \"\(sourceRelationshipName)\"))")
@@ -476,12 +476,12 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
476476
]
477477
autoreleasepool {
478478

479-
let sourceAttributes = sourceEntity.cs_resolvedAttributeRenamingIdentities()
480-
let destinationAttributes = destinationEntity.cs_resolvedAttributeRenamingIdentities()
479+
let sourceAttributes = sourceEntity.cs_resolveAttributeNames()
480+
let destinationAttributes = destinationEntity.cs_resolveAttributeRenamingIdentities()
481481

482482
let transformedRenamingIdentifiers = Set(destinationAttributes.keys)
483483
.intersection(sourceAttributes.keys)
484-
484+
485485
var sourceAttributesByDestinationKey: [KeyPathString: NSAttributeDescription] = [:]
486486
for renamingIdentifier in transformedRenamingIdentifiers {
487487

@@ -493,19 +493,19 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
493493
}
494494
entityMapping.relationshipMappings = autoreleasepool { () -> [NSPropertyMapping] in
495495

496-
let sourceRelationships = sourceEntity.cs_resolvedRelationshipRenamingIdentities()
497-
let destinationRelationships = destinationEntity.cs_resolvedRelationshipRenamingIdentities()
496+
let sourceRelationships = sourceEntity.cs_resolveRelationshipNames()
497+
let destinationRelationships = destinationEntity.cs_resolveRelationshipRenamingIdentities()
498498
let transformedRenamingIdentifiers = Set(destinationRelationships.keys)
499499
.intersection(sourceRelationships.keys)
500-
500+
501501
var relationshipMappings: [NSPropertyMapping] = []
502502
for renamingIdentifier in transformedRenamingIdentifiers {
503-
503+
504504
let sourceRelationship = sourceRelationships[renamingIdentifier]!.relationship
505505
let destinationRelationship = destinationRelationships[renamingIdentifier]!.relationship
506506
let sourceRelationshipName = sourceRelationship.name
507507
let destinationRelationshipName = destinationRelationship.name
508-
508+
509509
let propertyMapping = NSPropertyMapping()
510510
propertyMapping.name = destinationRelationshipName
511511
propertyMapping.valueExpression = NSExpression(format: "FUNCTION($\(NSMigrationManagerKey), \"destinationInstancesForSourceRelationshipNamed:sourceInstances:\", \"\(sourceRelationshipName)\", FUNCTION($\(NSMigrationSourceObjectKey), \"\(#selector(NSManagedObject.value(forKey:)))\", \"\(sourceRelationshipName)\"))")
@@ -593,9 +593,9 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
593593
var allMappedSourceKeys: [KeyPathString: KeyPathString] = [:]
594594
var allMappedDestinationKeys: [KeyPathString: KeyPathString] = [:]
595595

596-
let sourceRenamingIdentifiers = sourceModel.cs_resolvedRenamingIdentities()
596+
let sourceRenamingIdentifiers = sourceModel.cs_resolveNames()
597597
let sourceEntityNames = sourceModel.entitiesByName
598-
let destinationRenamingIdentifiers = destinationModel.cs_resolvedRenamingIdentities()
598+
let destinationRenamingIdentifiers = destinationModel.cs_resolveRenamingIdentities()
599599
let destinationEntityNames = destinationModel.entitiesByName
600600

601601
let removedRenamingIdentifiers = Set(sourceRenamingIdentifiers.keys)
@@ -610,7 +610,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
610610
for mapping in self.entityMappings {
611611

612612
switch mapping {
613-
613+
614614
case .deleteEntity(let sourceEntity):
615615
CoreStore.assert(
616616
sourceEntityNames[sourceEntity] != nil,
@@ -622,7 +622,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
622622
)
623623
deleteMappings.insert(mapping)
624624
allMappedSourceKeys[sourceEntity] = ""
625-
625+
626626
case .insertEntity(let destinationEntity):
627627
CoreStore.assert(
628628
destinationEntityNames[destinationEntity] != nil,
@@ -634,7 +634,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
634634
)
635635
insertMappings.insert(mapping)
636636
allMappedDestinationKeys[destinationEntity] = ""
637-
637+
638638
case .transformEntity(let sourceEntity, let destinationEntity, _):
639639
CoreStore.assert(
640640
sourceEntityNames[sourceEntity] != nil,
@@ -655,7 +655,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
655655
transformMappings.insert(mapping)
656656
allMappedSourceKeys[sourceEntity] = destinationEntity
657657
allMappedDestinationKeys[destinationEntity] = sourceEntity
658-
658+
659659
case .copyEntity(let sourceEntity, let destinationEntity):
660660
CoreStore.assert(
661661
sourceEntityNames[sourceEntity] != nil,
@@ -689,7 +689,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
689689
let sourceEntityName = sourceEntity.name!
690690
let destinationEntityName = destinationEntity.name!
691691
switch (allMappedSourceKeys[sourceEntityName], allMappedDestinationKeys[destinationEntityName]) {
692-
692+
693693
case (nil, nil):
694694
if sourceEntity.versionHash == destinationEntity.versionHash {
695695

@@ -699,8 +699,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
699699
destinationEntity: destinationEntityName
700700
)
701701
)
702-
}
703-
else {
702+
} else {
704703

705704
transformMappings.insert(
706705
.transformEntity(
@@ -712,15 +711,15 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
712711
}
713712
allMappedSourceKeys[sourceEntityName] = destinationEntityName
714713
allMappedDestinationKeys[destinationEntityName] = sourceEntityName
715-
714+
716715
case (""?, nil):
717716
insertMappings.insert(.insertEntity(destinationEntity: destinationEntityName))
718717
allMappedDestinationKeys[destinationEntityName] = ""
719-
718+
720719
case (nil, ""?):
721720
deleteMappings.insert(.deleteEntity(sourceEntity: sourceEntityName))
722721
allMappedSourceKeys[sourceEntityName] = ""
723-
722+
724723
default:
725724
continue
726725
}
@@ -730,11 +729,11 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
730729
let sourceEntity = sourceRenamingIdentifiers[renamingIdentifier]!.entity
731730
let sourceEntityName = sourceEntity.name!
732731
switch allMappedSourceKeys[sourceEntityName] {
733-
732+
734733
case nil:
735734
deleteMappings.insert(.deleteEntity(sourceEntity: sourceEntityName))
736735
allMappedSourceKeys[sourceEntityName] = ""
737-
736+
738737
default:
739738
continue
740739
}
@@ -744,11 +743,11 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
744743
let destinationEntity = destinationRenamingIdentifiers[renamingIdentifier]!.entity
745744
let destinationEntityName = destinationEntity.name!
746745
switch allMappedDestinationKeys[destinationEntityName] {
747-
746+
748747
case nil:
749748
insertMappings.insert(.insertEntity(destinationEntity: destinationEntityName))
750749
allMappedDestinationKeys[destinationEntityName] = ""
751-
750+
752751
default:
753752
continue
754753
}

0 commit comments

Comments
 (0)