From f7310471bf46ea56ca6d483f921a221b407af36a Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 12 Aug 2020 19:09:43 -0400 Subject: [PATCH] Use SPM for MongoSwift --- MongoView.xcodeproj/project.pbxproj | 76 +++++++------------ .../contents.xcworkspacedata | 3 - .../xcshareddata/swiftpm/Package.resolved | 34 +++++++++ MongoView/ExtendedJSON.swift | 6 +- MongoView/MongoController.swift | 2 +- MongoView/Node.swift | 38 +++++----- .../EditDocumentViewController.swift | 7 +- .../QueryViewController.swift | 33 +++++--- .../EditDocumentWindowController.swift | 4 +- 9 files changed, 110 insertions(+), 93 deletions(-) create mode 100644 MongoView.xcworkspace/xcshareddata/swiftpm/Package.resolved diff --git a/MongoView.xcodeproj/project.pbxproj b/MongoView.xcodeproj/project.pbxproj index f2c5b94..135b5ec 100644 --- a/MongoView.xcodeproj/project.pbxproj +++ b/MongoView.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 50; + objectVersion = 52; objects = { /* Begin PBXBuildFile section */ @@ -24,27 +24,10 @@ D627CE8924E399F100C39FE5 /* DetailViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = D627CE8724E399F100C39FE5 /* DetailViewController.xib */; }; D627CE8B24E438EE00C39FE5 /* DatabaseCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = D627CE8A24E438EE00C39FE5 /* DatabaseCollection.swift */; }; D627CE8D24E4478800C39FE5 /* JSONPrettyPrinter.swift in Sources */ = {isa = PBXBuildFile; fileRef = D627CE8C24E4478800C39FE5 /* JSONPrettyPrinter.swift */; }; + D627CE9024E4A9F100C39FE5 /* MongoSwift in Frameworks */ = {isa = PBXBuildFile; productRef = D627CE8F24E4A9F100C39FE5 /* MongoSwift */; }; D63CDEBE23C837DC0012D658 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D63CDEBD23C837DC0012D658 /* AppDelegate.swift */; }; D63CDEC023C837DD0012D658 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D63CDEBF23C837DD0012D658 /* Assets.xcassets */; }; D63CDEC323C837DD0012D658 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = D63CDEC123C837DD0012D658 /* MainMenu.xib */; }; - D63CDF1F23C837F10012D658 /* CLibMongoC.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1623C837F10012D658 /* CLibMongoC.framework */; }; - D63CDF2023C837F10012D658 /* CLibMongoC.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1623C837F10012D658 /* CLibMongoC.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; - D63CDF2123C837F10012D658 /* CNIOAtomics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1723C837F10012D658 /* CNIOAtomics.framework */; }; - D63CDF2223C837F10012D658 /* CNIOAtomics.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1723C837F10012D658 /* CNIOAtomics.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; - D63CDF2323C837F10012D658 /* CNIODarwin.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1823C837F10012D658 /* CNIODarwin.framework */; }; - D63CDF2423C837F10012D658 /* CNIODarwin.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1823C837F10012D658 /* CNIODarwin.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; - D63CDF2523C837F10012D658 /* CNIOLinux.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1923C837F10012D658 /* CNIOLinux.framework */; }; - D63CDF2623C837F10012D658 /* CNIOLinux.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1923C837F10012D658 /* CNIOLinux.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; - D63CDF2723C837F10012D658 /* CNIOSHA1.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1A23C837F10012D658 /* CNIOSHA1.framework */; }; - D63CDF2823C837F10012D658 /* CNIOSHA1.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1A23C837F10012D658 /* CNIOSHA1.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; - D63CDF2923C837F10012D658 /* MongoSwift.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1B23C837F10012D658 /* MongoSwift.framework */; }; - D63CDF2A23C837F10012D658 /* MongoSwift.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1B23C837F10012D658 /* MongoSwift.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; - D63CDF2B23C837F10012D658 /* MongoSwiftSync.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1C23C837F10012D658 /* MongoSwiftSync.framework */; }; - D63CDF2C23C837F10012D658 /* MongoSwiftSync.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1C23C837F10012D658 /* MongoSwiftSync.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; - D63CDF2D23C837F10012D658 /* NIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1D23C837F10012D658 /* NIO.framework */; }; - D63CDF2E23C837F10012D658 /* NIO.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1D23C837F10012D658 /* NIO.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; - D63CDF2F23C837F10012D658 /* NIOConcurrencyHelpers.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1E23C837F10012D658 /* NIOConcurrencyHelpers.framework */; }; - D63CDF3023C837F10012D658 /* NIOConcurrencyHelpers.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D63CDF1E23C837F10012D658 /* NIOConcurrencyHelpers.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; D63CDF3723C8381A0012D658 /* Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = D63CDF3323C838190012D658 /* Node.swift */; }; D63CDF3823C8381A0012D658 /* MongoController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D63CDF3423C838190012D658 /* MongoController.swift */; }; D63CDF3C23C838470012D658 /* DatabaseWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D63CDF3A23C838470012D658 /* DatabaseWindowController.swift */; }; @@ -61,25 +44,6 @@ /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ - D63CDF3123C837F10012D658 /* Embed Frameworks */ = { - isa = PBXCopyFilesBuildPhase; - buildActionMask = 2147483647; - dstPath = ""; - dstSubfolderSpec = 10; - files = ( - D63CDF2823C837F10012D658 /* CNIOSHA1.framework in Embed Frameworks */, - D63CDF2623C837F10012D658 /* CNIOLinux.framework in Embed Frameworks */, - D63CDF2423C837F10012D658 /* CNIODarwin.framework in Embed Frameworks */, - D63CDF2A23C837F10012D658 /* MongoSwift.framework in Embed Frameworks */, - D63CDF2223C837F10012D658 /* CNIOAtomics.framework in Embed Frameworks */, - D63CDF2023C837F10012D658 /* CLibMongoC.framework in Embed Frameworks */, - D63CDF2C23C837F10012D658 /* MongoSwiftSync.framework in Embed Frameworks */, - D63CDF3023C837F10012D658 /* NIOConcurrencyHelpers.framework in Embed Frameworks */, - D63CDF2E23C837F10012D658 /* NIO.framework in Embed Frameworks */, - ); - name = "Embed Frameworks"; - runOnlyForDeploymentPostprocessing = 0; - }; D6D13AFF2436C33D00493D97 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; @@ -144,15 +108,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - D63CDF2723C837F10012D658 /* CNIOSHA1.framework in Frameworks */, - D63CDF2523C837F10012D658 /* CNIOLinux.framework in Frameworks */, - D63CDF2323C837F10012D658 /* CNIODarwin.framework in Frameworks */, - D63CDF2923C837F10012D658 /* MongoSwift.framework in Frameworks */, - D63CDF2123C837F10012D658 /* CNIOAtomics.framework in Frameworks */, - D63CDF1F23C837F10012D658 /* CLibMongoC.framework in Frameworks */, - D63CDF2B23C837F10012D658 /* MongoSwiftSync.framework in Frameworks */, - D63CDF2F23C837F10012D658 /* NIOConcurrencyHelpers.framework in Frameworks */, - D63CDF2D23C837F10012D658 /* NIO.framework in Frameworks */, + D627CE9024E4A9F100C39FE5 /* MongoSwift in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -291,13 +247,15 @@ D63CDEB623C837DC0012D658 /* Sources */, D63CDEB723C837DC0012D658 /* Frameworks */, D63CDEB823C837DC0012D658 /* Resources */, - D63CDF3123C837F10012D658 /* Embed Frameworks */, ); buildRules = ( ); dependencies = ( ); name = MongoView; + packageProductDependencies = ( + D627CE8F24E4A9F100C39FE5 /* MongoSwift */, + ); productName = MongoView; productReference = D63CDEBA23C837DC0012D658 /* MongoView.app */; productType = "com.apple.product-type.application"; @@ -346,6 +304,9 @@ Base, ); mainGroup = D63CDEB123C837DC0012D658; + packageReferences = ( + D627CE8E24E4A9F100C39FE5 /* XCRemoteSwiftPackageReference "mongo-swift-driver" */, + ); productRefGroup = D63CDEBB23C837DC0012D658 /* Products */; projectDirPath = ""; projectRoot = ""; @@ -638,6 +599,25 @@ defaultConfigurationName = Release; }; /* End XCConfigurationList section */ + +/* Begin XCRemoteSwiftPackageReference section */ + D627CE8E24E4A9F100C39FE5 /* XCRemoteSwiftPackageReference "mongo-swift-driver" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/mongodb/mongo-swift-driver.git"; + requirement = { + kind = upToNextMinorVersion; + minimumVersion = 1.0.1; + }; + }; +/* End XCRemoteSwiftPackageReference section */ + +/* Begin XCSwiftPackageProductDependency section */ + D627CE8F24E4A9F100C39FE5 /* MongoSwift */ = { + isa = XCSwiftPackageProductDependency; + package = D627CE8E24E4A9F100C39FE5 /* XCRemoteSwiftPackageReference "mongo-swift-driver" */; + productName = MongoSwift; + }; +/* End XCSwiftPackageProductDependency section */ }; rootObject = D63CDEB223C837DC0012D658 /* Project object */; } diff --git a/MongoView.xcworkspace/contents.xcworkspacedata b/MongoView.xcworkspace/contents.xcworkspacedata index e3c83be..a341797 100644 --- a/MongoView.xcworkspace/contents.xcworkspacedata +++ b/MongoView.xcworkspace/contents.xcworkspacedata @@ -4,7 +4,4 @@ - - diff --git a/MongoView.xcworkspace/xcshareddata/swiftpm/Package.resolved b/MongoView.xcworkspace/xcshareddata/swiftpm/Package.resolved new file mode 100644 index 0000000..c024633 --- /dev/null +++ b/MongoView.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -0,0 +1,34 @@ +{ + "object": { + "pins": [ + { + "package": "mongo-swift-driver", + "repositoryURL": "https://github.com/mongodb/mongo-swift-driver.git", + "state": { + "branch": null, + "revision": "ec67468132743919e90a34a76073afcc4a13355e", + "version": "1.0.1" + } + }, + { + "package": "Nimble", + "repositoryURL": "https://github.com/Quick/Nimble.git", + "state": { + "branch": null, + "revision": "2b1809051b4a65c1d7f5233331daa24572cd7fca", + "version": "8.1.1" + } + }, + { + "package": "swift-nio", + "repositoryURL": "https://github.com/apple/swift-nio", + "state": { + "branch": null, + "revision": "acf5465b5e7fb9aeda54a34d16fb44c31a399715", + "version": "2.20.2" + } + } + ] + }, + "version": 1 +} diff --git a/MongoView/ExtendedJSON.swift b/MongoView/ExtendedJSON.swift index 9b706b5..b7721d7 100644 --- a/MongoView/ExtendedJSON.swift +++ b/MongoView/ExtendedJSON.swift @@ -30,9 +30,9 @@ struct ExtendedJSON { return context.evaluateScript("JSON.stringify(\(string))")?.toString() } - private static func fromExtJSON(_ string: String) -> Document? { + private static func fromExtJSON(_ string: String) -> BSONDocument? { do { - let doc = try Document(fromJSON: string) + let doc = try BSONDocument(fromJSON: string) return doc } catch { print("Unable to create document from extended JSON: \(error)") @@ -40,7 +40,7 @@ struct ExtendedJSON { } } - static func toDocument(_ string: String) -> Document? { + static func toDocument(_ string: String) -> BSONDocument? { guard let normalized = normalize(string), let doc = fromExtJSON(normalized) else { return nil diff --git a/MongoView/MongoController.swift b/MongoView/MongoController.swift index 11361b1..2b038e3 100644 --- a/MongoView/MongoController.swift +++ b/MongoView/MongoController.swift @@ -54,7 +54,7 @@ class MongoController { return client.db(collection.database) } - func collection(_ collection: DatabaseCollection) -> MongoCollection { + func collection(_ collection: DatabaseCollection) -> MongoCollection { return db(for: collection).collection(collection.name) } diff --git a/MongoView/Node.swift b/MongoView/Node.swift index cba7a80..ac87ab1 100644 --- a/MongoView/Node.swift +++ b/MongoView/Node.swift @@ -45,16 +45,16 @@ class Node: NSObject { if key == nil, case let .document(doc) = value, - case let .objectId(id) = doc["_id"] { - self.key = .objectId(id) + case let .objectID(id) = doc["_id"] { + self.key = .objectID(id) } else { self.key = key } } - convenience init(document: Document) { - if case let .objectId(id) = document["_id"] { - self.init(key: .objectId(id), value: .document(document)) + convenience init(document: BSONDocument) { + if case let .objectID(id) = document["_id"] { + self.init(key: .objectID(id), value: .document(document)) } else { self.init(key: nil, value: .document(document)) } @@ -77,7 +77,7 @@ extension Node { enum Key: Equatable, Hashable { case index(Int) case name(String) - case objectId(ObjectId) + case objectID(BSONObjectID) } } @@ -97,7 +97,7 @@ extension Node { return index.description case let .name(name): return name - case let .objectId(id): + case let .objectID(id): return id.description } } @@ -113,9 +113,7 @@ extension Node { case let .array(array): return "(\(array.count) element\(array.count == 1 ? "" : "s"))" case let .binary(value): - switch Binary.Subtype(rawValue: value.subtype) { - case nil: - return "(unknown binary data)" + switch value.subtype { case .generic: return "(generic binary data)" case .function: @@ -125,15 +123,15 @@ extension Node { case .uuidDeprecated: fallthrough case .uuid: - return try! UUID(from: value).description + return try! value.toUUID().description case .md5: return "(MD5 binary data)" - case .userDefined: - return "(user defined binary data)" + default: + return "(unknown binary data))" } case .undefined: return "undefined" - case let .objectId(value): + case let .objectID(value): return value.description case let .bool(value): return value.description @@ -142,7 +140,7 @@ extension Node { case .null: return "null" case let .regex(value): - return try! NSRegularExpression(from: value).description + return value.pattern case let .dbPointer(value): return "\(value.ref)(\(value.id))" case let .symbol(value): @@ -178,9 +176,7 @@ extension Node { case .array(_): return "Array" case let .binary(value): - switch Binary.Subtype(rawValue: value.subtype) { - case nil: - return "Unknown binary data" + switch value.subtype { case .generic: return "Generic binary data" case .function: @@ -193,12 +189,12 @@ extension Node { return "UUID" case .md5: return "MD5 hash" - case .userDefined: - return "User defined binary data" + default: + return "Unknown binary data" } case .undefined: return "Undefined" - case .objectId(_): + case .objectID(_): return "ObjectId" case .bool(_): return "Bool" diff --git a/MongoView/View Controllers/EditDocumentViewController.swift b/MongoView/View Controllers/EditDocumentViewController.swift index 7f9428f..bd09964 100644 --- a/MongoView/View Controllers/EditDocumentViewController.swift +++ b/MongoView/View Controllers/EditDocumentViewController.swift @@ -13,7 +13,7 @@ class EditDocumentViewController: NSViewController { private(set) var mongoController: MongoController! private(set) var collection: DatabaseCollection! - private(set) var document: Document! + private(set) var document: BSONDocument! var documentEdited: (() -> Void)? @@ -22,7 +22,7 @@ class EditDocumentViewController: NSViewController { @IBOutlet weak var cancelButton: NSButton! @IBOutlet weak var validateButton: NSButton! - init(mongoController: MongoController, collection: DatabaseCollection, document: Document) { + init(mongoController: MongoController, collection: DatabaseCollection, document: BSONDocument) { self.mongoController = mongoController self.collection = collection self.document = document @@ -38,7 +38,8 @@ class EditDocumentViewController: NSViewController { super.viewDidLoad() let printer = JSONPrettyPrinter(options: .convertMongoObjects) - editorTextView.string = (try? printer.prettify(document.extendedJSON)) ?? document.extendedJSON + let extended = document.toExtendedJSONString() + editorTextView.string = (try? printer.prettify(extended)) ?? extended editorTextView.isAutomaticQuoteSubstitutionEnabled = false } diff --git a/MongoView/View Controllers/QueryViewController.swift b/MongoView/View Controllers/QueryViewController.swift index 375219f..7a565f6 100644 --- a/MongoView/View Controllers/QueryViewController.swift +++ b/MongoView/View Controllers/QueryViewController.swift @@ -8,6 +8,7 @@ import Cocoa import MongoSwift +import NIO class QueryViewController: NSViewController { @@ -70,7 +71,7 @@ class QueryViewController: NSViewController { func refresh(reload: Bool = true) { let filterText = filterTextView.string.trimmingCharacters(in: .whitespacesAndNewlines) - let filter: Document + let filter: BSONDocument if !filterText.isEmpty, let doc = ExtendedJSON.toDocument(filterText) { filter = doc @@ -78,17 +79,25 @@ class QueryViewController: NSViewController { filter = [:] } - let documents = try! mongoController.collection(collection).find(filter).all() - rootNodes = documents.map { Node(document: $0) } - - title = "\(self.collection.database).\(self.collection.name)" - documentCountLabel.stringValue = "\(documents.count) document\(documents.count == 1 ? "" : "s")" + mongoController.statusManager.set("Querying \(collection)...", for: .query, override: true) - if reload { - outlineView.reloadData() + let collection = mongoController.collection(self.collection) + collection.find(filter).flatMap { (cursor: MongoCursor) -> EventLoopFuture<[BSONDocument]> in + return cursor.toArray() + }.whenSuccess { (documents) in + DispatchQueue.main.async { + self.rootNodes = documents.map { Node(document: $0) } + + self.title = "\(self.collection.database).\(self.collection.name)" + self.documentCountLabel.stringValue = "\(documents.count) document\(documents.count == 1 ? "" : "s")" + + if reload { + self.outlineView.reloadData() + } + + self.mongoController.statusManager.set("Queried \(collection)", for: .query, override: true) + } } - - mongoController.statusManager.set("Queried \(collection)", for: .query, override: true) } func deleteRootNode(_ node: Node) { @@ -97,8 +106,8 @@ class QueryViewController: NSViewController { alert.alertStyle = .warning alert.messageText = "Confirm deletion" alert.informativeText = "Are you sure you want to delete the document" - let id: ObjectId? - if case let .objectId(docId) = doc["_id"] { + let id: BSONObjectID? + if case let .objectID(docId) = doc["_id"] { id = docId alert.informativeText += " with id \(docId)" } else { diff --git a/MongoView/Windows/EditDocumentWindowController.swift b/MongoView/Windows/EditDocumentWindowController.swift index 06da4d5..23f6e1c 100644 --- a/MongoView/Windows/EditDocumentWindowController.swift +++ b/MongoView/Windows/EditDocumentWindowController.swift @@ -13,11 +13,11 @@ class EditDocumentWindowController: NSWindowController { private(set) var mongoController: MongoController! private(set) var collection: DatabaseCollection! - private(set) var mongoDocument: Document! + private(set) var mongoDocument: BSONDocument! var documentEdited: (() -> Void)? - convenience init(mongoController: MongoController, collection: DatabaseCollection, document: Document) { + convenience init(mongoController: MongoController, collection: DatabaseCollection, document: BSONDocument) { self.init(windowNibName: "EditDocumentWindowController") self.mongoController = mongoController