-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add disableGenerateInit to NSMainModelActor
- Loading branch information
Showing
13 changed files
with
227 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"configurations": [ | ||
{ | ||
"type": "lldb", | ||
"request": "launch", | ||
"args": [], | ||
"cwd": "${workspaceFolder:CoreDataEvolution}", | ||
"name": "Debug CoreDataEvolutionClient", | ||
"program": "${workspaceFolder:CoreDataEvolution}/.build/debug/CoreDataEvolutionClient", | ||
"preLaunchTask": "swift: Build Debug CoreDataEvolutionClient" | ||
}, | ||
{ | ||
"type": "lldb", | ||
"request": "launch", | ||
"args": [], | ||
"cwd": "${workspaceFolder:CoreDataEvolution}", | ||
"name": "Release CoreDataEvolutionClient", | ||
"program": "${workspaceFolder:CoreDataEvolution}/.build/release/CoreDataEvolutionClient", | ||
"preLaunchTask": "swift: Build Release CoreDataEvolutionClient" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// ------------------------------------------------ | ||
// Original project: CoreDataEvolution | ||
// Created on 2024/10/30 by Fatbobman(东坡肘子) | ||
// X: @fatbobman | ||
// Mastodon: @[email protected] | ||
// GitHub: @fatbobman | ||
// Blog: https://fatbobman.com | ||
// ------------------------------------------------ | ||
// Copyright © 2024-present Fatbobman. All rights reserved. | ||
|
||
import Foundation | ||
import SwiftData | ||
|
||
@MainActor | ||
public protocol MainModelActorX: AnyObject { | ||
/// Provides access to the NSPersistentContainer associated with the NSMainModelActor. | ||
var modelContainer: ModelContainer { get } | ||
} | ||
|
||
extension MainModelActorX { | ||
/// Exposes the view context for model operations. | ||
public var modelContext: ModelContext { | ||
modelContainer.mainContext | ||
} | ||
|
||
/// Retrieves a model instance based on its identifier, cast to the specified type. | ||
/// | ||
/// This method attempts to fetch a model instance from the context using the provided identifier. If the model is not found, it constructs a fetch descriptor with a predicate matching the identifier and attempts to fetch the model. The fetched model is then cast to the specified type. | ||
/// | ||
/// - Parameters: | ||
/// - id: The identifier of the model to fetch. | ||
/// - as: The type to which the fetched model should be cast. | ||
/// - Returns: The fetched model instance cast to the specified type, or nil if not found. | ||
public subscript<T>(id: PersistentIdentifier, as: T.Type) -> T? where T: PersistentModel { | ||
let predicate = #Predicate<T> { | ||
$0.persistentModelID == id | ||
} | ||
if let object: T = modelContext.registeredModel(for: id) { | ||
return object | ||
} | ||
let fetchDescriptor = FetchDescriptor<T>(predicate: predicate) | ||
let object: T? = try? modelContext.fetch(fetchDescriptor).first | ||
return object | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// | ||
// ------------------------------------------------ | ||
// Original project: CoreDataEvolution | ||
// Created on 2024/10/30 by Fatbobman(东坡肘子) | ||
// X: @fatbobman | ||
// Mastodon: @[email protected] | ||
// GitHub: @fatbobman | ||
// Blog: https://fatbobman.com | ||
// ------------------------------------------------ | ||
// Copyright © 2024-present Fatbobman. All rights reserved. | ||
|
||
import Foundation | ||
import SwiftSyntax | ||
import SwiftSyntaxMacros | ||
|
||
/// Determines whether to generate an initializer based on the attribute node. | ||
/// | ||
/// This function checks the attribute node for an argument labeled "disableGenerateInit" with a boolean value. | ||
/// If such an argument is found and its value is false, the function returns false, indicating that an initializer should not be generated. | ||
/// Otherwise, it returns true, indicating that an initializer should be generated. | ||
/// | ||
/// - Parameter node: The attribute node to check. | ||
/// - Returns: A boolean indicating whether to generate an initializer. | ||
func shouldGenerateInitializer(from node: AttributeSyntax) -> Bool { | ||
guard let argumentList = node.arguments?.as(LabeledExprListSyntax.self) else { | ||
return true // Default to true if no arguments are present. | ||
} | ||
|
||
for argument in argumentList { | ||
if argument.label?.text == "disableGenerateInit", | ||
let booleanLiteral = argument.expression.as(BooleanLiteralExprSyntax.self) | ||
{ | ||
return booleanLiteral.literal.text != "true" // Return false if "disableGenerateInit" is set to true. | ||
} | ||
} | ||
return true // Default to true if "disableGenerateInit" is not found or is set to false. | ||
} | ||
|
||
/// Checks if the access level of the declared type is public. | ||
/// | ||
/// This function iterates through the modifiers of the declaration to check if the "public" access level is specified. | ||
/// | ||
/// - Parameter declaration: The declaration to check. | ||
/// - Returns: A boolean indicating whether the access level is public. | ||
func isPublic(from declaration: some DeclGroupSyntax) -> Bool { | ||
return declaration.modifiers.contains { modifier in | ||
modifier.name.text == "public" // Check if the "public" modifier is present. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters