Use SPM for MongoSwift

This commit is contained in:
Shadowfacts 2020-08-12 19:09:43 -04:00
parent e40e8d662f
commit f7310471bf
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
9 changed files with 110 additions and 93 deletions

View File

@ -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 */;
}

View File

@ -4,7 +4,4 @@
<FileRef
location = "container:MongoView.xcodeproj">
</FileRef>
<FileRef
location = "group:../mongo-swift-driver/MongoSwift.xcodeproj">
</FileRef>
</Workspace>

View File

@ -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
}

View File

@ -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

View File

@ -54,7 +54,7 @@ class MongoController {
return client.db(collection.database)
}
func collection(_ collection: DatabaseCollection) -> MongoCollection<Document> {
func collection(_ collection: DatabaseCollection) -> MongoCollection<BSONDocument> {
return db(for: collection).collection(collection.name)
}

View File

@ -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"

View File

@ -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
}

View File

@ -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) }
mongoController.statusManager.set("Querying \(collection)...", for: .query, override: true)
title = "\(self.collection.database).\(self.collection.name)"
documentCountLabel.stringValue = "\(documents.count) document\(documents.count == 1 ? "" : "s")"
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 {
outlineView.reloadData()
self.outlineView.reloadData()
}
mongoController.statusManager.set("Queried \(collection)", for: .query, override: true)
self.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 {

View File

@ -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