From ae5f737baf2ae34ab483dce5ab41a06425a35e3b Mon Sep 17 00:00:00 2001 From: John Rommel Estropia Date: Sat, 7 May 2016 12:01:31 +0800 Subject: [PATCH] WIP: logging clauses --- CoreStoreDemo/CoreStoreDemo/AppDelegate.swift | 7 +- .../Concrete Clauses/From.swift | 14 +- ...reStore+CustomDebugStringConvertible.swift | 151 ++++++++++++++---- 3 files changed, 137 insertions(+), 35 deletions(-) diff --git a/CoreStoreDemo/CoreStoreDemo/AppDelegate.swift b/CoreStoreDemo/CoreStoreDemo/AppDelegate.swift index 6cb6bad..73209b2 100644 --- a/CoreStoreDemo/CoreStoreDemo/AppDelegate.swift +++ b/CoreStoreDemo/CoreStoreDemo/AppDelegate.swift @@ -24,11 +24,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate { application.statusBarStyle = .LightContent - - print(CoreStoreError.MappingModelNotFound(localStoreURL: NSURL(string: "file://sample.db")!, targetModel: NSManagedObjectModel.mergedModelFromBundles(nil)!, targetModelVersion: "Sample-1.0.0")) - CoreStore.defaultStack = DataStack(migrationChain: ["Sample-1.0.0": "Sample-1.0.2", "Sample-1.0.1": "Sample-1.0.2"]) - print(CoreStore.defaultStack) - print(CoreStore.beginUnsafe()) + print(CoreStore.beginUnsafe().commitAndWait()) + print(From("Config1", "Config2")) return true } diff --git a/Sources/Fetching and Querying/Concrete Clauses/From.swift b/Sources/Fetching and Querying/Concrete Clauses/From.swift index f4111ca..da41746 100644 --- a/Sources/Fetching and Querying/Concrete Clauses/From.swift +++ b/Sources/Fetching and Querying/Concrete Clauses/From.swift @@ -87,7 +87,7 @@ public struct From { - parameter configuration: the `NSPersistentStore` configuration name to associate objects from. This parameter is required if multiple configurations contain the created `NSManagedObject`'s entity type. Set to `nil` to use the default configuration. - parameter otherConfigurations: an optional list of other configuration names to associate objects from (see `configuration` parameter) */ - public init(_ configuration: String?, otherConfigurations: String?...) { + public init(_ configuration: String?, _ otherConfigurations: String?...) { self.init(entityClass: T.self, configurations: [configuration] + otherConfigurations) } @@ -300,6 +300,9 @@ public struct From { // MARK: Internal + internal let entityClass: AnyClass + internal let dumpInfo: (key: String, value: Any)? + internal func applyToFetchRequest(fetchRequest: NSFetchRequest, context: NSManagedObjectContext, applyAffectedStores: Bool = true) { fetchRequest.entity = context.entityDescriptionForEntityClass(self.entityClass) @@ -320,6 +323,7 @@ public struct From { return From( entityClass: self.entityClass, + dumpInfo: self.dumpInfo, findPersistentStores: self.findPersistentStores ) } @@ -327,13 +331,13 @@ public struct From { // MARK: Private - private let entityClass: AnyClass private let findPersistentStores: (context: NSManagedObjectContext) -> [NSPersistentStore]? private init(entityClass: AnyClass) { self.init( entityClass: entityClass, + dumpInfo: nil, findPersistentStores: { (context: NSManagedObjectContext) -> [NSPersistentStore]? in return context.parentStack?.persistentStoresForEntityClass(entityClass) @@ -346,6 +350,7 @@ public struct From { let configurationsSet = Set(configurations.map { $0 ?? Into.defaultConfigurationName }) self.init( entityClass: entityClass, + dumpInfo: ("configurations", configurations), findPersistentStores: { (context: NSManagedObjectContext) -> [NSPersistentStore]? in return context.parentStack?.persistentStoresForEntityClass(entityClass)?.filter { @@ -361,6 +366,7 @@ public struct From { let storeURLsSet = Set(storeURLs) self.init( entityClass: entityClass, + dumpInfo: ("storeURLs", storeURLs), findPersistentStores: { (context: NSManagedObjectContext) -> [NSPersistentStore]? in return context.parentStack?.persistentStoresForEntityClass(entityClass)?.filter { @@ -376,6 +382,7 @@ public struct From { let persistentStores = Set(persistentStores) self.init( entityClass: entityClass, + dumpInfo: ("persistentStores", persistentStores), findPersistentStores: { (context: NSManagedObjectContext) -> [NSPersistentStore]? in return context.parentStack?.persistentStoresForEntityClass(entityClass)?.filter { @@ -386,9 +393,10 @@ public struct From { ) } - private init(entityClass: AnyClass, findPersistentStores: (context: NSManagedObjectContext) -> [NSPersistentStore]?) { + private init(entityClass: AnyClass, dumpInfo: (key: String, value: Any)?, findPersistentStores: (context: NSManagedObjectContext) -> [NSPersistentStore]?) { self.entityClass = entityClass + self.dumpInfo = dumpInfo self.findPersistentStores = findPersistentStores } } diff --git a/Sources/Logging/CoreStore+CustomDebugStringConvertible.swift b/Sources/Logging/CoreStore+CustomDebugStringConvertible.swift index 7038695..f401817 100644 --- a/Sources/Logging/CoreStore+CustomDebugStringConvertible.swift +++ b/Sources/Logging/CoreStore+CustomDebugStringConvertible.swift @@ -87,8 +87,11 @@ extension CloudStorageOptions: CustomDebugStringConvertible, IndentableDebugStri return "[.\(flags[0])]" default: - let description = "[\n" + flags.joinWithSeparator(",\n") - return description.indent(1) + "\n]" + var string = "[\n" + string.appendContentsOf(flags.joinWithSeparator(",\n")) + string.indent(1) + string.appendContentsOf("\n") + return string } } } @@ -178,6 +181,32 @@ extension DataStack: CustomDebugStringConvertible, IndentableDebugStringConverti } +// MARK: - From + +extension From: CustomDebugStringConvertible, IndentableDebugStringConvertible { + + // MARK: CustomDebugStringConvertible + + public var debugDescription: String { + + return self.cs_dump() + } + + + // MARK: IndentableDebugStringConvertible + + private var cs_dumpInfo: DumpInfo { + + var dumpInfo: DumpInfo = [("entityClass", self.entityClass)] + if let extraInfo = self.dumpInfo { + + dumpInfo.append(extraInfo) + } + return dumpInfo + } +} + + // MARK: - ICloudStore extension ICloudStore: CustomDebugStringConvertible, IndentableDebugStringConvertible { @@ -318,8 +347,11 @@ extension LocalStorageOptions: CustomDebugStringConvertible, IndentableDebugStri return "[.\(flags[0])]" default: - let description = "[\n" + flags.joinWithSeparator(",\n") - return description.indent(1) + "\n]" + var string = "[\n" + string.appendContentsOf(flags.joinWithSeparator(",\n")) + string.indent(1) + string.appendContentsOf("\n]") + return string } } } @@ -366,17 +398,53 @@ extension MigrationChain: CustomDebugStringConvertible, IndentableDebugStringCon return "[\(paths[0])]" default: - var dump = "[" + var string = "[" paths.forEach { - dump.appendContentsOf("\n\($0);") + string.appendContentsOf("\n\($0);") } - return dump.indent(1) + "\n]" + string.indent(1) + string.appendContentsOf("\n]") + return string } } } +// MARK: - SaveResult + +extension SaveResult: CustomDebugStringConvertible, IndentableDebugStringConvertible { + + // MARK: CustomDebugStringConvertible + + public var debugDescription: String { + + return self.cs_dump() + } + + + // MARK: IndentableDebugStringConvertible + + private var cs_dumpValue: String { + + var string: String + switch self { + + case .Success(let hasChanges): + string = ".Success (" + string.appendDumpInfo("hasChanges", hasChanges) + + case .Failure(let error): + string = ".Failure (" + string.appendDumpInfo("error", error) + } + string.indent(1) + string.appendContentsOf("\n)") + return string + } +} + + // MARK: - SQLiteStore extension SQLiteStore: CustomDebugStringConvertible, IndentableDebugStringConvertible { @@ -489,9 +557,9 @@ private extension String { return self } - private func indent(level: Int) -> String { + private mutating func indent(level: Int) { - return self.stringByReplacingOccurrencesOfString("\n", withString: "\n\(String.indention(level))") + self = self.stringByReplacingOccurrencesOfString("\n", withString: "\n\(String.indention(level))") } private mutating func appendDumpInfo(key: String, _ value: Any) { @@ -540,23 +608,25 @@ private extension IndentableDebugStringConvertible { let value = self.cs_dumpValue let info = self.cs_dumpInfo - var dump = "(\(self.dynamicType.cs_typeString()))" + var string = "(\(self.dynamicType.cs_typeString()))" if !value.isEmpty { - dump.appendContentsOf(" \(value)") + string.appendContentsOf(" \(value)") } if info.isEmpty { - return dump + return string } else { - dump.appendContentsOf(" {") + string.appendContentsOf(" (") info.forEach { - dump.appendDumpInfo($0, $1) + string.appendDumpInfo($0, $1) } - return dump.indent(1) + "\n}" + string.indent(1) + string.appendContentsOf("\n)") + return string } } } @@ -580,7 +650,9 @@ extension Array: IndentableDebugStringConvertible { string.appendContentsOf("\n\(index) = \(formattedValue(item));") } - return string.indent(1) + "\n]" + string.indent(1) + string.appendContentsOf("\n]") + return string } } } @@ -601,7 +673,9 @@ extension Dictionary: IndentableDebugStringConvertible { string.appendContentsOf("\n\(formattedValue(key)) = \(formattedValue(value));") } - return string.indent(1) + "\n]" + string.indent(1) + string.appendContentsOf("\n]") + return string } } } @@ -610,7 +684,7 @@ extension NSAttributeDescription: IndentableDebugStringConvertible { private var cs_dumpValue: String { - var string = "{" + var string = "(" string.appendDumpInfo("attributeType", self.attributeType) string.appendDumpInfo("attributeValueClassName", self.attributeValueClassName) @@ -630,7 +704,9 @@ extension NSAttributeDescription: IndentableDebugStringConvertible { string.appendDumpInfo("storedInExternalRecord", self.storedInExternalRecord) string.appendDumpInfo("renamingIdentifier", self.renamingIdentifier) - return string.indent(1) + "\n}" + string.indent(1) + string.appendContentsOf("\n)") + return string } } @@ -683,7 +759,7 @@ extension NSEntityDescription: IndentableDebugStringConvertible { private var cs_dumpValue: String { - var string = "{" + var string = "(" string.appendDumpInfo("managedObjectClassName", self.managedObjectClassName!) string.appendDumpInfo("name", self.name) string.appendDumpInfo("abstract", self.abstract) @@ -699,7 +775,9 @@ extension NSEntityDescription: IndentableDebugStringConvertible { string.appendDumpInfo("uniquenessConstraints", self.uniquenessConstraints) } - return string.indent(1) + "\n}" + string.indent(1) + string.appendContentsOf("\n)") + return string } } @@ -707,22 +785,39 @@ extension NSError: IndentableDebugStringConvertible { private var cs_dumpValue: String { - var string = "{" + var string = "(" string.appendDumpInfo("domain", self.domain) string.appendDumpInfo("code", self.code) string.appendDumpInfo("userInfo", self.userInfo) - return string.indent(1) + "\n}" + string.indent(1) + string.appendContentsOf("\n)") + return string } } +//extension NSManagedObject: IndentableDebugStringConvertible { +// +// private var cs_dumpValue: String { +// +// // TODO: +// var string = "(" +// // string.appendDumpInfo("self", self) +// string.indent(1) +// string.appendContentsOf("\n)") +// return string +// } +//} + extension NSManagedObjectModel: IndentableDebugStringConvertible { private var cs_dumpValue: String { - var string = "{" + var string = "(" string.appendDumpInfo("configurations", self.configurations) string.appendDumpInfo("entities", self.entities) - return string.indent(1) + "\n}" + string.indent(1) + string.appendContentsOf("\n)") + return string } } @@ -738,7 +833,7 @@ extension NSRelationshipDescription: IndentableDebugStringConvertible { private var cs_dumpValue: String { - var string = "{" + var string = "(" string.appendDumpInfo("destinationEntity", self.destinationEntity?.name) string.appendDumpInfo("inverseRelationship", self.inverseRelationship?.name) @@ -760,7 +855,9 @@ extension NSRelationshipDescription: IndentableDebugStringConvertible { string.appendDumpInfo("storedInExternalRecord", self.storedInExternalRecord) string.appendDumpInfo("renamingIdentifier", self.renamingIdentifier) - return string.indent(1) + "\n}" + string.indent(1) + string.appendContentsOf("\n)") + return string } }