Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/CoreDataRepository/FetchableUnmanagedModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -85,4 +88,9 @@ extension FetchableUnmanagedModel {
public var errorDescription: String {
"\(Self.self)"
}

@inlinable
public static var errorDescription: String {
"\(Self.self)"
}
}
14 changes: 12 additions & 2 deletions Sources/CoreDataRepository/IdentifiedUnmanagedModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions Sources/CoreDataRepository/ReadableUnmanagedModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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()
Expand Down
4 changes: 0 additions & 4 deletions Sources/Internal/ModelsWithIntId/IdentifiableModel_Int.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down