Skip to content

Commit

Permalink
Merge branch 'feature/no-static-entity-1' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
helje5 committed Oct 7, 2023
2 parents 921d3e7 + 28b65a0 commit 57e9a01
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ extension ModelMacro {
/// - Parameters:
// - entity: An `NSEntityDescription` describing the object.
// - context: An `NSManagedObjectContext` the object should be inserted into.
@available(*, deprecated, renamed: "init(context:)",
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)
}
"""
Expand All @@ -67,7 +64,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)
}
"""
)
Expand All @@ -78,7 +75,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)
}
"""
)
Expand Down
22 changes: 0 additions & 22 deletions Sources/ManagedModelMacros/ModelMacro/ModelMembers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
* )
Expand Down Expand Up @@ -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(
"""
Expand All @@ -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
}
Expand Down
2 changes: 0 additions & 2 deletions Sources/ManagedModels/ModelMacroDefinition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ public extension PersistentModel {
func _setOptionalToOneValue<T>(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 {
Expand Down
15 changes: 1 addition & 14 deletions Sources/ManagedModels/PersistentModel/PersistentModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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<Self> {
NSFetchRequest<Self>(entityName: _$entity.name ?? NSStringFromClass(self))
NSFetchRequest<Self>(entityName: _typeName(Self.self, qualified: false))
}

@inlinable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ public extension NSManagedObjectModel {
// - encodingVersion
// - version

@available(*, deprecated, renamed: "model(for:)", message:
"""
Entities can only be used in one NSManagedObjectModel, use the `model(for:)`
static function to get access to s ahred, cached model.
"""
)
@inlinable
convenience init(_ entities: NSEntityDescription...,
version: Schema.Version = Version(1, 0, 0))
Expand All @@ -25,25 +19,13 @@ public extension NSManagedObjectModel {
self.entities = entities
}

@available(*, deprecated, renamed: "model(for:)", message:
"""
Entities can only be used in one NSManagedObjectModel, use the `model(for:)`
static function to get access to s ahred, cached model.
"""
)
convenience init(_ types: [ any PersistentModel.Type ],
version: Schema.Version = Version(1, 0, 0))
{
self.init()
self.entities = SchemaBuilder.shared.lookupAllEntities(for: types)
}

@available(*, deprecated, renamed: "model(for:)", message:
"""
Entities can only be used in one NSManagedObjectModel, use the `model(for:)`
static function to get access to s ahred, cached model.
"""
)
@inlinable
convenience init(versionedSchema: any VersionedSchema.Type) {
self.init(versionedSchema.models,
Expand All @@ -59,6 +41,7 @@ private var map = [ Set<ObjectIdentifier> : NSManagedObjectModel ]()

public extension NSManagedObjectModel {

/// A cached version of the initializer.
static func model(for versionedSchema: VersionedSchema.Type)
-> NSManagedObjectModel
{
Expand Down
1 change: 0 additions & 1 deletion Tests/ManagedModelTests/BasicModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
9 changes: 5 additions & 4 deletions Tests/ManagedModelTests/CoreDataAssumptionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 57e9a01

Please sign in to comment.