Skip to content

Commit

Permalink
working on linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Oct 11, 2024
1 parent 9b8cd52 commit 078e4ec
Show file tree
Hide file tree
Showing 22 changed files with 165 additions and 228 deletions.
4 changes: 2 additions & 2 deletions .swift-format
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
]
},
"prioritizeKeepingFunctionOutputTogether" : false,
"respectsExistingLineBreaks" : true,
"respectsExistingLineBreaks" : false,
"rules" : {
"AllPublicDeclarationsHaveDocumentation" : true,
"AllPublicDeclarationsHaveDocumentation" : false,
"AlwaysUseLiteralForEmptyCollectionInit" : false,
"AlwaysUseLowerCamelCase" : true,
"AmbiguousTrailingClosureOverload" : true,
Expand Down
37 changes: 19 additions & 18 deletions Example/Sources/ContentObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ internal class ContentObject {
}
}

private static func deleteModels(_ models: [Model<Item>], from database: (any Database))
async throws
{
try await database.withModelContext { modelContext in
let items: [Item] = models.compactMap {
modelContext.model(for: $0.persistentIdentifier) as? Item
}
dump(items.first?.persistentModelID)
assert(items.count == models.count)
for item in items {
modelContext.delete(item)
}
try modelContext.save()
}
}

private func beginUpdateItems() {
Task {
do {
Expand Down Expand Up @@ -75,39 +91,24 @@ internal class ContentObject {
self.beginUpdateItems()
}

fileprivate static func deleteModels(_ models: [ModelID<Item>], from database: (any Database))
async throws
{
try await database.withModelContext { modelContext in
let items: [Item] = models.compactMap {
modelContext.model(for: $0.persistentIdentifier) as? Item
}
dump(items.first?.persistentModelID)
assert(items.count == models.count)
for item in items {
modelContext.delete(item)
}
try modelContext.save()
}
}
internal func deleteSelectedItems() {
let models = self.selectedItems.map {
ModelID<Item>(persistentIdentifier: $0.id)
Model<Item>(persistentIdentifier: $0.id)
}
self.deleteItems(models)
}
internal func deleteItems(offsets: IndexSet) {
let models =
offsets
.compactMap { items[$0].id }
.map(ModelID<Item>.init(persistentIdentifier:))
.map(Model<Item>.init(persistentIdentifier:))

assert(models.count == offsets.count)

self.deleteItems(models)
}

internal func deleteItems(_ models: [ModelID<Item>]) {
internal func deleteItems(_ models: [Model<Item>]) {
guard let database else {
return
}
Expand Down
20 changes: 8 additions & 12 deletions Example/Sources/ItemModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,19 @@ import Foundation
import SwiftData

struct ItemModel: Identifiable {
private init(model: ModelID<Item>, timestamp: Date) {
let model: Model<Item>
let timestamp: Date

var id: PersistentIdentifier {
model.persistentIdentifier
}

private init(model: Model<Item>, timestamp: Date) {
self.model = model
self.timestamp = timestamp
}

internal init(item: Item) {
self.init(model: .init(item), timestamp: item.timestamp)
}
@available(*, deprecated)
internal init(id: PersistentIdentifier, timestamp: Date) {
self.model = .init(persistentIdentifier: id)
self.timestamp = timestamp
}

var id: PersistentIdentifier {
model.persistentIdentifier
}
let model: ModelID<Item>
let timestamp: Date
}
75 changes: 37 additions & 38 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import PackageDescription

// swiftlint:disable explicit_acl explicit_top_level_acl
let swiftSettings: [SwiftSetting] = [
SwiftSetting.enableExperimentalFeature("AccessLevelOnImport"),
SwiftSetting.enableExperimentalFeature("BitwiseCopyable"),
Expand All @@ -16,46 +17,44 @@ let swiftSettings: [SwiftSetting] = [
SwiftSetting.enableExperimentalFeature("VariadicGenerics"),

SwiftSetting.enableUpcomingFeature("FullTypedThrows"),
SwiftSetting.enableUpcomingFeature("InternalImportsByDefault")
SwiftSetting.enableUpcomingFeature("InternalImportsByDefault"),

// SwiftSetting.unsafeFlags([
// "-Xfrontend",
// "-warn-long-function-bodies=100"
// ]),
// SwiftSetting.unsafeFlags([
// "-Xfrontend",
// "-warn-long-expression-type-checking=100"
// ])
SwiftSetting.unsafeFlags([
"-Xfrontend",
"-warn-long-function-bodies=100"
]),
SwiftSetting.unsafeFlags([
"-Xfrontend",
"-warn-long-expression-type-checking=100"
])
]

let package = Package(
name: "DataThespian",
platforms: [.iOS(.v17), .macCatalyst(.v17), .macOS(.v14), .tvOS(.v17), .visionOS(.v1), .watchOS(.v10)],
products: [
// Products define the executables and libraries a package produces, making them visible to other packages.
.library(
name: "DataThespian",
targets: ["DataThespian"]
)
],
dependencies: [
.package(url: "https://github.com/brightdigit/FelinePine.git", from: "1.0.0-beta.2"),
.package(url: "https://github.com/swiftlang/swift-testing.git", from: "0.12.0"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
// Targets can depend on other targets in this package and products from dependencies.
.target(
name: "DataThespian",
dependencies: ["FelinePine"],
swiftSettings: swiftSettings
),
.testTarget(
name: "DataThespianTests",
dependencies: [
"DataThespian",
.product(name: "Testing", package: "swift-testing"),
]
)
]
name: "DataThespian",
platforms: [.iOS(.v17), .macCatalyst(.v17), .macOS(.v14), .tvOS(.v17), .visionOS(.v1), .watchOS(.v10)],
products: [
.library(
name: "DataThespian",
targets: ["DataThespian"]
)
],
dependencies: [
.package(url: "https://github.com/brightdigit/FelinePine.git", from: "1.0.0-beta.2"),
.package(url: "https://github.com/swiftlang/swift-testing.git", from: "0.12.0"),
],
targets: [
.target(
name: "DataThespian",
dependencies: ["FelinePine"],
swiftSettings: swiftSettings
),
.testTarget(
name: "DataThespianTests",
dependencies: [
"DataThespian",
.product(name: "Testing", package: "swift-testing"),
]
)
]
)
// swiftlint:enable explicit_acl explicit_top_level_acl
2 changes: 1 addition & 1 deletion Scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ run_command $MINT_RUN swiftlint lint $SWIFTLINT_OPTIONS

pushd $PACKAGE_DIR
run_command $MINT_RUN swift-format lint --recursive --parallel $SWIFTFORMAT_OPTIONS Sources Tests Example/Sources
run_command $MINT_RUN periphery scan $PERIPHERY_OPTIONS --disable-update-check
#run_command $MINT_RUN periphery scan $PERIPHERY_OPTIONS --disable-update-check
popd
4 changes: 1 addition & 3 deletions Sources/DataThespian/Assert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,5 @@ public import Foundation
@inlinable internal func assert(isMainThread: Bool) { assert(isMainThread == Thread.isMainThread) }

@inlinable internal func assertionFailure(
error: any Error,
file: StaticString = #file,
line: UInt = #line
error: any Error, file: StaticString = #file, line: UInt = #line
) { assertionFailure(error.localizedDescription, file: file, line: line) }
4 changes: 1 addition & 3 deletions Sources/DataThespian/BackgroundDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
public final class BackgroundDatabase: Database {
public func withModelContext<T>(_ closure: @Sendable @escaping (ModelContext) throws -> T)
async rethrows -> T
{
try await self.database.withModelContext(closure)
}
{ try await self.database.withModelContext(closure) }

private actor DatabaseContainer {
private let factory: @Sendable () -> any Database
Expand Down
7 changes: 2 additions & 5 deletions Sources/DataThespian/DataMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,11 @@
func addObserver() {
guard object == nil else { return }
object = NotificationCenter.default.addObserver(
forName: .NSManagedObjectContextDidSave,
object: nil,
queue: nil,
forName: .NSManagedObjectContextDidSave, object: nil, queue: nil,
using: { notification in
let update = NotificationDataUpdate(notification)
Task { await self.notifyRegisration(update) }
}
)
})
}

func notifyRegisration(_ update: any DatabaseChangeSet) {
Expand Down
48 changes: 21 additions & 27 deletions Sources/DataThespian/Database+Extras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,30 @@ public import SwiftData
extension Database {
public func insert<PersistentModelType: PersistentModel>(
_ closuer: @Sendable @escaping () -> PersistentModelType
) async -> ModelID<PersistentModelType> {
) async -> Model<PersistentModelType> {
let id: PersistentIdentifier = await self.insert(closuer)
return .init(persistentIdentifier: id)
}

public func with<PersistentModelType: PersistentModel, U: Sendable>(
_ id: ModelID<PersistentModelType>,
_ id: Model<PersistentModelType>,
_ closure: @escaping @Sendable (PersistentModelType) throws -> U
) async throws -> U {
) async rethrows -> U {
try await self.get(for: id.persistentIdentifier) { (model: PersistentModelType?) -> U in
guard let model else {
throw ModelID<PersistentModelType>.Error.notFound(id.persistentIdentifier)
throw Model<PersistentModelType>.NotFoundError(
persistentIdentifier: id.persistentIdentifier)
}
return try closure(model)
}
}

public func first<T: PersistentModel>(_ selectPredicate: Predicate<T>) async throws -> ModelID<
T
>? { try await self.first(selectPredicate, with: ModelID.ifMap) }
public func first<T: PersistentModel>(_ selectPredicate: Predicate<T>) async throws -> Model<T>? {
try await self.first(selectPredicate, with: Model.ifMap)
}

public func first<T: PersistentModel, U: Sendable>(
_ selectPredicate: Predicate<T>,
with closure: @escaping @Sendable (T?) throws -> U
_ selectPredicate: Predicate<T>, with closure: @escaping @Sendable (T?) throws -> U
) async throws -> U {
try await self.fetch {
.init(predicate: selectPredicate, fetchLimit: 1)
Expand All @@ -66,8 +66,7 @@ extension Database {
}

public func first<T: PersistentModel, U: Sendable>(
fetchWith selectPredicate: Predicate<T>,
otherwiseInsertBy insert: @Sendable @escaping () -> T,
fetchWith selectPredicate: Predicate<T>, otherwiseInsertBy insert: @Sendable @escaping () -> T,
with closure: @escaping @Sendable (T) throws -> U
) async throws -> U {
let value = try await self.fetch {
Expand All @@ -78,7 +77,7 @@ extension Database {

if let value { return value }

let inserted: ModelID = await self.insert(insert)
let inserted: Model = await self.insert(insert)

return try await self.with(inserted, closure)
}
Expand All @@ -87,18 +86,16 @@ extension Database {
async throws
{ try await self.delete(where: predicate) }

public func delete<T: PersistentModel>(_ model: ModelID<T>) async {
public func delete<T: PersistentModel>(_ model: Model<T>) async {
await self.delete(T.self, withID: model.persistentIdentifier)
}

public func deleteAll(of types: [any PersistentModel.Type]) async throws {
try await self.transaction { context in for type in types { try context.delete(model: type) }
}
try await self.transaction { context in for type in types { try context.delete(model: type) } }
}

public func fetch<T: PersistentModel, U: Sendable>(
_: T.Type,
with closure: @escaping @Sendable ([T]) throws -> U
_: T.Type, with closure: @escaping @Sendable ([T]) throws -> U
) async throws -> U {
try await self.fetch {
FetchDescriptor<T>()
Expand All @@ -107,19 +104,17 @@ extension Database {
}
}

public func fetch<T: PersistentModel>(_: T.Type) async throws -> [ModelID<T>] {
try await self.fetch(T.self) { models in models.map(ModelID.init) }
public func fetch<T: PersistentModel>(_: T.Type) async throws -> [Model<T>] {
try await self.fetch(T.self) { models in models.map(Model.init) }
}
public func fetch<T: PersistentModel>(
_: T.Type,
_ selectDescriptor: @escaping @Sendable () -> FetchDescriptor<T>
) async throws -> [ModelID<T>] {
try await self.fetch(selectDescriptor) { models in models.map(ModelID.init) }
_: T.Type, _ selectDescriptor: @escaping @Sendable () -> FetchDescriptor<T>
) async throws -> [Model<T>] {
try await self.fetch(selectDescriptor) { models in models.map(Model.init) }
}

public func fetch<T, U: Sendable>(
of _: T.Type,
for objectIDs: [PersistentIdentifier],
of _: T.Type, for objectIDs: [PersistentIdentifier],
with closure: @escaping @Sendable (T) throws -> U
) async throws -> [U] where T: PersistentModel {
try await withThrowingTaskGroup(of: U?.self, returning: [U].self) { group in
Expand All @@ -134,8 +129,7 @@ extension Database {
}

public func get<T, U: Sendable>(
of _: T.Type,
for objectID: PersistentIdentifier,
of _: T.Type, for objectID: PersistentIdentifier,
with closure: @escaping @Sendable (T?) throws -> U
) async throws -> U where T: PersistentModel {
try await self.get(for: objectID) { model in try closure(model) }
Expand Down
Loading

0 comments on commit 078e4ec

Please sign in to comment.