Skip to content

Commit 820fc61

Browse files
committed
Add defaultValue parameter to @Attribute
The `@NSManaged` properties cannot have initializers, but the attribute can still carry the defaultValue.
1 parent 2d813ec commit 820fc61

File tree

4 files changed

+7
-9
lines changed

4 files changed

+7
-9
lines changed

Sources/ManagedModelMacros/ModelMacro/MetadataSlot.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ extension ModelMacro {
4242
labeledExpressions.append(.init(
4343
label: "valueType", expression: ExprSyntax("\(valueType).self")
4444
))
45-
// TBD: put it in here, or not? Would also need to do it in fallback?
46-
labeledExpressions.append(.init(
47-
label: "defaultValue", expression: ExprSyntax("nil")
48-
))
4945

5046
return ExprSyntax(FunctionCallExprSyntax(callee: ExprSyntax("CoreData.NSAttributeDescription")) {
5147
labeledExpressions

Sources/ManagedModels/ModelMacroDefinition.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
* e.g. ``Schema/Attribute/Option/unique``, or none.
1313
* - originalName: The peer to CoreData's `renamingIdentifier`.
1414
* - hashModifier: The peer to CoreData's `versionHashModifier`.
15+
* - defaultValue: The default value for the property.
1516
*/
1617
@available(swift 5.9)
1718
@attached(peer)
1819
public macro Attribute(
1920
_ options: NSAttributeDescription.Option...,
2021
originalName: String? = nil,
21-
hashModifier: String? = nil
22+
hashModifier: String? = nil,
23+
defaultValue: Any? = nil
2224
) = #externalMacro(module: "ManagedModelMacros", type: "AttributeMacro")
2325

2426

Sources/ManagedModels/SchemaCompatibility/NSAttributeDescription+Data.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,8 @@ public extension NSAttributeDescription {
238238
// a few more datapoints.
239239
convenience init(_ options: Option..., originalName: String? = nil,
240240
hashModifier: String? = nil,
241-
name: String, valueType: Any.Type,
242-
defaultValue: Any? = nil)
241+
defaultValue: Any? = nil,
242+
name: String, valueType: Any.Type)
243243
{
244244
self.init(name: name, originalName: originalName, options: options,
245245
valueType: valueType, defaultValue: defaultValue,

Tests/ManagedModelMacrosTests/ManagedModelMacrosTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ final class ModelMacroTests: XCTestCase {
126126
XCTAssertTrue(explodedSource.contains("static let schemaMetadata"))
127127
XCTAssertTrue(explodedSource.contains(
128128
"""
129-
metadata: CoreData.NSAttributeDescription(.external, originalName: "First", name: "firstname", valueType: Swift.String.self, defaultValue: nil))
129+
metadata: CoreData.NSAttributeDescription(.external, originalName: "First", name: "firstname", valueType: Swift.String.self))
130130
"""
131131
))
132132

@@ -282,7 +282,7 @@ final class ModelMacroTests: XCTestCase {
282282
"""
283283
))
284284

285-
#if true
285+
#if false
286286
print("Exploded:---\n")
287287
print(explodedSource)
288288
print("\n-----")

0 commit comments

Comments
 (0)