From 8ad436cd75f1bf497391c1ee2d9a2f28c0325cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Helge=20He=C3=9F?= Date: Sat, 7 Oct 2023 13:15:56 +0200 Subject: [PATCH] Drop generation of _$entity No caching of that. --- .../ModelMacro/GenerateInitializers.swift | 5 ++--- .../ModelMacro/ModelMembers.swift | 22 ------------------- .../ManagedModels/ModelMacroDefinition.swift | 2 -- .../PersistentModel/PersistentModel+KVC.swift | 4 ---- .../PersistentModel/PersistentModel.swift | 15 +------------ Tests/ManagedModelTests/BasicModelTests.swift | 1 - .../CoreDataAssumptionsTests.swift | 9 ++++---- .../Schemas/ExpandedPersonAddressSchema.swift | 6 ++--- .../PersonAddressSchemaNoInverse.swift | 2 +- 9 files changed, 11 insertions(+), 55 deletions(-) diff --git a/Sources/ManagedModelMacros/ModelMacro/GenerateInitializers.swift b/Sources/ManagedModelMacros/ModelMacro/GenerateInitializers.swift index db4ced2..952a0f8 100644 --- a/Sources/ManagedModelMacros/ModelMacro/GenerateInitializers.swift +++ b/Sources/ManagedModelMacros/ModelMacro/GenerateInitializers.swift @@ -46,7 +46,6 @@ extension ModelMacro { message: "Use `init(context:)` or `init()` instead.") \(raw: access)override init(entity: CoreData.NSEntityDescription, insertInto context: NSManagedObjectContext?) { - assert(entity === Self._$entity, "Attempt to initialize PersistentModel w/ different entity?") super.init(entity: entity, insertInto: context) } """ @@ -67,7 +66,7 @@ extension ModelMacro { /// - Parameters: // - context: An `NSManagedObjectContext` the object should be inserted into. \(raw: access)init(context: CoreData.NSManagedObjectContext?) { - super.init(entity: Self._$entity, insertInto: context) + super.init(entity: Self.entity(), insertInto: context) } """ ) @@ -78,7 +77,7 @@ extension ModelMacro { /// Initialize a `\(modelClassName)` object w/o inserting it into a /// context. \(raw: access)init() { - super.init(entity: Self._$entity, insertInto: nil) + super.init(entity: Self.entity(), insertInto: nil) } """ ) diff --git a/Sources/ManagedModelMacros/ModelMacro/ModelMembers.swift b/Sources/ManagedModelMacros/ModelMacro/ModelMembers.swift index cf8ea79..3eba04e 100644 --- a/Sources/ManagedModelMacros/ModelMacro/ModelMembers.swift +++ b/Sources/ManagedModelMacros/ModelMacro/ModelMembers.swift @@ -14,8 +14,6 @@ import SwiftDiagnostics * @attached(member, names: // Those are the names we add * named(init), // Initializers. * named(schemaMetadata), // The metadata. - * named(entity), // Override the `entity()` function. - * named(_$entity), // The cached the Entity * named(_$originalName), * named(_$hashModifier) * ) @@ -55,17 +53,6 @@ extension ModelMacro: MemberMacro { // @attached(member, names:...) ) newMembers.append(DeclSyntax(metadata)) - if classDecl.findFunctionWithName("entity", isStaticOrClass: true, - parameterCount: 0) == nil - { - newMembers.append( - """ - /// Returns the `NSEntityDescription` associated w/ the `PersistentModel`. - \(raw: access)override class func entity() -> NSEntityDescription { _$entity } - """ - ) - } - // TODO: Lookup `originalName` parameter in `macroNode` newMembers.append( """ @@ -77,15 +64,6 @@ extension ModelMacro: MemberMacro { // @attached(member, names:...) \(raw: access)static let _$hashModifier : String? = nil """ ) - - newMembers.append( - """ - /// The shared `NSEntityDescription` for the `PersistentModel`. - /// Never modify the referred object! - \(raw: access)static let _$entity = - ManagedModels.SchemaBuilder.shared._entity(for: \(modelClassName).self) - """ - ) return newMembers } diff --git a/Sources/ManagedModels/ModelMacroDefinition.swift b/Sources/ManagedModels/ModelMacroDefinition.swift index 09c4d4b..4a762f9 100644 --- a/Sources/ManagedModels/ModelMacroDefinition.swift +++ b/Sources/ManagedModels/ModelMacroDefinition.swift @@ -81,8 +81,6 @@ public macro _PersistedProperty() = @attached(member, names: // Those are the names we add named(init), // Initializers. named(schemaMetadata), // The metadata. - named(entity), // Override the `entity()` function. - named(_$entity), // The cached the Entity named(_$originalName), named(_$hashModifier) ) diff --git a/Sources/ManagedModels/PersistentModel/PersistentModel+KVC.swift b/Sources/ManagedModels/PersistentModel/PersistentModel+KVC.swift index 0ae9630..4bb2bed 100644 --- a/Sources/ManagedModels/PersistentModel/PersistentModel+KVC.swift +++ b/Sources/ManagedModels/PersistentModel/PersistentModel+KVC.swift @@ -120,10 +120,6 @@ public extension PersistentModel { func _setOptionalToOneValue(forKey key: String, to model: T?) where T: PersistentModel { - #if DEBUG - let relship = Self._$entity.relationshipsByName[key]! - assert(!relship.isToMany, "relship: \(relship)") - #endif if let model { if model.modelContext != self.modelContext { if let otherCtx = model.modelContext, self.modelContext == nil { diff --git a/Sources/ManagedModels/PersistentModel/PersistentModel.swift b/Sources/ManagedModels/PersistentModel/PersistentModel.swift index cafa6ec..9360666 100644 --- a/Sources/ManagedModels/PersistentModel/PersistentModel.swift +++ b/Sources/ManagedModels/PersistentModel/PersistentModel.swift @@ -19,16 +19,6 @@ public protocol PersistentModel: NSManagedObject, Hashable, Identifiable { */ static var schemaMetadata : [ NSManagedObjectModel.PropertyMetadata ] { get } - /** - * Reflection data for the model. - * - * This is considered private, use a Schema to access entities, and NEVER - * modify the schema objects after they got setup. - * - * API DIFF: SwiftData doesn't have that, always builds dynamically. - */ - static var _$entity : NSEntityDescription { get } - // Why have that? Cheap cache. /// The `renamingIdentifier` of the model. static var _$originalName : String? { get } @@ -55,16 +45,13 @@ extension PersistentModel { public static var schemaMetadata : [ NSManagedObjectModel.PropertyMetadata ] { fatalError("Subclass needs to implement `schemaMetadata`") } - - @inlinable - public static var _$entity : NSEntityDescription { self.entity() } } public extension PersistentModel { @inlinable static func fetchRequest() -> NSFetchRequest { - NSFetchRequest(entityName: _$entity.name ?? NSStringFromClass(self)) + NSFetchRequest(entityName: _typeName(Self.self, qualified: false)) } @inlinable diff --git a/Tests/ManagedModelTests/BasicModelTests.swift b/Tests/ManagedModelTests/BasicModelTests.swift index 90f3fb0..ee8c382 100644 --- a/Tests/ManagedModelTests/BasicModelTests.swift +++ b/Tests/ManagedModelTests/BasicModelTests.swift @@ -16,7 +16,6 @@ final class BasicModelTests: XCTestCase { func testEntityName() throws { let addressType = Fixtures.PersonAddressSchema.Address.self - XCTAssertEqual(addressType._$entity.name, "Address") XCTAssertEqual(addressType.entity().name, "Address") } diff --git a/Tests/ManagedModelTests/CoreDataAssumptionsTests.swift b/Tests/ManagedModelTests/CoreDataAssumptionsTests.swift index f1783f6..3da87a0 100644 --- a/Tests/ManagedModelTests/CoreDataAssumptionsTests.swift +++ b/Tests/ManagedModelTests/CoreDataAssumptionsTests.swift @@ -20,9 +20,9 @@ final class CoreDataAssumptionsTests: XCTestCase { let relationship = NSRelationshipDescription() relationship.name = "addresses" //relationship.destinationEntity = - // Fixtures.PersonAddressSchema.Address._$entity + // Fixtures.PersonAddressSchema.Address.entity() //relationship.inverseRelationship = - // Fixtures.PersonAddressSchema.Address._$entity.relationshipsByName["person"] + // Fixtures.PersonAddressSchema.Address.entity().relationshipsByName["person"] // This just seems to be the default. XCTAssertTrue(relationship.isToMany) @@ -32,9 +32,10 @@ final class CoreDataAssumptionsTests: XCTestCase { let relationship = NSRelationshipDescription() relationship.name = "person" relationship.maxCount = 1 // toOne marker! + #if false // old relationship.destinationEntity = - Fixtures.PersonAddressSchema.Person._$entity - // Yes! This does not work at this point. + Fixtures.PersonAddressSchema.Person.entity() + #endif XCTAssertFalse(relationship.isToMany) } diff --git a/Tests/ManagedModelTests/Schemas/ExpandedPersonAddressSchema.swift b/Tests/ManagedModelTests/Schemas/ExpandedPersonAddressSchema.swift index d3581f6..a6c442d 100644 --- a/Tests/ManagedModelTests/Schemas/ExpandedPersonAddressSchema.swift +++ b/Tests/ManagedModelTests/Schemas/ExpandedPersonAddressSchema.swift @@ -31,7 +31,7 @@ extension Fixtures { init(firstname: String, lastname: String, addresses: [ Address ]) { // Note: Could do Self.entity! - super.init(entity: Self._$entity, insertInto: nil) + super.init(entity: Self.entity(), insertInto: nil) self.firstname = firstname self.lastname = lastname self.addresses = addresses @@ -42,7 +42,6 @@ extension Fixtures { .init(name: "lastname" , keypath: \Person.lastname), .init(name: "addresses" , keypath: \Person.addresses) ] - public static let _$entity = SchemaBuilder.shared._entity(for: Person.self) public static let _$originalName : String? = nil public static let _$hashModifier : String? = nil } @@ -54,7 +53,7 @@ extension Fixtures { @NSManaged var person : Person init(street: String, appartment: String? = nil, person: Person) { - super.init(entity: Self._$entity, insertInto: nil) + super.init(entity: Self.entity(), insertInto: nil) self.street = street self.appartment = appartment self.person = person @@ -65,7 +64,6 @@ extension Fixtures { .init(name: "appartment" , keypath: \Address.appartment), .init(name: "person" , keypath: \Address.person) ] - public static let _$entity = SchemaBuilder.shared._entity(for: Address.self) public static let _$originalName : String? = nil public static let _$hashModifier : String? = nil } diff --git a/Tests/ManagedModelTests/Schemas/PersonAddressSchemaNoInverse.swift b/Tests/ManagedModelTests/Schemas/PersonAddressSchemaNoInverse.swift index efff698..07d9e7b 100644 --- a/Tests/ManagedModelTests/Schemas/PersonAddressSchemaNoInverse.swift +++ b/Tests/ManagedModelTests/Schemas/PersonAddressSchemaNoInverse.swift @@ -24,7 +24,7 @@ extension Fixtures { var addresses : [ Address ] init(firstname: String, lastname: String, addresses: [ Address ]) { - super.init(entity: Self._$entity, insertInto: nil) + super.init(entity: Self.entity(), insertInto: nil) self.firstname = firstname self.lastname = lastname self.addresses = addresses