Skip to content

Commit

Permalink
Update For Swift 6 (#61)
Browse files Browse the repository at this point in the history
* Update to 5.9

* Make it compile

* Remove the warnings

* Adopt new APIs

* update CI

* Fix test compilation
  • Loading branch information
0xTim authored Oct 6, 2024
1 parent 266a489 commit 79bf354
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 67 deletions.
14 changes: 6 additions & 8 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
dbimage:
- mongo:6
runs-on: ubuntu-latest
container: swift:5.7-jammy
container: swift:6.0-jammy
services:
mongo-a:
image: ${{ matrix.dbimage }}
Expand All @@ -48,7 +48,7 @@ jobs:
api-breakage:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
container: swift:5.8-jammy
container: swift:6.0-jammy
steps:
- name: Check out package
uses: actions/checkout@v3
Expand All @@ -68,11 +68,9 @@ jobs:
dbimage:
- mongo:6
runner:
- swift:5.6-focal
- swift:5.7-jammy
- swift:5.8-jammy
- swiftlang/swift:nightly-5.9-jammy
- swiftlang/swift:nightly-main-jammy
- swift:5.9-focal
- swift:5.10-jammy
- swift:6.0-jammy
container: ${{ matrix.runner }}
runs-on: ubuntu-latest
services:
Expand All @@ -92,7 +90,7 @@ jobs:
fail-fast: false
matrix:
dbimage: ['mongodb-community']
macos: ['macos-12']
macos: ['macos-14']
xcode: ['latest-stable']
runs-on: ${{ matrix.macos }}
env:
Expand Down
18 changes: 14 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.6
// swift-tools-version:5.9
import PackageDescription

let package = Package(
Expand All @@ -13,7 +13,7 @@ let package = Package(
.library(name: "FluentMongoDriver", targets: ["FluentMongoDriver"]),
],
dependencies: [
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.37.0"),
.package(url: "https://github.com/vapor/fluent-kit.git", from: "1.49.0"),
.package(url: "https://github.com/orlandos-nl/MongoKitten.git", from: "6.7.13"),
.package(url: "https://github.com/orlandos-nl/DNSClient.git", exact: "2.3.0"),
],
Expand All @@ -24,14 +24,24 @@ let package = Package(
.product(name: "FluentKit", package: "fluent-kit"),
.product(name: "MongoKitten", package: "MongoKitten"),
.product(name: "DNSClient", package: "DNSClient"),
]
],
swiftSettings: swiftSettings
),
.testTarget(
name: "FluentMongoDriverTests",
dependencies: [
.target(name: "FluentMongoDriver"),
.product(name: "FluentBenchmark", package: "fluent-kit"),
]
],
swiftSettings: swiftSettings
),
]
)

var swiftSettings: [SwiftSetting] { [
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("ConciseMagicFile"),
.enableUpcomingFeature("ForwardTrailingClosures"),
.enableUpcomingFeature("DisableOutwardActorInference"),
.enableExperimentalFeature("StrictConcurrency=complete"),
] }
4 changes: 2 additions & 2 deletions Sources/FluentMongoDriver/ChangeStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import MongoKitten
import FluentKit

extension Model {
public static func watch(on database: Database, options: ChangeStreamOptions = .init()) -> EventLoopFuture<ChangeStream<Self>> {
guard let mongodb = database as? MongoDatabaseRepresentable else {
public static func watch(on database: any Database, options: ChangeStreamOptions = .init()) -> EventLoopFuture<ChangeStream<Self>> {
guard let mongodb = database as? (any MongoDatabaseRepresentable) else {
return database.eventLoop.makeFailedFuture(FluentMongoError.notMongoDB)
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/FluentMongoDriver/DatabaseValue+Mongo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import FluentKit
import MongoKitten

extension DatabaseQuery.Value {
func makePrimitive() throws -> Primitive {
func makePrimitive() throws -> any Primitive {
switch self {
case .array(let values):
var array = Document(isArray: true)
Expand All @@ -24,7 +24,7 @@ extension DatabaseQuery.Value {
return Null()
case .default:
throw FluentMongoError.unsupportedDefaultValue
case .custom(let primitive as Primitive):
case .custom(let primitive as any Primitive):
return primitive
case .enumCase(let string):
return string
Expand Down
8 changes: 4 additions & 4 deletions Sources/FluentMongoDriver/Document+DatabaseOutput.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import FluentKit
import MongoKitten
@preconcurrency import MongoKitten

extension Document {
internal func databaseOutput(using decoder: BSONDecoder) -> DatabaseOutput {
internal func databaseOutput(using decoder: BSONDecoder) -> any DatabaseOutput {
_FluentMongoOutput(document: self, decoder: decoder, schema: nil)
}
}
Expand All @@ -16,7 +16,7 @@ private struct _FluentMongoOutput: DatabaseOutput {
self.document.debugDescription
}

func schema(_ schema: String) -> DatabaseOutput {
func schema(_ schema: String) -> any DatabaseOutput {
_FluentMongoOutput(document: self.document, decoder: self.decoder, schema: schema)
}

Expand All @@ -41,7 +41,7 @@ private struct _FluentMongoOutput: DatabaseOutput {
)
}

private func primitive(_ key: FieldKey) -> Primitive? {
private func primitive(_ key: FieldKey) -> (any Primitive)? {
if let schema = self.schema {
let nested = self.document[schema] as! Document
return nested[key.makeMongoKey()]
Expand Down
13 changes: 11 additions & 2 deletions Sources/FluentMongoDriver/Errors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@ import MongoCore
import FluentKit
import Foundation

extension MongoWriteError: DatabaseError {
extension MongoWriteError {
public var isSyntaxError: Bool { false }
public var isConstraintFailure: Bool { false }
public var isConnectionClosed: Bool { false }
}

extension MongoError: DatabaseError {
// Used for DatabaseError
extension MongoError {
public var isSyntaxError: Bool { false }
public var isConstraintFailure: Bool { false }
public var isConnectionClosed: Bool { false }
}

#if compiler(<6)
extension MongoWriteError: DatabaseError {}
extension MongoError: DatabaseError {}
#else
extension MongoWriteError: @retroactive DatabaseError {}
extension MongoError: @retroactive DatabaseError {}
#endif

public enum FluentMongoError: Error, DatabaseError {
public var isSyntaxError: Bool { false }
public var isConstraintFailure: Bool { false }
Expand Down
12 changes: 6 additions & 6 deletions Sources/FluentMongoDriver/GridFS.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import MongoKitten
@preconcurrency import MongoKitten
import FluentKit

extension MongoDatabaseRepresentable {
Expand All @@ -9,15 +9,15 @@ extension MongoDatabaseRepresentable {
}

extension GridFSFile {
public static func find(_ id: Primitive, on database: Database) -> EventLoopFuture<GridFSFile?> {
guard let mongodb = database as? MongoDatabaseRepresentable else {
public static func find(_ id: any Primitive, on database: any Database) -> EventLoopFuture<GridFSFile?> {
guard let mongodb = database as? (any MongoDatabaseRepresentable) else {
return database.eventLoop.makeFailedFuture(FluentMongoError.notMongoDB)
}

return mongodb._gridFS.findFile(byId: id)
}

public static func read(_ id: Primitive, on database: Database) -> EventLoopFuture<ByteBuffer?> {
public static func read(_ id: any Primitive, on database: any Database) -> EventLoopFuture<ByteBuffer?> {
return find(id, on: database).flatMap { file in
guard let file = file else {
return database.eventLoop.makeSucceededFuture(nil)
Expand All @@ -32,9 +32,9 @@ extension GridFSFile {
_ buffer: ByteBuffer,
named filename: String? = nil,
metadata: Document? = nil,
on database: Database
on database: any Database
) -> EventLoopFuture<GridFSFile> {
guard let mongodb = database as? MongoDatabaseRepresentable else {
guard let mongodb = database as? (any MongoDatabaseRepresentable) else {
return database.eventLoop.makeFailedFuture(FluentMongoError.notMongoDB)
}

Expand Down
9 changes: 5 additions & 4 deletions Sources/FluentMongoDriver/MongoDB+Aggregate.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import FluentKit
import MongoKitten
@preconcurrency import MongoKitten
import MongoCore

extension FluentMongoDatabase {
@preconcurrency
func aggregate(
query: DatabaseQuery,
aggregate: DatabaseQuery.Aggregate,
onOutput: @escaping (DatabaseOutput) -> ()
onOutput: @Sendable @escaping (any DatabaseOutput) -> ()
) -> EventLoopFuture<Void> {
guard case .field(let field, let method) = aggregate else {
return eventLoop.makeFailedFuture(FluentMongoError.unsupportedCustomAggregate)
Expand Down Expand Up @@ -52,7 +53,7 @@ extension FluentMongoDatabase {

private func count(
query: DatabaseQuery,
onOutput: @escaping (DatabaseOutput) -> ()
onOutput: @escaping @Sendable (any DatabaseOutput) -> ()
) -> EventLoopFuture<Void> {
do {
let condition = try query.makeMongoDBFilter(aggregate: false)
Expand All @@ -78,7 +79,7 @@ extension FluentMongoDatabase {
query: DatabaseQuery,
mongoOperator: String,
field: DatabaseQuery.Field,
onOutput: @escaping (DatabaseOutput) -> ()
onOutput: @escaping @Sendable (any DatabaseOutput) -> ()
) -> EventLoopFuture<Void> {
do {
let field = try field.makeMongoPath()
Expand Down
5 changes: 3 additions & 2 deletions Sources/FluentMongoDriver/MongoDB+Create.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import FluentKit
import MongoKitten
@preconcurrency import MongoKitten
import MongoCore

extension FluentMongoDatabase {
@preconcurrency
func create(
query: DatabaseQuery,
onOutput: @escaping (DatabaseOutput) -> ()
onOutput: @Sendable @escaping (any DatabaseOutput) -> ()
) -> EventLoopFuture<Void> {
do {
let documents = try query.makeValueDocuments()
Expand Down
6 changes: 3 additions & 3 deletions Sources/FluentMongoDriver/MongoDB+DatabaseRow.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import MongoKitten
@preconcurrency import MongoKitten
import FluentKit

struct _MongoDBAggregateResponse: DatabaseOutput {
let value: Primitive
let value: any Primitive
let decoder: BSONDecoder

var description: String {
"\(self.value)"
}

func schema(_ schema: String) -> DatabaseOutput {
func schema(_ schema: String) -> any DatabaseOutput {
self
}

Expand Down
5 changes: 3 additions & 2 deletions Sources/FluentMongoDriver/MongoDB+Delete.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import FluentKit
import MongoKitten
@preconcurrency import MongoKitten
import MongoCore

extension FluentMongoDatabase {
@preconcurrency
func delete(
query: DatabaseQuery,
onOutput: @escaping (DatabaseOutput) -> ()
onOutput: @Sendable @escaping (any DatabaseOutput) -> ()
) -> EventLoopFuture<Void> {
do {
let filter = try query.makeMongoDBFilter(aggregate: false)
Expand Down
5 changes: 3 additions & 2 deletions Sources/FluentMongoDriver/MongoDB+Join.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import MongoCore
extension FluentMongoDatabase {
func join(
query: DatabaseQuery,
onOutput: @escaping (DatabaseOutput) -> ()
onOutput: @escaping (any DatabaseOutput) -> ()
) -> EventLoopFuture<Void> {
do {
let stages = try query.makeAggregatePipeline()
Expand All @@ -19,9 +19,10 @@ extension FluentMongoDatabase {
}
}

@preconcurrency
func joinCount(
query: DatabaseQuery,
onOutput: @escaping (DatabaseOutput) -> ()
onOutput: @Sendable @escaping (any DatabaseOutput) -> ()
) -> EventLoopFuture<Void> {
do {
let stages = try query.makeAggregatePipeline()
Expand Down
3 changes: 2 additions & 1 deletion Sources/FluentMongoDriver/MongoDB+Read.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import MongoKitten
import MongoCore

extension FluentMongoDatabase {
@preconcurrency
func read(
query: DatabaseQuery,
onOutput: @escaping (DatabaseOutput) -> ()
onOutput: @Sendable @escaping (any DatabaseOutput) -> ()
) -> EventLoopFuture<Void> {
do {
let condition = try query.makeMongoDBFilter(aggregate: false)
Expand Down
2 changes: 1 addition & 1 deletion Sources/FluentMongoDriver/MongoDB+Schema.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import FluentKit
import MongoKitten
@preconcurrency import MongoKitten
import MongoCore

extension FluentMongoDatabase {
Expand Down
5 changes: 3 additions & 2 deletions Sources/FluentMongoDriver/MongoDB+Transaction.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import FluentKit
import MongoKitten
@preconcurrency import MongoKitten
import MongoCore

extension FluentMongoDatabase {
func transaction<T>(_ closure: @escaping (Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
@preconcurrency
func transaction<T: Sendable>(_ closure: @Sendable @escaping (any Database) -> EventLoopFuture<T>) -> EventLoopFuture<T> {
guard !self.inTransaction else {
return closure(self)
}
Expand Down
5 changes: 3 additions & 2 deletions Sources/FluentMongoDriver/MongoDB+Update.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import FluentKit
import MongoKitten
@preconcurrency import MongoKitten
import MongoCore

extension FluentMongoDatabase {
@preconcurrency
func update(
query: DatabaseQuery,
onOutput: @escaping (DatabaseOutput) -> ()
onOutput: @Sendable @escaping (any DatabaseOutput) -> ()
) -> EventLoopFuture<Void> {
do {
let filter = try query.makeMongoDBFilter(aggregate: false)
Expand Down
Loading

0 comments on commit 79bf354

Please sign in to comment.