From 6d0480660847471034d209deae899bf5add61d62 Mon Sep 17 00:00:00 2001 From: John Rommel Estropia Date: Wed, 26 Apr 2017 08:31:37 +0900 Subject: [PATCH] WIP --- .../MigrationMappingProvider.swift | 73 +++++++++++++------ 1 file changed, 50 insertions(+), 23 deletions(-) diff --git a/Sources/Migrating/Migration Mapping Providers/MigrationMappingProvider.swift b/Sources/Migrating/Migration Mapping Providers/MigrationMappingProvider.swift index fd6b271..cb84183 100644 --- a/Sources/Migrating/Migration Mapping Providers/MigrationMappingProvider.swift +++ b/Sources/Migrating/Migration Mapping Providers/MigrationMappingProvider.swift @@ -37,6 +37,9 @@ public protocol SchemaMappingProvider { func createMappingModel() throws -> (mappingModel: NSMappingModel, migrationType: MigrationType) } + +// MARK: - EntityMappingProvider + public protocol EntityMappingProvider { var source: (schema: DynamicSchema, entity: DynamicEntity) { get } @@ -46,33 +49,68 @@ public protocol EntityMappingProvider { } -// MARK: - XcodeMappingModelProvider +// MARK: - LightweightMappingModelProvider -open class XcodeMappingModelProvider: SchemaMappingProvider { +open class LightweightMappingModelProvider: SchemaMappingProvider { - private let mappingModelBundles: [Bundle] - - public required init(source: SourceSchema, destination: DestinationSchema, mappingModelBundles: [Bundle] = Bundle.allBundles) { + public required init(source: SourceSchema, destination: DestinationSchema) { self.sourceSchema = source self.destinationSchema = destination - self.mappingModelBundles = mappingModelBundles } // MARK: SchemaMappingProvider - public typealias SourceSchema = S - public typealias DestinationSchema = D - - public let sourceSchema: SourceSchema - public let destinationSchema: DestinationSchema + public let sourceSchema: DynamicSchema + public let destinationSchema: DynamicSchema public func createMappingModel() throws -> (mappingModel: NSMappingModel, migrationType: MigrationType) { let sourceModel = self.sourceSchema.rawModel() let destinationModel = self.destinationSchema.rawModel() + let mappingModel = try NSMappingModel.inferredMappingModel( + forSourceModel: sourceModel, + destinationModel: destinationModel + ) + return ( + mappingModel, + .lightweight( + sourceVersion: self.sourceSchema.modelVersion, + destinationVersion: self.destinationSchema.modelVersion + ) + ) + } +} + + +// MARK: - XcodeMappingModelProvider + +open class XcodeMappingModelProvider: LightweightMappingModelProvider { + + private let mappingModelBundles: [Bundle] + + public required init(source: SourceSchema, destination: DestinationSchema, mappingModelBundles: [Bundle]) { + + self.mappingModelBundles = mappingModelBundles + super.init(source: source, destination: destination) + } + + public required init(source: SourceSchema, destination: DestinationSchema) { + + self.mappingModelBundles = Bundle.allBundles + super.init(source: source, destination: destination) + } + + + // MARK: SchemaMappingProvider + + public override func createMappingModel() throws -> (mappingModel: NSMappingModel, migrationType: MigrationType) { + + let sourceModel = self.sourceSchema.rawModel() + let destinationModel = self.destinationSchema.rawModel() + if let mappingModel = NSMappingModel( from: self.mappingModelBundles, forSourceModel: sourceModel, @@ -86,18 +124,7 @@ open class XcodeMappingModelProvider: Schema ) ) } - - let mappingModel = try NSMappingModel.inferredMappingModel( - forSourceModel: sourceModel, - destinationModel: destinationModel - ) - return ( - mappingModel, - .lightweight( - sourceVersion: self.sourceSchema.modelVersion, - destinationVersion: self.destinationSchema.modelVersion - ) - ) + return try super.createMappingModel() } }