From 9fa66b581f31088ea0ef4e46857d834880578ede Mon Sep 17 00:00:00 2001 From: Valerio Mazzeo Date: Thu, 18 Apr 2019 21:44:39 +0100 Subject: [PATCH] Swift 5 (#11) --- .swift-version | 2 +- .travis.yml | 6 ++--- Package.swift | 4 ++-- README.md | 4 ++-- .../FluentMongo/BSONEncoder+BSONValue.swift | 2 +- Sources/FluentMongo/Document+Nested.swift | 2 +- Sources/FluentMongo/FluentMongoModel.swift | 2 +- Sources/FluentMongo/IndexBuilder.swift | 4 ++-- .../FluentMongo/MongoConnection+Connect.swift | 2 +- Sources/FluentMongo/MongoConnection.swift | 23 +++++++++---------- Sources/FluentMongo/MongoDatabaseConfig.swift | 2 +- Sources/FluentMongo/QueryBuilder+Filter.swift | 2 +- Sources/FluentMongo/QueryBuilder+Key.swift | 2 +- .../FluentMongo/QueryBuilder+Operators.swift | 2 +- .../FluentMongo/QueryBuilder+Options.swift | 2 +- Sources/FluentMongo/QueryBuilder+Update.swift | 2 +- .../FluentMongoProviderTests.swift | 9 ++++++-- 17 files changed, 38 insertions(+), 34 deletions(-) diff --git a/.swift-version b/.swift-version index bf77d54..819e07a 100644 --- a/.swift-version +++ b/.swift-version @@ -1 +1 @@ -4.2 +5.0 diff --git a/.travis.yml b/.travis.yml index ebe0aaf..10ebd99 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,13 +4,13 @@ os: language: generic sudo: required dist: trusty -osx_image: xcode10.1 +osx_image: xcode10.2 services: docker before_install: - if [ $TRAVIS_OS_NAME == "linux" ]; then SWIFT_VERSION=$(<.swift-version); - docker pull swift:${SWIFT_VERSION}; + docker pull swift:${SWIFT_VERSION}-xenial; docker pull mongo:4.0.8; docker run -d -p 27017:27017 mongo:4.0.8; elif [ $TRAVIS_OS_NAME == "osx" ]; then @@ -48,7 +48,7 @@ script: && swift build && swift build -c release && swift test"; - docker run --rm --net=host -v $(pwd):/app --workdir /app swift:${SWIFT_VERSION} bash -c "${args}"; + docker run --rm --net=host -v $(pwd):/app --workdir /app swift:${SWIFT_VERSION}-xenial bash -c "${args}"; fi notifications: diff --git a/Package.swift b/Package.swift index 0c669cb..5abb5d3 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:4.2 +// swift-tools-version:5.0 // // Package.swift @@ -18,7 +18,7 @@ let package = Package( dependencies: [ .package(url: "https://github.com/vapor/core.git", .upToNextMajor(from: "3.8.1")), .package(url: "https://github.com/vapor/fluent.git", .upToNextMajor(from: "3.2.0")), - .package(url: "https://github.com/mongodb/mongo-swift-driver.git", .exact("0.0.9")) + .package(url: "https://github.com/mongodb/mongo-swift-driver.git", .upToNextMinor(from: "0.1.0")) ], targets: [ .target(name: "FluentMongo", dependencies: ["Async", "Fluent", "MongoSwift"]), diff --git a/README.md b/README.md index 58fc304..4fde52c 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # vapor-fluent-mongo -![Swift](https://img.shields.io/badge/swift-4.2-orange.svg) +![Swift](https://img.shields.io/badge/swift-5.0-orange.svg) [![Build Status](https://travis-ci.com/asensei/vapor-fluent-mongo.svg?token=eSrCssnzja3G3GciyhUB&branch=master)](https://travis-ci.com/asensei/vapor-fluent-mongo) Mongo driver for Fluent `3.x`. @@ -29,7 +29,7 @@ On Linux: please follow the [instructions](http://mongoc.org/libmongoc/current/i Add FluentMongo to your dependencies in `Package.swift`: ```swift -// swift-tools-version:4.2 +// swift-tools-version:5.0 import PackageDescription let package = Package( diff --git a/Sources/FluentMongo/BSONEncoder+BSONValue.swift b/Sources/FluentMongo/BSONEncoder+BSONValue.swift index ea58688..123a238 100644 --- a/Sources/FluentMongo/BSONEncoder+BSONValue.swift +++ b/Sources/FluentMongo/BSONEncoder+BSONValue.swift @@ -9,7 +9,7 @@ import Foundation import MongoSwift -public extension BSONEncoder { +extension BSONEncoder { public func encodeBSONValue(_ value: T) throws -> BSONValue { // if it's already a `BSONValue`, just return it, unless if it is an diff --git a/Sources/FluentMongo/Document+Nested.swift b/Sources/FluentMongo/Document+Nested.swift index 42e1967..4c78ba5 100644 --- a/Sources/FluentMongo/Document+Nested.swift +++ b/Sources/FluentMongo/Document+Nested.swift @@ -10,7 +10,7 @@ import Foundation import MongoSwift // Review when https://jira.mongodb.org/browse/SWIFT-273 will be fixed. -public extension Document { +extension Document { public subscript(keys: [String]) -> BSONValue? { get { guard !keys.isEmpty else { diff --git a/Sources/FluentMongo/FluentMongoModel.swift b/Sources/FluentMongo/FluentMongoModel.swift index ef59a90..4f011d0 100644 --- a/Sources/FluentMongo/FluentMongoModel.swift +++ b/Sources/FluentMongo/FluentMongoModel.swift @@ -16,7 +16,7 @@ public protocol FluentMongoModel { var _id: ID? { get set } } -public extension FluentMongoModel where Self: Model { +extension FluentMongoModel where Self: Model { public static var idKey: WritableKeyPath { return \._id } diff --git a/Sources/FluentMongo/IndexBuilder.swift b/Sources/FluentMongo/IndexBuilder.swift index dd8550c..34ad3b1 100644 --- a/Sources/FluentMongo/IndexBuilder.swift +++ b/Sources/FluentMongo/IndexBuilder.swift @@ -22,7 +22,7 @@ public final class IndexBuilder { } } -public extension IndexBuilder { +extension IndexBuilder { public func create() -> Future { return self.connection.flatMap { conn in @@ -37,7 +37,7 @@ public extension IndexBuilder { } } -public extension IndexBuilder { +extension IndexBuilder { public func key(_ key: KeyPath, _ direction: FluentMongoQuerySortDirection = .ascending) -> IndexBuilder { let property: FluentProperty = .keyPath(key) diff --git a/Sources/FluentMongo/MongoConnection+Connect.swift b/Sources/FluentMongo/MongoConnection+Connect.swift index 8ca6eae..ac3d58f 100644 --- a/Sources/FluentMongo/MongoConnection+Connect.swift +++ b/Sources/FluentMongo/MongoConnection+Connect.swift @@ -9,7 +9,7 @@ import Foundation import Async -public extension MongoConnection { +extension MongoConnection { public static func connect(config: MongoDatabaseConfig, on worker: Worker) -> Future { do { diff --git a/Sources/FluentMongo/MongoConnection.swift b/Sources/FluentMongo/MongoConnection.swift index 9a44501..356ec7b 100644 --- a/Sources/FluentMongo/MongoConnection.swift +++ b/Sources/FluentMongo/MongoConnection.swift @@ -16,7 +16,7 @@ public final class MongoConnection: BasicWorker, DatabaseConnection, DatabaseQue public required init(config: MongoDatabaseConfig, on worker: Worker) throws { self.config = config self.worker = worker - self.client = try MongoClient(connectionString: config.connectionURL.absoluteString, options: config.options) + self.client = try MongoClient(config.connectionURL.absoluteString, options: config.options) self.isClosed = false self.logger = nil } @@ -54,8 +54,8 @@ public final class MongoConnection: BasicWorker, DatabaseConnection, DatabaseQue public func query(_ query: Database.Query, _ handler: @escaping (Database.Output) throws -> Void) -> Future { do { self.logger?.record(query: String(describing: query)) - let database = try self.client.db(config.database) - let collection = try database.collection(query.collection) + let database = self.client.db(config.database) + let collection = database.collection(query.collection) switch query.action { case .insert: @@ -103,11 +103,10 @@ public final class MongoConnection: BasicWorker, DatabaseConnection, DatabaseQue } return self.worker.future() - } catch let error as MongoError { + } catch let error as ServerError { switch error { - // TODO: update this as soon as https://github.com/mongodb/mongo-swift-driver/issues/200 is fixed. - case .commandError(let message) where message.hasPrefix("E11000"): - return self.worker.future(error: Error.duplicatedKey(message)) + case .writeError(let writeError, let writeConcernError, _) where writeError?.code == 11000 || writeConcernError?.code == 11000: + return self.worker.future(error: Error.duplicatedKey(error.errorDescription ?? "No error description available.")) default: return self.worker.future(error: Error.underlyingDriverError(error)) } @@ -145,7 +144,7 @@ extension MongoConnection { throw IndexBuilderError.invalidKeys } self.logger?.record(query: "MongoConnection.createIndex") - let database = try self.client.db(config.database) + let database = self.client.db(config.database) self.logger?.record(query: "Create index on \(collection)") _ = try database.collection(collection).createIndex(index) @@ -162,7 +161,7 @@ extension MongoConnection { } self.logger?.record(query: "MongoConnection.dropIndex") - let database = try self.client.db(config.database) + let database = self.client.db(config.database) self.logger?.record(query: "Drop index on \(collection)") _ = try database.collection(collection).dropIndex(index) @@ -180,7 +179,7 @@ extension MongoConnection { func prepareMigrationMetadata() -> Future { do { self.logger?.record(query: "MongoConnection.prepareMigrationMetadata") - let database = try self.client.db(config.database) + let database = self.client.db(config.database) let collection = MigrationLog.entity let collections = try database.listCollections(options: .init(filter: ["name": collection])) if collections.contains(where: { $0["name"] as? String == collection }) { @@ -199,7 +198,7 @@ extension MongoConnection { func revertMigrationMetadata() -> Future { do { self.logger?.record(query: "MongoConnection.revertMigrationMetadata") - let database = try self.client.db(config.database) + let database = self.client.db(config.database) let collection = MigrationLog.entity self.logger?.record(query: "Drop collection: \(collection)") _ = try database.collection(collection).drop() @@ -211,7 +210,7 @@ extension MongoConnection { } } -public extension MongoConnection { +extension MongoConnection { public enum Error: Swift.Error { case invalidQuery(Database.Query) case duplicatedKey(String) diff --git a/Sources/FluentMongo/MongoDatabaseConfig.swift b/Sources/FluentMongo/MongoDatabaseConfig.swift index 1defc99..89d341b 100644 --- a/Sources/FluentMongo/MongoDatabaseConfig.swift +++ b/Sources/FluentMongo/MongoDatabaseConfig.swift @@ -78,7 +78,7 @@ public struct MongoDatabaseConfig { } } -public extension MongoDatabaseConfig { +extension MongoDatabaseConfig { public enum EnvironmentKey: String { case connectionURL = "FLUENT_MONGO_CONNECTION_URL" diff --git a/Sources/FluentMongo/QueryBuilder+Filter.swift b/Sources/FluentMongo/QueryBuilder+Filter.swift index 5a830df..0e1725a 100644 --- a/Sources/FluentMongo/QueryBuilder+Filter.swift +++ b/Sources/FluentMongo/QueryBuilder+Filter.swift @@ -9,7 +9,7 @@ import Foundation import Fluent -public extension QueryBuilder where Database == MongoDatabase { +extension QueryBuilder where Database == MongoDatabase { @discardableResult public func filter(_ key: KeyPath, _ method: MongoDatabase.QueryFilterMethod, _ value: C.Element) -> Self diff --git a/Sources/FluentMongo/QueryBuilder+Key.swift b/Sources/FluentMongo/QueryBuilder+Key.swift index 836a8cf..d5452a7 100644 --- a/Sources/FluentMongo/QueryBuilder+Key.swift +++ b/Sources/FluentMongo/QueryBuilder+Key.swift @@ -10,7 +10,7 @@ import Foundation import Fluent // Duplicates https://github.com/vapor/fluent/pull/601 -public extension QueryBuilder { +extension QueryBuilder { // MARK: Key /// Applies a key to this query specifying a field to fetch from the database. diff --git a/Sources/FluentMongo/QueryBuilder+Operators.swift b/Sources/FluentMongo/QueryBuilder+Operators.swift index 41ab1a6..2ed8014 100644 --- a/Sources/FluentMongo/QueryBuilder+Operators.swift +++ b/Sources/FluentMongo/QueryBuilder+Operators.swift @@ -9,7 +9,7 @@ import Foundation import Fluent -public extension QueryBuilder where Database == MongoDatabase { +extension QueryBuilder where Database == MongoDatabase { @discardableResult public func filter(_ value: FluentMongoFilterOperator) -> Self { diff --git a/Sources/FluentMongo/QueryBuilder+Options.swift b/Sources/FluentMongo/QueryBuilder+Options.swift index 73e90ad..324fa9c 100644 --- a/Sources/FluentMongo/QueryBuilder+Options.swift +++ b/Sources/FluentMongo/QueryBuilder+Options.swift @@ -10,7 +10,7 @@ import Foundation import Fluent import MongoSwift -public extension QueryBuilder where Database.Query == FluentMongoQuery { +extension QueryBuilder where Database.Query == FluentMongoQuery { /// Enables writing to temporary files. When set to true, aggregation stages can write data to the _tmp subdirectory in the dbPath directory. @discardableResult diff --git a/Sources/FluentMongo/QueryBuilder+Update.swift b/Sources/FluentMongo/QueryBuilder+Update.swift index ff3a2e2..ed7e94a 100644 --- a/Sources/FluentMongo/QueryBuilder+Update.swift +++ b/Sources/FluentMongo/QueryBuilder+Update.swift @@ -9,7 +9,7 @@ import Foundation import Fluent -public extension QueryBuilder where Database == MongoDatabase, Result: Model { +extension QueryBuilder where Database == MongoDatabase, Result: Model { /// Adds elements to an array only if they do not already exist in the set. public func update(_ field: KeyPath, addToSet values: C) -> Self where C: Collection & Encodable, C.Element: Encodable { diff --git a/Tests/FluentMongoTests/FluentMongoProviderTests.swift b/Tests/FluentMongoTests/FluentMongoProviderTests.swift index 0eefdc4..eb70eb3 100755 --- a/Tests/FluentMongoTests/FluentMongoProviderTests.swift +++ b/Tests/FluentMongoTests/FluentMongoProviderTests.swift @@ -53,7 +53,7 @@ class FluentMongoProviderTests: XCTestCase { database: "vapor_database" ) - try MongoClient(connectionString: config.connectionURL.absoluteString).db(config.database).drop() + try MongoClient(config.connectionURL.absoluteString).db(config.database).drop() self.database = MongoDatabase(config: config) self.benchmarker = try Benchmarker(self.database, on: eventLoop, onFail: XCTFail) } catch { @@ -158,7 +158,12 @@ class FluentMongoProviderTests: XCTestCase { let r0 = try User.query(on: conn).filter(\.nicknames, .equal, ["a"]).all().wait() XCTAssertEqual(r0.count, 0) - let r1 = try User.query(on: conn).filter(\.nicknames, .equal, ["a", "b", "c"]).all().wait() + let r1 = try User.query(on: conn).group(.and) { group in + group.filter(\.nicknames, .inSubset, ["a"]) + group.filter(\.nicknames, .inSubset, ["b"]) + group.filter(\.nicknames, .inSubset, ["c"]) + }.all().wait() + XCTAssertEqual(r1.count, 1) XCTAssertEqual(r1.first?.name, "User1")