diff --git a/Sources/CoreDataRepository/FetchableUnmanagedModel.swift b/Sources/CoreDataRepository/FetchableUnmanagedModel.swift index a177eb9..c60929b 100644 --- a/Sources/CoreDataRepository/FetchableUnmanagedModel.swift +++ b/Sources/CoreDataRepository/FetchableUnmanagedModel.swift @@ -70,6 +70,9 @@ public protocol FetchableUnmanagedModel: Sendable { /// A description of the context from where an error is thrown var errorDescription: String { get } + + /// A description of the context from where an error is thrown but there is no instance `self` to use + static var errorDescription: String { get } } extension FetchableUnmanagedModel { @@ -85,4 +88,9 @@ extension FetchableUnmanagedModel { public var errorDescription: String { "\(Self.self)" } + + @inlinable + public static var errorDescription: String { + "\(Self.self)" + } } diff --git a/Sources/CoreDataRepository/IdentifiedUnmanagedModel.swift b/Sources/CoreDataRepository/IdentifiedUnmanagedModel.swift index 9899b71..36e3f30 100644 --- a/Sources/CoreDataRepository/IdentifiedUnmanagedModel.swift +++ b/Sources/CoreDataRepository/IdentifiedUnmanagedModel.swift @@ -32,12 +32,22 @@ extension IdentifiedUnmanagedModel { let fetchResult = try context.fetch(request) guard let managed = fetchResult.first, fetchResult.count == 1 else { throw CoreDataError - .noMatchFoundWhenReadingItem(description: "\(Self.self) -- id: \(errorDescription(for: id))") + .noMatchFoundWhenReadingItem( + description: "\(Self.errorDescription) -- id: \(errorDescription(for: id))" + ) } guard !managed.isDeleted else { throw CoreDataError - .fetchedObjectIsFlaggedAsDeleted(description: "\(Self.self) -- id: \(errorDescription(for: id))") + .fetchedObjectIsFlaggedAsDeleted( + description: "\(Self.errorDescription) -- id: \(errorDescription(for: id))" + ) } return managed } } + +extension IdentifiedUnmanagedModel where UnmanagedId: CustomStringConvertible { + public static func errorDescription(for unmanagedId: UnmanagedId) -> String { + unmanagedId.description + } +} diff --git a/Sources/CoreDataRepository/ReadableUnmanagedModel.swift b/Sources/CoreDataRepository/ReadableUnmanagedModel.swift index 2a5eb0a..25e7ae0 100644 --- a/Sources/CoreDataRepository/ReadableUnmanagedModel.swift +++ b/Sources/CoreDataRepository/ReadableUnmanagedModel.swift @@ -80,7 +80,7 @@ extension ReadableUnmanagedModel where Self: ManagedIdReferencable { @inlinable public func readManaged(from context: NSManagedObjectContext) throws -> ManagedModel { guard let managedId else { - throw CoreDataError.noObjectIdOnItem(description: "\(Self.self)") + throw CoreDataError.noObjectIdOnItem(description: errorDescription) } return try context.notDeletedObject(for: managedId).asManagedModel() } @@ -90,7 +90,7 @@ extension ReadableUnmanagedModel where Self: ManagedIdUrlReferencable { @inlinable public func readManaged(from context: NSManagedObjectContext) throws -> ManagedModel { guard let managedIdUrl else { - throw CoreDataError.noUrlOnItemToMapToObjectId(description: "\(Self.self)") + throw CoreDataError.noUrlOnItemToMapToObjectId(description: errorDescription) } let managedId = try context.objectId(from: managedIdUrl).get() return try context.notDeletedObject(for: managedId).asManagedModel() diff --git a/Sources/Internal/ModelsWithIntId/IdentifiableModel_Int.swift b/Sources/Internal/ModelsWithIntId/IdentifiableModel_Int.swift index 15cd66f..bf0a287 100644 --- a/Sources/Internal/ModelsWithIntId/IdentifiableModel_Int.swift +++ b/Sources/Internal/ModelsWithIntId/IdentifiableModel_Int.swift @@ -105,10 +105,6 @@ extension IdentifiableModel_IntId: IdentifiedUnmanagedModel { } package nonisolated(unsafe) static let unmanagedIdExpression = NSExpression(forKeyPath: \ManagedModel_IntId.id) - - package static func errorDescription(for unmanagedId: Int) -> String { - unmanagedId.description - } } extension IdentifiableModel_IntId: WritableUnmanagedModel { diff --git a/Sources/Internal/ModelsWithUuidId/IdentifiableModel_Uuid.swift b/Sources/Internal/ModelsWithUuidId/IdentifiableModel_Uuid.swift index 6bc34fd..71f805f 100644 --- a/Sources/Internal/ModelsWithUuidId/IdentifiableModel_Uuid.swift +++ b/Sources/Internal/ModelsWithUuidId/IdentifiableModel_Uuid.swift @@ -105,10 +105,6 @@ extension IdentifiableModel_UuidId: IdentifiedUnmanagedModel { } package nonisolated(unsafe) static let unmanagedIdExpression = NSExpression(forKeyPath: \ManagedModel_UuidId.id) - - package static func errorDescription(for unmanagedId: UUID) -> String { - unmanagedId.uuidString - } } extension IdentifiableModel_UuidId: WritableUnmanagedModel {