Skip to content

Commit 1530d28

Browse files
committed
Merge branch 'develop'
2 parents 4b29c44 + a971db3 commit 1530d28

14 files changed

+66
-103
lines changed

Sources/ManagedModelMacros/ModelMacro/GenerateInitializers.swift

+2-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,8 @@ extension ModelMacro {
4242
/// - Parameters:
4343
// - entity: An `NSEntityDescription` describing the object.
4444
// - context: An `NSManagedObjectContext` the object should be inserted into.
45-
@available(*, deprecated, renamed: "init(context:)",
46-
message: "Use `init(context:)` or `init()` instead.")
4745
\(raw: access)override init(entity: CoreData.NSEntityDescription, insertInto context: NSManagedObjectContext?)
4846
{
49-
assert(entity === Self._$entity, "Attempt to initialize PersistentModel w/ different entity?")
5047
super.init(entity: entity, insertInto: context)
5148
}
5249
"""
@@ -67,7 +64,7 @@ extension ModelMacro {
6764
/// - Parameters:
6865
// - context: An `NSManagedObjectContext` the object should be inserted into.
6966
\(raw: access)init(context: CoreData.NSManagedObjectContext?) {
70-
super.init(entity: Self._$entity, insertInto: context)
67+
super.init(entity: Self.entity(), insertInto: context)
7168
}
7269
"""
7370
)
@@ -78,7 +75,7 @@ extension ModelMacro {
7875
/// Initialize a `\(modelClassName)` object w/o inserting it into a
7976
/// context.
8077
\(raw: access)init() {
81-
super.init(entity: Self._$entity, insertInto: nil)
78+
super.init(entity: Self.entity(), insertInto: nil)
8279
}
8380
"""
8481
)

Sources/ManagedModelMacros/ModelMacro/ModelMembers.swift

-22
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ import SwiftDiagnostics
1414
* @attached(member, names: // Those are the names we add
1515
* named(init), // Initializers.
1616
* named(schemaMetadata), // The metadata.
17-
* named(entity), // Override the `entity()` function.
18-
* named(_$entity), // The cached the Entity
1917
* named(_$originalName),
2018
* named(_$hashModifier)
2119
* )
@@ -55,17 +53,6 @@ extension ModelMacro: MemberMacro { // @attached(member, names:...)
5553
)
5654
newMembers.append(DeclSyntax(metadata))
5755

58-
if classDecl.findFunctionWithName("entity", isStaticOrClass: true,
59-
parameterCount: 0) == nil
60-
{
61-
newMembers.append(
62-
"""
63-
/// Returns the `NSEntityDescription` associated w/ the `PersistentModel`.
64-
\(raw: access)override class func entity() -> NSEntityDescription { _$entity }
65-
"""
66-
)
67-
}
68-
6956
// TODO: Lookup `originalName` parameter in `macroNode`
7057
newMembers.append(
7158
"""
@@ -77,15 +64,6 @@ extension ModelMacro: MemberMacro { // @attached(member, names:...)
7764
\(raw: access)static let _$hashModifier : String? = nil
7865
"""
7966
)
80-
81-
newMembers.append(
82-
"""
83-
/// The shared `NSEntityDescription` for the `PersistentModel`.
84-
/// Never modify the referred object!
85-
\(raw: access)static let _$entity =
86-
ManagedModels.SchemaBuilder.shared._entity(for: \(modelClassName).self)
87-
"""
88-
)
8967

9068
return newMembers
9169
}

Sources/ManagedModels/ModelMacroDefinition.swift

-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ public macro _PersistedProperty() =
8181
@attached(member, names: // Those are the names we add
8282
named(init), // Initializers.
8383
named(schemaMetadata), // The metadata.
84-
named(entity), // Override the `entity()` function.
85-
named(_$entity), // The cached the Entity
8684
named(_$originalName),
8785
named(_$hashModifier)
8886
)

Sources/ManagedModels/PersistentModel/PersistentModel+KVC.swift

-4
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,6 @@ public extension PersistentModel {
120120
func _setOptionalToOneValue<T>(forKey key: String, to model: T?)
121121
where T: PersistentModel
122122
{
123-
#if DEBUG
124-
let relship = Self._$entity.relationshipsByName[key]!
125-
assert(!relship.isToMany, "relship: \(relship)")
126-
#endif
127123
if let model {
128124
if model.modelContext != self.modelContext {
129125
if let otherCtx = model.modelContext, self.modelContext == nil {

Sources/ManagedModels/PersistentModel/PersistentModel.swift

+1-14
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,6 @@ public protocol PersistentModel: NSManagedObject, Hashable, Identifiable {
1919
*/
2020
static var schemaMetadata : [ NSManagedObjectModel.PropertyMetadata ] { get }
2121

22-
/**
23-
* Reflection data for the model.
24-
*
25-
* This is considered private, use a Schema to access entities, and NEVER
26-
* modify the schema objects after they got setup.
27-
*
28-
* API DIFF: SwiftData doesn't have that, always builds dynamically.
29-
*/
30-
static var _$entity : NSEntityDescription { get }
31-
// Why have that? Cheap cache.
3222

3323
/// The `renamingIdentifier` of the model.
3424
static var _$originalName : String? { get }
@@ -55,16 +45,13 @@ extension PersistentModel {
5545
public static var schemaMetadata : [ NSManagedObjectModel.PropertyMetadata ] {
5646
fatalError("Subclass needs to implement `schemaMetadata`")
5747
}
58-
59-
@inlinable
60-
public static var _$entity : NSEntityDescription { self.entity() }
6148
}
6249

6350
public extension PersistentModel {
6451

6552
@inlinable
6653
static func fetchRequest() -> NSFetchRequest<Self> {
67-
NSFetchRequest<Self>(entityName: _$entity.name ?? NSStringFromClass(self))
54+
NSFetchRequest<Self>(entityName: _typeName(Self.self, qualified: false))
6855
}
6956

7057
@inlinable

Sources/ManagedModels/SchemaCompatibility/CoreDataPrimitiveValue.swift

+15-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public extension CoreData.NSAttributeDescription {
99
struct TypeConfiguration {
1010
let attributeType : NSAttributeType
1111
let isOptional : Bool
12-
let attributeValueClassName : String
12+
let attributeValueClassName : String?
1313
}
1414
}
1515

@@ -37,35 +37,35 @@ extension Int: CoreDataPrimitiveValue {
3737
public static let coreDataValue = NSAttributeDescription.TypeConfiguration(
3838
attributeType : .integer64AttributeType,
3939
isOptional : false,
40-
attributeValueClassName : "NSNumber"
40+
attributeValueClassName : nil
4141
)
4242
}
4343
extension Int16: CoreDataPrimitiveValue {
4444
public static let coreDataValue = NSAttributeDescription.TypeConfiguration(
4545
attributeType : .integer16AttributeType,
4646
isOptional : false,
47-
attributeValueClassName : "NSNumber"
47+
attributeValueClassName : nil
4848
)
4949
}
5050
extension Int32: CoreDataPrimitiveValue {
5151
public static let coreDataValue = NSAttributeDescription.TypeConfiguration(
5252
attributeType : .integer32AttributeType,
5353
isOptional : false,
54-
attributeValueClassName : "NSNumber"
54+
attributeValueClassName : nil
5555
)
5656
}
5757
extension Int64: CoreDataPrimitiveValue {
5858
public static let coreDataValue = NSAttributeDescription.TypeConfiguration(
5959
attributeType : .integer64AttributeType,
6060
isOptional : false,
61-
attributeValueClassName : "NSNumber"
61+
attributeValueClassName : nil
6262
)
6363
}
6464
extension Int8: CoreDataPrimitiveValue {
6565
public static let coreDataValue = NSAttributeDescription.TypeConfiguration(
6666
attributeType : .integer16AttributeType,
6767
isOptional : false,
68-
attributeValueClassName : "NSNumber"
68+
attributeValueClassName : nil
6969
)
7070
}
7171

@@ -91,30 +91,30 @@ extension String: CoreDataPrimitiveValue {
9191
public static let coreDataValue = NSAttributeDescription.TypeConfiguration(
9292
attributeType : .stringAttributeType,
9393
isOptional : false,
94-
attributeValueClassName : "NSString"
94+
attributeValueClassName : nil
9595
)
9696
}
9797

9898
extension Bool: CoreDataPrimitiveValue {
9999
public static let coreDataValue = NSAttributeDescription.TypeConfiguration(
100100
attributeType : .booleanAttributeType,
101101
isOptional : false,
102-
attributeValueClassName : "NSNumber"
102+
attributeValueClassName : nil
103103
)
104104
}
105105

106106
extension Double: CoreDataPrimitiveValue {
107107
public static let coreDataValue = NSAttributeDescription.TypeConfiguration(
108108
attributeType : .doubleAttributeType,
109109
isOptional : false,
110-
attributeValueClassName : "NSNumber"
110+
attributeValueClassName : nil
111111
)
112112
}
113113
extension Float: CoreDataPrimitiveValue {
114114
public static let coreDataValue = NSAttributeDescription.TypeConfiguration(
115115
attributeType : .floatAttributeType,
116116
isOptional : false,
117-
attributeValueClassName : "NSNumber"
117+
attributeValueClassName : nil
118118
)
119119
}
120120

@@ -124,38 +124,38 @@ extension Date: CoreDataPrimitiveValue {
124124
public static let coreDataValue = NSAttributeDescription.TypeConfiguration(
125125
attributeType : .dateAttributeType,
126126
isOptional : false,
127-
attributeValueClassName : "NSDate"
127+
attributeValueClassName : nil
128128
)
129129
}
130130

131131
extension Data: CoreDataPrimitiveValue {
132132
public static let coreDataValue = NSAttributeDescription.TypeConfiguration(
133133
attributeType : .binaryDataAttributeType,
134134
isOptional : false,
135-
attributeValueClassName : "NSDate"
135+
attributeValueClassName : nil
136136
)
137137
}
138138

139139
extension Decimal: CoreDataPrimitiveValue {
140140
public static let coreDataValue = NSAttributeDescription.TypeConfiguration(
141141
attributeType : .decimalAttributeType,
142142
isOptional : false,
143-
attributeValueClassName : "NSDecimalNumber"
143+
attributeValueClassName : nil
144144
)
145145
}
146146

147147
extension UUID: CoreDataPrimitiveValue {
148148
public static let coreDataValue = NSAttributeDescription.TypeConfiguration(
149149
attributeType : .UUIDAttributeType,
150150
isOptional : false,
151-
attributeValueClassName : "NSUUID"
151+
attributeValueClassName : nil
152152
)
153153
}
154154

155155
extension URL: CoreDataPrimitiveValue {
156156
public static let coreDataValue = NSAttributeDescription.TypeConfiguration(
157157
attributeType : .URIAttributeType,
158158
isOptional : false,
159-
attributeValueClassName : "NSURL"
159+
attributeValueClassName : nil
160160
)
161161
}

Sources/ManagedModels/SchemaCompatibility/NSAttributeDescription+Data.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ extension CoreData.NSAttributeDescription: SchemaProperty {
4242
let config = primitiveType.coreDataValue
4343
self.attributeType = config.attributeType
4444
self.isOptional = config.isOptional
45-
self.attributeValueClassName = config.attributeValueClassName
45+
if let newClassName = config.attributeValueClassName {
46+
self.attributeValueClassName = newClassName
47+
}
4648
return
4749
}
4850

@@ -55,7 +57,9 @@ extension CoreData.NSAttributeDescription: SchemaProperty {
5557
let config = primitiveType.coreDataValue
5658
self.attributeType = config.attributeType
5759
self.isOptional = config.isOptional
58-
self.attributeValueClassName = config.attributeValueClassName
60+
if let newClassName = config.attributeValueClassName {
61+
self.attributeValueClassName = newClassName
62+
}
5963
return true
6064
}
6165
else {

Sources/ManagedModels/SchemaCompatibility/NSManagedObjectModel+Data.swift

+4-20
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ public extension NSManagedObjectModel {
1111
// - encodingVersion
1212
// - version
1313

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

28-
@available(*, deprecated, renamed: "model(for:)", message:
29-
"""
30-
Entities can only be used in one NSManagedObjectModel, use the `model(for:)`
31-
static function to get access to s ahred, cached model.
32-
"""
33-
)
3422
convenience init(_ types: [ any PersistentModel.Type ],
3523
version: Schema.Version = Version(1, 0, 0))
3624
{
3725
self.init()
38-
self.entities = SchemaBuilder.shared.lookupAllEntities(for: types)
26+
self.entities = SchemaBuilder().lookupAllEntities(for: types)
3927
}
4028

41-
@available(*, deprecated, renamed: "model(for:)", message:
42-
"""
43-
Entities can only be used in one NSManagedObjectModel, use the `model(for:)`
44-
static function to get access to s ahred, cached model.
45-
"""
46-
)
4729
@inlinable
4830
convenience init(versionedSchema: any VersionedSchema.Type) {
4931
self.init(versionedSchema.models,
@@ -56,9 +38,11 @@ public extension NSManagedObjectModel {
5638

5739
private let lock = NSLock()
5840
private var map = [ Set<ObjectIdentifier> : NSManagedObjectModel ]()
41+
private let sharedBuilder = SchemaBuilder()
5942

6043
public extension NSManagedObjectModel {
6144

45+
/// A cached version of the initializer.
6246
static func model(for versionedSchema: VersionedSchema.Type)
6347
-> NSManagedObjectModel
6448
{
@@ -86,7 +70,7 @@ public extension NSManagedObjectModel {
8670
if let cachedMOM { mom = cachedMOM }
8771
else {
8872
mom = NSManagedObjectModel()
89-
mom.entities = SchemaBuilder.shared.lookupAllEntities(for: types)
73+
mom.entities = sharedBuilder.lookupAllEntities(for: types)
9074
map[typeIDs] = mom
9175
}
9276
lock.unlock()

Sources/ManagedModels/SchemaGeneration/SchemaBuilder.swift

+2-8
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,11 @@ import CoreData
2727
*/
2828
public final class SchemaBuilder {
2929
// Notes:
30-
// - this MUST NOT call `.entity` on the model! might recurse w/ lock, this
30+
// - this MUST NOT call `.entity()` on the model! might recurse w/ lock, this
3131
// object is the authority!
3232
// - there can be multiple entities that use the same name, this spans the
3333
// whole type system. E.g. when versioned schemas are used.
34-
35-
/**
36-
* A shared SchemaBuilder that caches `NSEntityDescription` values for
37-
* ``PersistentModel`` `NSManagedObject`'s.
38-
*/
39-
public static let shared = SchemaBuilder()
40-
34+
4135
private let lock = NSLock() // TODO: use better lock :-)
4236

4337
/// ObjectIdentifier of PersistentModel type to the associated schema.

Tests/ManagedModelTests/BasicModelTests.swift

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ final class BasicModelTests: XCTestCase {
1616

1717
func testEntityName() throws {
1818
let addressType = Fixtures.PersonAddressSchema.Address.self
19-
XCTAssertEqual(addressType._$entity.name, "Address")
2019
XCTAssertEqual(addressType.entity().name, "Address")
2120
}
2221

Tests/ManagedModelTests/CoreDataAssumptionsTests.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ final class CoreDataAssumptionsTests: XCTestCase {
2020
let relationship = NSRelationshipDescription()
2121
relationship.name = "addresses"
2222
//relationship.destinationEntity =
23-
// Fixtures.PersonAddressSchema.Address._$entity
23+
// Fixtures.PersonAddressSchema.Address.entity()
2424
//relationship.inverseRelationship =
25-
// Fixtures.PersonAddressSchema.Address._$entity.relationshipsByName["person"]
25+
// Fixtures.PersonAddressSchema.Address.entity().relationshipsByName["person"]
2626

2727
// This just seems to be the default.
2828
XCTAssertTrue(relationship.isToMany)
@@ -32,9 +32,10 @@ final class CoreDataAssumptionsTests: XCTestCase {
3232
let relationship = NSRelationshipDescription()
3333
relationship.name = "person"
3434
relationship.maxCount = 1 // toOne marker!
35+
#if false // old
3536
relationship.destinationEntity =
36-
Fixtures.PersonAddressSchema.Person._$entity
37-
// Yes! This does not work at this point.
37+
Fixtures.PersonAddressSchema.Person.entity()
38+
#endif
3839
XCTAssertFalse(relationship.isToMany)
3940
}
4041

0 commit comments

Comments
 (0)