mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-04-28 03:37:10 +02:00
Resolve source entity and managed object attributes by name
Addresses #300 – see it for details. Also improves the use of whitespace consistency across the `CustomSchemaMappingProvider.swift` and uses method names for `cs_resolve…` over `cs_resolved…` for better alignment with [Swift API Design Guidelines](https://swift.org/documentation/api-design-guidelines).
This commit is contained in:
@@ -121,7 +121,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
|
|||||||
|
|
||||||
// MARK: Equatable
|
// MARK: Equatable
|
||||||
|
|
||||||
public static func == (lhs: CustomMapping, rhs: CustomMapping) -> Bool {
|
public static func ==(lhs: CustomMapping, rhs: CustomMapping) -> Bool {
|
||||||
|
|
||||||
switch (lhs, rhs) {
|
switch (lhs, rhs) {
|
||||||
|
|
||||||
@@ -324,7 +324,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
|
|||||||
|
|
||||||
// MARK: Equatable
|
// MARK: Equatable
|
||||||
|
|
||||||
public static func == (lhs: CustomSchemaMappingProvider, rhs: CustomSchemaMappingProvider) -> Bool {
|
public static func ==(lhs: CustomSchemaMappingProvider, rhs: CustomSchemaMappingProvider) -> Bool {
|
||||||
|
|
||||||
return lhs.sourceVersion == rhs.sourceVersion
|
return lhs.sourceVersion == rhs.sourceVersion
|
||||||
&& lhs.destinationVersion == rhs.destinationVersion
|
&& lhs.destinationVersion == rhs.destinationVersion
|
||||||
@@ -422,8 +422,8 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
|
|||||||
entityMapping.sourceExpression = expression(forSource: sourceEntity)
|
entityMapping.sourceExpression = expression(forSource: sourceEntity)
|
||||||
entityMapping.attributeMappings = autoreleasepool { () -> [NSPropertyMapping] in
|
entityMapping.attributeMappings = autoreleasepool { () -> [NSPropertyMapping] in
|
||||||
|
|
||||||
let sourceAttributes = sourceEntity.cs_resolvedAttributeRenamingIdentities()
|
let sourceAttributes = sourceEntity.cs_resolveAttributeNames()
|
||||||
let destinationAttributes = destinationEntity.cs_resolvedAttributeRenamingIdentities()
|
let destinationAttributes = destinationEntity.cs_resolveAttributeRenamingIdentities()
|
||||||
|
|
||||||
var attributeMappings: [NSPropertyMapping] = []
|
var attributeMappings: [NSPropertyMapping] = []
|
||||||
for (renamingIdentifier, destination) in destinationAttributes {
|
for (renamingIdentifier, destination) in destinationAttributes {
|
||||||
@@ -439,8 +439,8 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
|
|||||||
}
|
}
|
||||||
entityMapping.relationshipMappings = autoreleasepool { () -> [NSPropertyMapping] in
|
entityMapping.relationshipMappings = autoreleasepool { () -> [NSPropertyMapping] in
|
||||||
|
|
||||||
let sourceRelationships = sourceEntity.cs_resolvedRelationshipRenamingIdentities()
|
let sourceRelationships = sourceEntity.cs_resolveRelationshipNames()
|
||||||
let destinationRelationships = destinationEntity.cs_resolvedRelationshipRenamingIdentities()
|
let destinationRelationships = destinationEntity.cs_resolveRelationshipRenamingIdentities()
|
||||||
var relationshipMappings: [NSPropertyMapping] = []
|
var relationshipMappings: [NSPropertyMapping] = []
|
||||||
for (renamingIdentifier, destination) in destinationRelationships {
|
for (renamingIdentifier, destination) in destinationRelationships {
|
||||||
|
|
||||||
@@ -476,8 +476,8 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
|
|||||||
]
|
]
|
||||||
autoreleasepool {
|
autoreleasepool {
|
||||||
|
|
||||||
let sourceAttributes = sourceEntity.cs_resolvedAttributeRenamingIdentities()
|
let sourceAttributes = sourceEntity.cs_resolveAttributeNames()
|
||||||
let destinationAttributes = destinationEntity.cs_resolvedAttributeRenamingIdentities()
|
let destinationAttributes = destinationEntity.cs_resolveAttributeRenamingIdentities()
|
||||||
|
|
||||||
let transformedRenamingIdentifiers = Set(destinationAttributes.keys)
|
let transformedRenamingIdentifiers = Set(destinationAttributes.keys)
|
||||||
.intersection(sourceAttributes.keys)
|
.intersection(sourceAttributes.keys)
|
||||||
@@ -493,8 +493,8 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
|
|||||||
}
|
}
|
||||||
entityMapping.relationshipMappings = autoreleasepool { () -> [NSPropertyMapping] in
|
entityMapping.relationshipMappings = autoreleasepool { () -> [NSPropertyMapping] in
|
||||||
|
|
||||||
let sourceRelationships = sourceEntity.cs_resolvedRelationshipRenamingIdentities()
|
let sourceRelationships = sourceEntity.cs_resolveRelationshipNames()
|
||||||
let destinationRelationships = destinationEntity.cs_resolvedRelationshipRenamingIdentities()
|
let destinationRelationships = destinationEntity.cs_resolveRelationshipRenamingIdentities()
|
||||||
let transformedRenamingIdentifiers = Set(destinationRelationships.keys)
|
let transformedRenamingIdentifiers = Set(destinationRelationships.keys)
|
||||||
.intersection(sourceRelationships.keys)
|
.intersection(sourceRelationships.keys)
|
||||||
|
|
||||||
@@ -593,9 +593,9 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
|
|||||||
var allMappedSourceKeys: [KeyPathString: KeyPathString] = [:]
|
var allMappedSourceKeys: [KeyPathString: KeyPathString] = [:]
|
||||||
var allMappedDestinationKeys: [KeyPathString: KeyPathString] = [:]
|
var allMappedDestinationKeys: [KeyPathString: KeyPathString] = [:]
|
||||||
|
|
||||||
let sourceRenamingIdentifiers = sourceModel.cs_resolvedRenamingIdentities()
|
let sourceRenamingIdentifiers = sourceModel.cs_resolveNames()
|
||||||
let sourceEntityNames = sourceModel.entitiesByName
|
let sourceEntityNames = sourceModel.entitiesByName
|
||||||
let destinationRenamingIdentifiers = destinationModel.cs_resolvedRenamingIdentities()
|
let destinationRenamingIdentifiers = destinationModel.cs_resolveRenamingIdentities()
|
||||||
let destinationEntityNames = destinationModel.entitiesByName
|
let destinationEntityNames = destinationModel.entitiesByName
|
||||||
|
|
||||||
let removedRenamingIdentifiers = Set(sourceRenamingIdentifiers.keys)
|
let removedRenamingIdentifiers = Set(sourceRenamingIdentifiers.keys)
|
||||||
@@ -699,8 +699,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
|
|||||||
destinationEntity: destinationEntityName
|
destinationEntity: destinationEntityName
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
|
|
||||||
transformMappings.insert(
|
transformMappings.insert(
|
||||||
.transformEntity(
|
.transformEntity(
|
||||||
|
|||||||
@@ -32,24 +32,30 @@ import Foundation
|
|||||||
internal extension NSEntityDescription {
|
internal extension NSEntityDescription {
|
||||||
|
|
||||||
@nonobjc
|
@nonobjc
|
||||||
internal func cs_resolvedAttributeRenamingIdentities() -> [String: (attribute: NSAttributeDescription, versionHash: Data)] {
|
internal func cs_resolveAttributeNames() -> [String: (attribute: NSAttributeDescription, versionHash: Data)] {
|
||||||
|
return self.attributesByName.reduce(into: [:], { (result, attribute: (name: String, description: NSAttributeDescription)) in
|
||||||
var mapping: [String: (attribute: NSAttributeDescription, versionHash: Data)] = [:]
|
result[attribute.name] = (attribute.description, attribute.description.versionHash)
|
||||||
for (attributeName, attributeDescription) in self.attributesByName {
|
})
|
||||||
|
|
||||||
mapping[attributeDescription.renamingIdentifier ?? attributeName] = (attributeDescription, attributeDescription.versionHash)
|
|
||||||
}
|
|
||||||
return mapping
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@nonobjc
|
@nonobjc
|
||||||
internal func cs_resolvedRelationshipRenamingIdentities() -> [String: (relationship: NSRelationshipDescription, versionHash: Data)] {
|
internal func cs_resolveAttributeRenamingIdentities() -> [String: (attribute: NSAttributeDescription, versionHash: Data)] {
|
||||||
|
return self.attributesByName.reduce(into: [:], { (result, attribute: (name: String, description: NSAttributeDescription)) in
|
||||||
|
result[attribute.description.renamingIdentifier ?? attribute.name] = (attribute.description, attribute.description.versionHash)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
var mapping: [String: (relationship: NSRelationshipDescription, versionHash: Data)] = [:]
|
@nonobjc
|
||||||
for (relationshipName, relationshipDescription) in self.relationshipsByName {
|
internal func cs_resolveRelationshipNames() -> [String: (relationship: NSRelationshipDescription, versionHash: Data)] {
|
||||||
|
return self.relationshipsByName.reduce(into: [:], { (result, relationship: (name: String, description: NSRelationshipDescription)) in
|
||||||
|
result[relationship.name] = (relationship.description, relationship.description.versionHash)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
mapping[relationshipDescription.renamingIdentifier ?? relationshipName] = (relationshipDescription, relationshipDescription.versionHash)
|
@nonobjc
|
||||||
}
|
internal func cs_resolveRelationshipRenamingIdentities() -> [String: (relationship: NSRelationshipDescription, versionHash: Data)] {
|
||||||
return mapping
|
return self.relationshipsByName.reduce(into: [:], { (result, relationship: (name: String, description: NSRelationshipDescription)) in
|
||||||
|
result[relationship.description.renamingIdentifier ?? relationship.name] = (relationship.description, relationship.description.versionHash)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,13 +32,16 @@ import Foundation
|
|||||||
internal extension NSManagedObjectModel {
|
internal extension NSManagedObjectModel {
|
||||||
|
|
||||||
@nonobjc
|
@nonobjc
|
||||||
internal func cs_resolvedRenamingIdentities() -> [String: (entity: NSEntityDescription, versionHash: Data)] {
|
internal func cs_resolveNames() -> [String: (entity: NSEntityDescription, versionHash: Data)] {
|
||||||
|
return self.entitiesByName.reduce(into: [:], { (result, entity: (name: String, description: NSEntityDescription)) in
|
||||||
|
result[entity.name] = (entity.description, entity.description.versionHash)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
var mapping: [String: (entity: NSEntityDescription, versionHash: Data)] = [:]
|
@nonobjc
|
||||||
for (entityName, entityDescription) in self.entitiesByName {
|
internal func cs_resolveRenamingIdentities() -> [String: (entity: NSEntityDescription, versionHash: Data)] {
|
||||||
|
return self.entitiesByName.reduce(into: [:], { (result, entity: (name: String, description: NSEntityDescription)) in
|
||||||
mapping[entityDescription.renamingIdentifier ?? entityName] = (entityDescription, entityDescription.versionHash)
|
result[entity.description.renamingIdentifier ?? entity.name] = (entity.description, entity.description.versionHash)
|
||||||
}
|
})
|
||||||
return mapping
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user