diff --git a/CHANGELOG.md b/CHANGELOG.md index db04f93..b11f044 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# 3.0.0 +1. Separated Metadata from Objects (removed `metadata` from `Persistable`) +2. Updated for Swift 3.0 +3. Pod name changed to RCSYapDatabaseExtensions +4. Use `map` instead of `flatMap` when reading multiple objects so the order of the output corresponds to the order of the input. + # 2.6.0 Updated for Swift 2.3 - sorry it's taken so long! diff --git a/Cartfile b/Cartfile index 034481a..00e0c1c 100644 --- a/Cartfile +++ b/Cartfile @@ -1,3 +1,2 @@ -github "danthorpe/ValueCoding" "1.5.0" -github "CocoaLumberjack/CocoaLumberjack" "2.4.0" -github "yapstudios/YapDatabase" "2.9.2" \ No newline at end of file +github "danthorpe/ValueCoding" "2.1.0" +github "yapstudios/YapDatabase" "2.9.3" \ No newline at end of file diff --git a/Cartfile.resolved b/Cartfile.resolved index 8ae8b91..f10e04c 100644 --- a/Cartfile.resolved +++ b/Cartfile.resolved @@ -1,4 +1,3 @@ -github "CocoaLumberjack/CocoaLumberjack" "2.4.0" -github "tonymillion/Reachability" "c4d225a479379f9998dbd5b55070427373d20574" -github "danthorpe/ValueCoding" "1.5.0" -github "yapstudios/YapDatabase" "2.9.2" +github "CocoaLumberjack/CocoaLumberjack" "3.0.0" +github "danthorpe/ValueCoding" "2.1.0" +github "yapstudios/YapDatabase" "2.9.3" diff --git a/Gemfile.lock b/Gemfile.lock index b7cdf05..911cca6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -55,9 +55,9 @@ GEM netrc (= 0.7.8) cocoapods-try (1.1.0) colored (1.2) - commander (4.4.1) + commander (4.4.3) highline (~> 1.7.2) - credentials_manager (0.16.2) + credentials_manager (0.16.4) colored commander (>= 4.3.5) highline (>= 1.7.1) @@ -299,9 +299,10 @@ PLATFORMS DEPENDENCIES cocoapods cocoapods-keys + credentials_manager (= 0.16.4) fastlane slather xcpretty BUNDLED WITH - 1.13.6 + 1.13.7 diff --git a/YapDatabaseExtensions.podspec b/RCSYapDatabaseExtensions.podspec similarity index 50% rename from YapDatabaseExtensions.podspec rename to RCSYapDatabaseExtensions.podspec index c2da953..013359a 100644 --- a/YapDatabaseExtensions.podspec +++ b/RCSYapDatabaseExtensions.podspec @@ -1,30 +1,30 @@ Pod::Spec.new do |s| - s.name = "YapDatabaseExtensions" - s.version = "2.6.0" + s.name = "RCSYapDatabaseExtensions" + s.version = "3.0.0" s.summary = "Helpers for using value types with YapDatabase." s.description = <<-DESC Defines APIs to conveniently read, write and remove objects and values to or from YapDatabse. See ValueCoding for value type support. + + Fork of danthorpe's YapDatabaseExtensions with Swift 3 support and + metadata separated from objects. DESC - s.homepage = "https://github.com/danthorpe/YapDatabaseExtensions" + s.homepage = "https://github.com/JimRoepcke/YapDatabaseExtensions" s.license = 'MIT' - s.author = { "Daniel Thorpe" => "@danthorpe" } - s.source = { :git => "https://github.com/danthorpe/YapDatabaseExtensions.git", :tag => s.version.to_s } - s.module_name = 'YapDatabaseExtensions' - s.social_media_url = 'https://twitter.com/danthorpe' + s.authors = { "Daniel Thorpe" => "@danthorpe", "Jim Roepcke" => "@JimRoepcke" } + s.source = { :git => "https://github.com/JimRoepcke/YapDatabaseExtensions.git", :tag => s.version.to_s } + s.module_name = 'RCSYapDatabaseExtensions' + s.social_media_url = 'https://twitter.com/JimRoepcke' s.requires_arc = true s.default_subspec = 'Persitable' - s.ios.deployment_target = '8.0' - s.osx.deployment_target = '10.10' + s.ios.deployment_target = '9.0' + s.osx.deployment_target = '10.11' - s.dependency 'ValueCoding', '~> 1.5' + s.dependency 'ValueCoding', '~> 2.1.0' s.dependency 'YapDatabase', '2.9.2' - # Ensure the correct version of Swift is used - s.pod_target_xcconfig = { 'SWIFT_VERSION' => '2.3' } - s.subspec 'Core' do |ss| ss.source_files = [ 'Sources/YapDatabaseExtensions.swift', @@ -33,12 +33,12 @@ Pod::Spec.new do |s| end s.subspec 'Functional' do |ss| - ss.dependency 'YapDatabaseExtensions/Core' + ss.dependency 'RCSYapDatabaseExtensions/Core' ss.source_files = 'Sources/Functional_*.swift' end s.subspec 'Persitable' do |ss| - ss.dependency 'YapDatabaseExtensions/Functional' + ss.dependency 'RCSYapDatabaseExtensions/Functional' ss.source_files = [ 'Sources/Read.swift', 'Sources/Persistable_*.swift' diff --git a/Sources/Curried_ObjectWithNoMetadata.swift b/Sources/Curried_ObjectWithNoMetadata.swift index 4ec7b60..6429cb5 100644 --- a/Sources/Curried_ObjectWithNoMetadata.swift +++ b/Sources/Curried_ObjectWithNoMetadata.swift @@ -22,8 +22,8 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readAtIndex< - ReadTransaction where - ReadTransaction: ReadTransactionType>(index: YapDB.Index) -> ReadTransaction -> Self? { + ReadTransaction>(_ index: YapDB.Index) -> (ReadTransaction) -> Self? where + ReadTransaction: ReadTransactionType { return { $0.readAtIndex(index) } } @@ -35,10 +35,10 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readAtIndexes< - Indexes, ReadTransaction where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, - ReadTransaction: ReadTransactionType>(indexes: Indexes) -> ReadTransaction -> [Self] { + Indexes, ReadTransaction>(_ indexes: Indexes) -> (ReadTransaction) -> [Self?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, + ReadTransaction: ReadTransactionType { return { $0.readAtIndexes(indexes) } } @@ -50,8 +50,8 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readByKey< - ReadTransaction where - ReadTransaction: ReadTransactionType>(key: String) -> ReadTransaction -> Self? { + ReadTransaction>(_ key: String) -> (ReadTransaction) -> Self? where + ReadTransaction: ReadTransactionType { return { $0.readByKey(key) } } @@ -63,10 +63,10 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readByKeys< - Keys, ReadTransaction where - Keys: SequenceType, - Keys.Generator.Element == String, - ReadTransaction: ReadTransactionType>(keys: Keys) -> ReadTransaction -> [Self] { + Keys, ReadTransaction>(_ keys: Keys) -> (ReadTransaction) -> [Self?] where + Keys: Sequence, + Keys.Iterator.Element == String, + ReadTransaction: ReadTransactionType { return { $0.readAtIndexes(Self.indexesWithKeys(keys)) } } @@ -77,7 +77,7 @@ extension Persistable where - warning: Be aware that this will capure `self`. - returns: a (WriteTransaction) -> Self closure */ - public func write() -> WriteTransaction -> Self { + public func write() -> (WriteTransaction) -> Self { return { $0.write(self) } } } diff --git a/Sources/Curried_ObjectWithObjectMetadata.swift b/Sources/Curried_ObjectWithObjectMetadata.swift index 77587cb..9ea47fe 100644 --- a/Sources/Curried_ObjectWithObjectMetadata.swift +++ b/Sources/Curried_ObjectWithObjectMetadata.swift @@ -22,9 +22,9 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readWithMetadataAtIndex< - ReadTransaction, Metadata where + ReadTransaction, Metadata>(_ index: YapDB.Index) -> (ReadTransaction) -> YapItem? where ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(index: YapDB.Index) -> ReadTransaction -> YapItem? { + Metadata: NSCoding { return { $0.readWithMetadataAtIndex(index) } } @@ -36,11 +36,11 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readWithMetadataAtIndexes< - Indexes, ReadTransaction, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, ReadTransaction, Metadata>(_ indexes: Indexes) -> (ReadTransaction) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(indexes: Indexes) -> ReadTransaction -> [YapItem] { + Metadata: NSCoding { return { $0.readWithMetadataAtIndexes(indexes) } } @@ -52,9 +52,9 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readWithMetadataByKey< - ReadTransaction, Metadata where + ReadTransaction, Metadata>(_ key: String) -> (ReadTransaction) -> YapItem? where ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(key: String) -> ReadTransaction -> YapItem? { + Metadata: NSCoding { return { $0.readWithMetadataByKey(key) } } @@ -66,11 +66,11 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readWithMetadataByKeys< - Keys, ReadTransaction, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, ReadTransaction, Metadata>(_ keys: Keys) -> (ReadTransaction) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(keys: Keys) -> ReadTransaction -> [YapItem] { + Metadata: NSCoding { return { $0.readWithMetadataAtIndexes(Self.indexesWithKeys(keys)) } } @@ -82,9 +82,9 @@ extension Persistable where - returns: a (WriteTransaction) -> Self closure */ public func writeWithMetadata< - WriteTransaction, Metadata where + WriteTransaction, Metadata>(_ metadata: Metadata? = nil) -> (WriteTransaction) -> YapItem where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(metadata: Metadata? = nil) -> WriteTransaction -> YapItem { + Metadata: NSCoding { return { $0.writeWithMetadata(YapItem(self, metadata)) } } } diff --git a/Sources/Curried_ObjectWithValueMetadata.swift b/Sources/Curried_ObjectWithValueMetadata.swift index 9dd1702..be14cda 100644 --- a/Sources/Curried_ObjectWithValueMetadata.swift +++ b/Sources/Curried_ObjectWithValueMetadata.swift @@ -22,11 +22,11 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readWithMetadataAtIndex< - ReadTransaction, Metadata where + ReadTransaction, Metadata>(_ index: YapDB.Index) -> (ReadTransaction) -> YapItem? where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> ReadTransaction -> YapItem? { + Metadata.Coder.Value == Metadata { return { $0.readWithMetadataAtIndex(index) } } @@ -38,13 +38,13 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readWithMetadataAtIndexes< - Indexes, ReadTransaction, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, ReadTransaction, Metadata>(_ indexes: Indexes) -> (ReadTransaction) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> ReadTransaction -> [YapItem] { + Metadata.Coder.Value == Metadata { return { $0.readWithMetadataAtIndexes(indexes) } } @@ -56,11 +56,11 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readWithMetadataByKey< - ReadTransaction, Metadata where + ReadTransaction, Metadata>(_ key: String) -> (ReadTransaction) -> YapItem? where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> ReadTransaction -> YapItem? { + Metadata.Coder.Value == Metadata { return { $0.readWithMetadataByKey(key) } } @@ -72,13 +72,13 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readWithMetadataByKeys< - Keys, ReadTransaction, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, ReadTransaction, Metadata>(_ keys: Keys) -> (ReadTransaction) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> ReadTransaction -> [YapItem] { + Metadata.Coder.Value == Metadata { return { $0.readWithMetadataAtIndexes(Self.indexesWithKeys(keys)) } } @@ -90,11 +90,11 @@ extension Persistable where - returns: a (WriteTransaction) -> Self closure */ public func writeWithMetadata< - WriteTransaction, Metadata where + WriteTransaction, Metadata>(_ metadata: Metadata? = nil) -> (WriteTransaction) -> YapItem where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(metadata: Metadata? = nil) -> WriteTransaction -> YapItem { + Metadata.Coder.Value == Metadata { return { $0.writeWithMetadata(YapItem(self, metadata)) } } } diff --git a/Sources/Curried_ValueWithNoMetadata.swift b/Sources/Curried_ValueWithNoMetadata.swift index 60cd00a..cf5eab8 100644 --- a/Sources/Curried_ValueWithNoMetadata.swift +++ b/Sources/Curried_ValueWithNoMetadata.swift @@ -14,7 +14,7 @@ import ValueCoding extension Persistable where Self: ValueCoding, Self.Coder: NSCoding, - Self.Coder.ValueType == Self { + Self.Coder.Value == Self { /** Returns a closure which, given a read transaction will return @@ -24,8 +24,8 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readAtIndex< - ReadTransaction where - ReadTransaction: ReadTransactionType>(index: YapDB.Index) -> ReadTransaction -> Self? { + ReadTransaction>(_ index: YapDB.Index) -> (ReadTransaction) -> Self? where + ReadTransaction: ReadTransactionType { return { $0.readAtIndex(index) } } @@ -37,10 +37,10 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readAtIndexes< - Indexes, ReadTransaction where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, - ReadTransaction: ReadTransactionType>(indexes: Indexes) -> ReadTransaction -> [Self] { + Indexes, ReadTransaction>(_ indexes: Indexes) -> (ReadTransaction) -> [Self?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, + ReadTransaction: ReadTransactionType { return { $0.readAtIndexes(indexes) } } @@ -52,8 +52,8 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readByKey< - ReadTransaction where - ReadTransaction: ReadTransactionType>(key: String) -> ReadTransaction -> Self? { + ReadTransaction>(_ key: String) -> (ReadTransaction) -> Self? where + ReadTransaction: ReadTransactionType { return { $0.readByKey(key) } } @@ -65,10 +65,10 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readByKeys< - Keys, ReadTransaction where - Keys: SequenceType, - Keys.Generator.Element == String, - ReadTransaction: ReadTransactionType>(keys: Keys) -> ReadTransaction -> [Self] { + Keys, ReadTransaction>(_ keys: Keys) -> (ReadTransaction) -> [Self?] where + Keys: Sequence, + Keys.Iterator.Element == String, + ReadTransaction: ReadTransactionType { return { $0.readAtIndexes(Self.indexesWithKeys(keys)) } } @@ -79,7 +79,7 @@ extension Persistable where - warning: Be aware that this will capure `self`. - returns: a (WriteTransaction) -> Self closure */ - public func write() -> WriteTransaction -> Self { + public func write() -> (WriteTransaction) -> Self { return { $0.write(self) } } } diff --git a/Sources/Curried_ValueWithObjectMetadata.swift b/Sources/Curried_ValueWithObjectMetadata.swift index 6ff0779..26ce801 100644 --- a/Sources/Curried_ValueWithObjectMetadata.swift +++ b/Sources/Curried_ValueWithObjectMetadata.swift @@ -14,7 +14,7 @@ import ValueCoding extension Persistable where Self: ValueCoding, Self.Coder: NSCoding, - Self.Coder.ValueType == Self { + Self.Coder.Value == Self { /** Returns a closure which, given a read transaction will return @@ -24,9 +24,9 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readWithMetadataAtIndex< - ReadTransaction, Metadata where + ReadTransaction, Metadata>(_ index: YapDB.Index) -> (ReadTransaction) -> YapItem? where ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(index: YapDB.Index) -> ReadTransaction -> YapItem? { + Metadata: NSCoding { return { $0.readWithMetadataAtIndex(index) } } @@ -38,11 +38,11 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readWithMetadataAtIndexes< - Indexes, ReadTransaction, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, ReadTransaction, Metadata>(_ indexes: Indexes) -> (ReadTransaction) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(indexes: Indexes) -> ReadTransaction -> [YapItem] { + Metadata: NSCoding { return { $0.readWithMetadataAtIndexes(indexes) } } @@ -54,9 +54,9 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readWithMetadataByKey< - ReadTransaction, Metadata where + ReadTransaction, Metadata>(_ key: String) -> (ReadTransaction) -> YapItem? where ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(key: String) -> ReadTransaction -> YapItem? { + Metadata: NSCoding { return { $0.readWithMetadataByKey(key) } } @@ -68,11 +68,11 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readWithMetadataByKeys< - Keys, ReadTransaction, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, ReadTransaction, Metadata>(_ keys: Keys) -> (ReadTransaction) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, ReadTransaction: ReadTransactionType, - Metadata: NSCoding>(keys: Keys) -> ReadTransaction -> [YapItem] { + Metadata: NSCoding { return { $0.readWithMetadataAtIndexes(Self.indexesWithKeys(keys)) } } @@ -84,9 +84,9 @@ extension Persistable where - returns: a (WriteTransaction) -> Self closure */ public func writeWithMetadata< - WriteTransaction, Metadata where + WriteTransaction, Metadata>(_ metadata: Metadata? = nil) -> (WriteTransaction) -> YapItem where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(metadata: Metadata? = nil) -> WriteTransaction -> YapItem { + Metadata: NSCoding { return { $0.writeWithMetadata(YapItem(self, metadata)) } } } diff --git a/Sources/Curried_ValueWithValueMetadata.swift b/Sources/Curried_ValueWithValueMetadata.swift index 4525175..8043070 100644 --- a/Sources/Curried_ValueWithValueMetadata.swift +++ b/Sources/Curried_ValueWithValueMetadata.swift @@ -14,7 +14,7 @@ import ValueCoding extension Persistable where Self: ValueCoding, Self.Coder: NSCoding, - Self.Coder.ValueType == Self { + Self.Coder.Value == Self { /** Returns a closure which, given a read transaction will return @@ -24,11 +24,11 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readWithMetadataAtIndex< - ReadTransaction, Metadata where + ReadTransaction, Metadata>(_ index: YapDB.Index) -> (ReadTransaction) -> YapItem? where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> ReadTransaction -> YapItem? { + Metadata.Coder.Value == Metadata { return { $0.readWithMetadataAtIndex(index) } } @@ -40,13 +40,13 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readWithMetadataAtIndexes< - Indexes, ReadTransaction, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, ReadTransaction, Metadata>(_ indexes: Indexes) -> (ReadTransaction) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> ReadTransaction -> [YapItem] { + Metadata.Coder.Value == Metadata { return { $0.readWithMetadataAtIndexes(indexes) } } @@ -58,11 +58,11 @@ extension Persistable where - returns: a (ReadTransaction) -> Self? closure. */ public static func readWithMetadataByKey< - ReadTransaction, Metadata where + ReadTransaction, Metadata>(_ key: String) -> (ReadTransaction) -> YapItem? where ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> ReadTransaction -> YapItem? { + Metadata.Coder.Value == Metadata { return { $0.readWithMetadataByKey(key) } } @@ -74,13 +74,13 @@ extension Persistable where - returns: a (ReadTransaction) -> [Self] closure. */ public static func readWithMetadataByKeys< - Keys, ReadTransaction, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, ReadTransaction, Metadata>(_ keys: Keys) -> (ReadTransaction) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, ReadTransaction: ReadTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> ReadTransaction -> [YapItem] { + Metadata.Coder.Value == Metadata { return { $0.readWithMetadataAtIndexes(Self.indexesWithKeys(keys)) } } @@ -92,11 +92,11 @@ extension Persistable where - returns: a (WriteTransaction) -> Self closure */ public func writeWithMetadata< - WriteTransaction, Metadata where + WriteTransaction, Metadata>(_ metadata: Metadata? = nil) -> (WriteTransaction) -> YapItem where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(metadata: Metadata? = nil) -> WriteTransaction -> YapItem { + Metadata.Coder.Value == Metadata { return { $0.writeWithMetadata(YapItem(self, metadata)) } } } diff --git a/Sources/Functional_AnyWithObjectMetadata.swift b/Sources/Functional_AnyWithObjectMetadata.swift index 1b905b4..367ef22 100644 --- a/Sources/Functional_AnyWithObjectMetadata.swift +++ b/Sources/Functional_AnyWithObjectMetadata.swift @@ -21,7 +21,7 @@ extension ReadTransactionType { - returns: an optional `MetadataType` */ public func readMetadataAtIndex< - MetadataType: NSCoding>(index: YapDB.Index) -> MetadataType? { + MetadataType: NSCoding>(_ index: YapDB.Index) -> MetadataType? { return readMetadataAtIndex(index) as? MetadataType } @@ -32,11 +32,11 @@ extension ReadTransactionType { - returns: an array of `MetadataType` */ public func readMetadataAtIndexes< - Indexes, MetadataType where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, - MetadataType: NSCoding>(indexes: Indexes) -> [MetadataType] { - return indexes.flatMap(readMetadataAtIndex) + Indexes, MetadataType>(_ indexes: Indexes) -> [MetadataType?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, + MetadataType: NSCoding { + return indexes.map(readMetadataAtIndex) } } @@ -49,7 +49,7 @@ extension ConnectionType { - returns: an optional `MetadataType` */ public func readMetadataAtIndex< - MetadataType: NSCoding>(index: YapDB.Index) -> MetadataType? { + MetadataType: NSCoding>(_ index: YapDB.Index) -> MetadataType? { return read { $0.readMetadataAtIndex(index) } } @@ -60,10 +60,10 @@ extension ConnectionType { - returns: an array of `MetadataType` */ public func readMetadataAtIndexes< - Indexes, MetadataType where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, - MetadataType: NSCoding>(indexes: Indexes) -> [MetadataType] { + Indexes, MetadataType>(_ indexes: Indexes) -> [MetadataType?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, + MetadataType: NSCoding { return read { $0.readMetadataAtIndexes(indexes) } } } diff --git a/Sources/Functional_AnyWithValueMetadata.swift b/Sources/Functional_AnyWithValueMetadata.swift index 1e91339..fe210ef 100644 --- a/Sources/Functional_AnyWithValueMetadata.swift +++ b/Sources/Functional_AnyWithValueMetadata.swift @@ -21,11 +21,11 @@ extension ReadTransactionType { - returns: an optional `MetadataType` */ public func readMetadataAtIndex< - MetadataType where + MetadataType>(_ index: YapDB.Index) -> MetadataType? where MetadataType: ValueCoding, MetadataType.Coder: NSCoding, - MetadataType.Coder.ValueType == MetadataType>(index: YapDB.Index) -> MetadataType? { - return MetadataType.decode(readMetadataAtIndex(index)) + MetadataType.Coder.Value == MetadataType { + return MetadataType.decode(readMetadataAtIndex(index) as Any?) } /** @@ -35,13 +35,13 @@ extension ReadTransactionType { - returns: an array of `MetadataType` */ public func readMetadataAtIndexes< - Indexes, MetadataType where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, MetadataType>(_ indexes: Indexes) -> [MetadataType?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, MetadataType: ValueCoding, MetadataType.Coder: NSCoding, - MetadataType.Coder.ValueType == MetadataType>(indexes: Indexes) -> [MetadataType] { - return indexes.flatMap(readMetadataAtIndex) + MetadataType.Coder.Value == MetadataType { + return indexes.map(readMetadataAtIndex) } } @@ -54,10 +54,10 @@ extension ConnectionType { - returns: an optional `MetadataType` */ public func readMetadataAtIndex< - MetadataType where + MetadataType>(_ index: YapDB.Index) -> MetadataType? where MetadataType: ValueCoding, MetadataType.Coder: NSCoding, - MetadataType.Coder.ValueType == MetadataType>(index: YapDB.Index) -> MetadataType? { + MetadataType.Coder.Value == MetadataType { return read { $0.readMetadataAtIndex(index) } } @@ -68,12 +68,12 @@ extension ConnectionType { - returns: an array of `MetadataType` */ public func readMetadataAtIndexes< - Indexes, MetadataType where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, MetadataType>(_ indexes: Indexes) -> [MetadataType?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, MetadataType: ValueCoding, MetadataType.Coder: NSCoding, - MetadataType.Coder.ValueType == MetadataType>(indexes: Indexes) -> [MetadataType] { + MetadataType.Coder.Value == MetadataType { return read { $0.readMetadataAtIndexes(indexes) } } } diff --git a/Sources/Functional_ObjectWithNoMetadata.swift b/Sources/Functional_ObjectWithNoMetadata.swift index 6cebd10..f67317a 100644 --- a/Sources/Functional_ObjectWithNoMetadata.swift +++ b/Sources/Functional_ObjectWithNoMetadata.swift @@ -21,9 +21,9 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readAtIndex< - Object where + Object>(_ index: YapDB.Index) -> Object? where Object: Persistable, - Object: NSCoding>(index: YapDB.Index) -> Object? { + Object: NSCoding { return readAtIndex(index) as? Object } @@ -34,12 +34,12 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, Object where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Object>(_ indexes: Indexes) -> [Object?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Object: Persistable, - Object: NSCoding>(indexes: Indexes) -> [Object] { - return indexes.flatMap(readAtIndex) + Object: NSCoding { + return indexes.map(readAtIndex) } /** @@ -49,9 +49,9 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readByKey< - Object where + Object>(_ key: String) -> Object? where Object: Persistable, - Object: NSCoding>(key: String) -> Object? { + Object: NSCoding { return readAtIndex(Object.indexWithKey(key)) } @@ -62,11 +62,11 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, Object where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, Object>(_ keys: Keys) -> [Object?] where + Keys: Sequence, + Keys.Iterator.Element == String, Object: Persistable, - Object: NSCoding>(keys: Keys) -> [Object] { + Object: NSCoding { return readAtIndexes(Object.indexesWithKeys(keys)) } @@ -76,9 +76,9 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAll< - Object where + Object>() -> [Object?] where Object: Persistable, - Object: NSCoding>() -> [Object] { + Object: NSCoding { return readByKeys(keysInCollection(Object.collection)) } } @@ -92,9 +92,9 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readAtIndex< - Object where + Object>(_ index: YapDB.Index) -> Object? where Object: Persistable, - Object: NSCoding>(index: YapDB.Index) -> Object? { + Object: NSCoding { return read { $0.readAtIndex(index) } } @@ -105,11 +105,11 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, Object where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Object>(_ indexes: Indexes) -> [Object?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Object: Persistable, - Object: NSCoding>(indexes: Indexes) -> [Object] { + Object: NSCoding { return read { $0.readAtIndexes(indexes) } } @@ -120,9 +120,9 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readByKey< - Object where + Object>(_ key: String) -> Object? where Object: Persistable, - Object: NSCoding>(key: String) -> Object? { + Object: NSCoding { return readAtIndex(Object.indexWithKey(key)) } @@ -133,11 +133,11 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, Object where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, Object>(_ keys: Keys) -> [Object?] where + Keys: Sequence, + Keys.Iterator.Element == String, Object: Persistable, - Object: NSCoding>(keys: Keys) -> [Object] { + Object: NSCoding { return readAtIndexes(Object.indexesWithKeys(keys)) } @@ -147,9 +147,9 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAll< - Object where + Object>() -> [Object?] where Object: Persistable, - Object: NSCoding>() -> [Object] { + Object: NSCoding { return read { $0.readAll() } } } @@ -165,10 +165,10 @@ extension WriteTransactionType { - returns: the same item */ public func write< - Object where + Object>(_ item: Object) -> Object where Object: Persistable, - Object: NSCoding>(item: Object) -> Object { - writeAtIndex(item.index, object: item, metadata: .None) + Object: NSCoding { + writeAtIndex(item.index, object: item, metadata: .none) return item } @@ -179,11 +179,11 @@ extension WriteTransactionType { - returns: the same items, in an array. */ public func write< - Items, Object where - Items: SequenceType, - Items.Generator.Element == Object, + Items, Object>(_ items: Items) -> [Object] where + Items: Sequence, + Items.Iterator.Element == Object, Object: Persistable, - Object: NSCoding>(items: Items) -> [Object] { + Object: NSCoding { return items.map(write) } } @@ -197,9 +197,9 @@ extension ConnectionType { - returns: the same item */ public func write< - Object where + Object>(_ item: Object) -> Object where Object: Persistable, - Object: NSCoding>(item: Object) -> Object { + Object: NSCoding { return write { $0.write(item) } } @@ -210,11 +210,11 @@ extension ConnectionType { - returns: the same items, in an array. */ public func write< - Items, Object where - Items: SequenceType, - Items.Generator.Element == Object, + Items, Object>(_ items: Items) -> [Object] where + Items: Sequence, + Items.Iterator.Element == Object, Object: Persistable, - Object: NSCoding>(items: Items) -> [Object] { + Object: NSCoding { return write { $0.write(items) } } @@ -226,9 +226,9 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - Object where + Object>(_ item: Object, queue: DispatchQueue = DispatchQueue.main, completion: ((Object) -> Void)? = .none) where Object: Persistable, - Object: NSCoding>(item: Object, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (Object -> Void)? = .None) { + Object: NSCoding { asyncWrite({ $0.write(item) }, queue: queue, completion: completion) } @@ -240,11 +240,11 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - Items, Object where - Items: SequenceType, - Items.Generator.Element == Object, + Items, Object>(_ items: Items, queue: DispatchQueue = DispatchQueue.main, completion: (([Object]) -> Void)? = .none) where + Items: Sequence, + Items.Iterator.Element == Object, Object: Persistable, - Object: NSCoding>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([Object] -> Void)? = .None) { + Object: NSCoding { asyncWrite({ $0.write(items) }, queue: queue, completion: completion) } } diff --git a/Sources/Functional_ObjectWithObjectMetadata.swift b/Sources/Functional_ObjectWithObjectMetadata.swift index 3a9d98d..70e2d60 100644 --- a/Sources/Functional_ObjectWithObjectMetadata.swift +++ b/Sources/Functional_ObjectWithObjectMetadata.swift @@ -21,10 +21,10 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readWithMetadataAtIndex< - Object, Metadata where + Object, Metadata>(_ index: YapDB.Index) -> YapItem? where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(index: YapDB.Index) -> YapItem? { + Metadata: NSCoding { guard let item: Object = readAtIndex(index) else { return nil } let metadata: Metadata? = readMetadataAtIndex(index) return YapItem(item, metadata) @@ -37,14 +37,13 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readWithMetadataAtIndexes< - Indexes, Object, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Object, Metadata>(_ indexes: Indexes) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(indexes: Indexes) -> [YapItem] { - // FIXME: using flatMap means the output length need not match the input length - return indexes.flatMap(readWithMetadataAtIndex) + Metadata: NSCoding { + return indexes.map(readWithMetadataAtIndex) } /** @@ -54,10 +53,10 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readWithMetadataByKey< - Object, Metadata where + Object, Metadata>(_ key: String) -> YapItem? where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(key: String) -> YapItem? { + Metadata: NSCoding { return readWithMetadataAtIndex(Object.indexWithKey(key)) } @@ -68,12 +67,12 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readWithMetadataByKeys< - Keys, Object, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, Object, Metadata>(_ keys: Keys) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(keys: Keys) -> [YapItem] { + Metadata: NSCoding { return readWithMetadataAtIndexes(Object.indexesWithKeys(keys)) } @@ -83,10 +82,10 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readWithMetadataAll< - Object, Metadata where + Object, Metadata>() -> [YapItem?] where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>() -> [YapItem] { + Metadata: NSCoding { return readWithMetadataByKeys(keysInCollection(Object.collection)) } } @@ -100,10 +99,10 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readWithMetadataAtIndex< - Object, Metadata where + Object, Metadata>(_ index: YapDB.Index) -> YapItem? where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(index: YapDB.Index) -> YapItem? { + Metadata: NSCoding { return read { $0.readWithMetadataAtIndex(index) } } @@ -114,12 +113,12 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readWithMetadataAtIndexes< - Indexes, Object, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Object, Metadata>(_ indexes: Indexes) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(indexes: Indexes) -> [YapItem] { + Metadata: NSCoding { return read { $0.readWithMetadataAtIndexes(indexes) } } @@ -130,10 +129,10 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readWithMetadataByKey< - Object, Metadata where + Object, Metadata>(_ key: String) -> YapItem? where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(key: String) -> YapItem? { + Metadata: NSCoding { return readWithMetadataAtIndex(Object.indexWithKey(key)) } @@ -144,12 +143,12 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readWithMetadataByKeys< - Keys, Object, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, Object, Metadata>(_ keys: Keys) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(keys: Keys) -> [YapItem] { + Metadata: NSCoding { return readWithMetadataAtIndexes(Object.indexesWithKeys(keys)) } @@ -159,10 +158,10 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readWithMetadataAll< - Object, Metadata where + Object, Metadata>() -> [YapItem?] where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>() -> [YapItem] { + Metadata: NSCoding { return read { $0.readWithMetadataAll() } } } @@ -177,10 +176,10 @@ extension WriteTransactionType { - parameter item: the item to store. */ public func writeWithMetadata< - Object, Metadata where + Object, Metadata>(_ item: YapItem) -> YapItem where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(item: YapItem) -> YapItem { + Metadata: NSCoding { writeAtIndex(item.value.index, object: item.value, metadata: item.metadata) return item } @@ -191,12 +190,12 @@ extension WriteTransactionType { - parameter items: a SequenceType of items to store. */ public func writeWithMetadata< - Items, Object, Metadata where - Items: SequenceType, - Items.Generator.Element == YapItem, + Items, Object, Metadata>(_ items: Items) -> [YapItem] where + Items: Sequence, + Items.Iterator.Element == YapItem, Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(items: Items) -> [YapItem] { + Metadata: NSCoding { return items.map(writeWithMetadata) } } @@ -209,10 +208,10 @@ extension ConnectionType { - parameter item: the item to store. */ public func writeWithMetadata< - Object, Metadata where + Object, Metadata>(_ item: YapItem) -> YapItem where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(item: YapItem) -> YapItem { + Metadata: NSCoding { return write { $0.writeWithMetadata(item) } } @@ -222,12 +221,12 @@ extension ConnectionType { - parameter items: a SequenceType of items to store. */ public func writeWithMetadata< - Items, Object, Metadata where - Items: SequenceType, - Items.Generator.Element == YapItem, + Items, Object, Metadata>(_ items: Items) -> [YapItem] where + Items: Sequence, + Items.Iterator.Element == YapItem, Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(items: Items) -> [YapItem] { + Metadata: NSCoding { return write { $0.writeWithMetadata(items) } } @@ -239,10 +238,10 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWriteWithMetadata< - Object, Metadata where + Object, Metadata>(_ item: YapItem, queue: DispatchQueue = DispatchQueue.main, completion: ((YapItem) -> Void)? = .none) where Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(item: YapItem, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { + Metadata: NSCoding { asyncWrite({ $0.writeWithMetadata(item) }, queue: queue, completion: completion) } @@ -254,12 +253,12 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWriteWithMetadata< - Items, Object, Metadata where - Items: SequenceType, - Items.Generator.Element == YapItem, + Items, Object, Metadata>(_ items: Items, queue: DispatchQueue = DispatchQueue.main, completion: (([YapItem]) -> Void)? = .none) where + Items: Sequence, + Items.Iterator.Element == YapItem, Object: Persistable, Object: NSCoding, - Metadata: NSCoding>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { + Metadata: NSCoding { asyncWrite({ $0.writeWithMetadata(items) }, queue: queue, completion: completion) } } diff --git a/Sources/Functional_ObjectWithValueMetadata.swift b/Sources/Functional_ObjectWithValueMetadata.swift index ca78eb5..8c9906c 100644 --- a/Sources/Functional_ObjectWithValueMetadata.swift +++ b/Sources/Functional_ObjectWithValueMetadata.swift @@ -21,12 +21,12 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readWithMetadataAtIndex< - Object, Metadata where + Object, Metadata>(_ index: YapDB.Index) -> YapItem? where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> YapItem? { + Metadata.Coder.Value == Metadata { guard let item: Object = readAtIndex(index) else { return nil } let metadata: Metadata? = readMetadataAtIndex(index) return YapItem(item, metadata) @@ -39,16 +39,15 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readWithMetadataAtIndexes< - Indexes, Object, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Object, Metadata>(_ indexes: Indexes) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [YapItem] { - // FIXME: using flatMap means the output length need not match the input length - return indexes.flatMap(readWithMetadataAtIndex) + Metadata.Coder.Value == Metadata { + return indexes.map(readWithMetadataAtIndex) } /** @@ -58,12 +57,12 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readWithMetadataByKey< - Object, Metadata where + Object, Metadata>(_ key: String) -> YapItem? where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> YapItem? { + Metadata.Coder.Value == Metadata { return readWithMetadataAtIndex(Object.indexWithKey(key)) } @@ -74,14 +73,14 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readWithMetadataByKeys< - Keys, Object, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, Object, Metadata>(_ keys: Keys) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [YapItem] { + Metadata.Coder.Value == Metadata { return readWithMetadataAtIndexes(Object.indexesWithKeys(keys)) } @@ -91,12 +90,12 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readWithMetadataAll< - Object, Metadata where + Object, Metadata>() -> [YapItem?] where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>() -> [YapItem] { + Metadata.Coder.Value == Metadata { return readWithMetadataByKeys(keysInCollection(Object.collection)) } } @@ -110,12 +109,12 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readWithMetadataAtIndex< - Object, Metadata where + Object, Metadata>(_ index: YapDB.Index) -> YapItem? where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> YapItem? { + Metadata.Coder.Value == Metadata { return read { $0.readWithMetadataAtIndex(index) } } @@ -126,14 +125,14 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readWithMetadataAtIndexes< - Indexes, Object, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Object, Metadata>(_ indexes: Indexes) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [YapItem] { + Metadata.Coder.Value == Metadata { return read { $0.readWithMetadataAtIndexes(indexes) } } @@ -144,12 +143,12 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readWithMetadataByKey< - Object, Metadata where + Object, Metadata>(_ key: String) -> YapItem? where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> YapItem? { + Metadata.Coder.Value == Metadata { return readWithMetadataAtIndex(Object.indexWithKey(key)) } @@ -160,14 +159,14 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readWithMetadataByKeys< - Keys, Object, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, Object, Metadata>(_ keys: Keys) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [YapItem] { + Metadata.Coder.Value == Metadata { return readWithMetadataAtIndexes(Object.indexesWithKeys(keys)) } @@ -177,12 +176,12 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readWithMetadataAll< - Object, Metadata where + Object, Metadata>() -> [YapItem?] where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>() -> [YapItem] { + Metadata.Coder.Value == Metadata { return read { $0.readWithMetadataAll() } } } @@ -197,12 +196,12 @@ extension WriteTransactionType { - parameter item: the item to store. */ public func writeWithMetadata< - Object, Metadata where + Object, Metadata>(_ item: YapItem) -> YapItem where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(item: YapItem) -> YapItem { + Metadata.Coder.Value == Metadata { writeAtIndex(item.value.index, object: item.value, metadata: item.metadata?.encoded) return item } @@ -213,14 +212,14 @@ extension WriteTransactionType { - parameter items: a SequenceType of items to store. */ public func writeWithMetadata< - Items, Object, Metadata where - Items: SequenceType, - Items.Generator.Element == YapItem, + Items, Object, Metadata>(_ items: Items) -> [YapItem] where + Items: Sequence, + Items.Iterator.Element == YapItem, Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(items: Items) -> [YapItem] { + Metadata.Coder.Value == Metadata { return items.map(writeWithMetadata) } } @@ -233,12 +232,12 @@ extension ConnectionType { - parameter item: the item to store. */ public func writeWithMetadata< - Object, Metadata where + Object, Metadata>(_ item: YapItem) -> YapItem where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(item: YapItem) -> YapItem { + Metadata.Coder.Value == Metadata { return write { $0.writeWithMetadata(item) } } @@ -248,14 +247,14 @@ extension ConnectionType { - parameter items: a SequenceType of items to store. */ public func writeWithMetadata< - Items, Object, Metadata where - Items: SequenceType, - Items.Generator.Element == YapItem, + Items, Object, Metadata>(_ items: Items) -> [YapItem] where + Items: Sequence, + Items.Iterator.Element == YapItem, Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(items: Items) -> [YapItem] { + Metadata.Coder.Value == Metadata { return write { $0.writeWithMetadata(items) } } @@ -267,12 +266,12 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWriteWithMetadata< - Object, Metadata where + Object, Metadata>(_ item: YapItem, queue: DispatchQueue = DispatchQueue.main, completion: ((YapItem) -> Void)? = .none) where Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(item: YapItem, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { + Metadata.Coder.Value == Metadata { asyncWrite({ $0.writeWithMetadata(item) }, queue: queue, completion: completion) } @@ -284,14 +283,14 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWriteWithMetadata< - Items, Object, Metadata where - Items: SequenceType, - Items.Generator.Element == YapItem, + Items, Object, Metadata>(_ items: Items, queue: DispatchQueue = DispatchQueue.main, completion: (([YapItem]) -> Void)? = .none) where + Items: Sequence, + Items.Iterator.Element == YapItem, Object: Persistable, Object: NSCoding, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { + Metadata.Coder.Value == Metadata { asyncWrite({ $0.writeWithMetadata(items) }, queue: queue, completion: completion) } } diff --git a/Sources/Functional_Remove.swift b/Sources/Functional_Remove.swift index b45fc28..7074c6e 100644 --- a/Sources/Functional_Remove.swift +++ b/Sources/Functional_Remove.swift @@ -15,7 +15,7 @@ extension WriteTransactionType { - parameter item: a `Persistable` item */ - public func remove(item: Item) { + public func remove(_ item: Item) { removeAtIndexes([item.index]) } @@ -25,10 +25,10 @@ extension WriteTransactionType { - parameter items: a sequence of `Persistable` items */ public func remove< - Items, Item where - Items: SequenceType, - Items.Generator.Element == Item, - Item: Persistable>(items: Items) { + Items, Item>(_ items: Items) where + Items: Sequence, + Items.Iterator.Element == Item, + Item: Persistable { removeAtIndexes(items.map { $0.index }) } } @@ -41,7 +41,7 @@ extension ConnectionType { - parameter item: a `Persistable` item */ - public func remove(item: Item) { + public func remove(_ item: Item) { write { $0.remove(item) } } @@ -52,10 +52,10 @@ extension ConnectionType { - parameter items: a sequence of `Persistable` items */ public func remove< - Items, Item where - Items: SequenceType, - Items.Generator.Element == Item, - Item: Persistable>(items: Items) { + Items, Item>(_ items: Items) where + Items: Sequence, + Items.Iterator.Element == Item, + Item: Persistable { write { $0.remove(items) } } @@ -65,7 +65,7 @@ extension ConnectionType { - parameter item: a `Persistable` item */ - public func asyncRemove(item: Item, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: dispatch_block_t? = .None) { + public func asyncRemove(_ item: Item, queue: DispatchQueue = DispatchQueue.main, completion: (()->())? = .none) { asyncWrite({ $0.remove(item) }, queue: queue, completion: { _ in completion?() }) } @@ -76,10 +76,10 @@ extension ConnectionType { - parameter items: a sequence of `Persistable` items */ public func asyncRemove< - Items, Item where - Items: SequenceType, - Items.Generator.Element == Item, - Item: Persistable>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: dispatch_block_t? = .None) { + Items, Item>(_ items: Items, queue: DispatchQueue = DispatchQueue.main, completion: (()->())? = .none) where + Items: Sequence, + Items.Iterator.Element == Item, + Item: Persistable { asyncWrite({ $0.remove(items) }, queue: queue, completion: { _ in completion?() }) } } diff --git a/Sources/Functional_ValueWIthObjectMetadata.swift b/Sources/Functional_ValueWIthObjectMetadata.swift index ce9f5b8..9e416fe 100644 --- a/Sources/Functional_ValueWIthObjectMetadata.swift +++ b/Sources/Functional_ValueWIthObjectMetadata.swift @@ -21,12 +21,12 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readWithMetadataAtIndex< - Value, Metadata where + Value, Metadata>(_ index: YapDB.Index) -> YapItem? where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>(index: YapDB.Index) -> YapItem? { + Value.Coder.Value == Value, + Metadata: NSCoding { guard let item: Value = Value.decode(readAtIndex(index)) else { return nil } let metadata: Metadata? = readMetadataAtIndex(index) return YapItem(item, metadata) @@ -39,16 +39,15 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readWithMetadataAtIndexes< - Indexes, Value, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Value, Metadata>(_ indexes: Indexes) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>(indexes: Indexes) -> [YapItem] { - // FIXME: using flatMap means the output length need not match the input length - return indexes.flatMap(readWithMetadataAtIndex) + Value.Coder.Value == Value, + Metadata: NSCoding { + return indexes.map(readWithMetadataAtIndex) } /** @@ -58,12 +57,12 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readWithMetadataByKey< - Value, Metadata where + Value, Metadata>(_ key: String) -> YapItem? where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>(key: String) -> YapItem? { + Value.Coder.Value == Value, + Metadata: NSCoding { return readWithMetadataAtIndex(Value.indexWithKey(key)) } @@ -74,14 +73,14 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readWithMetadataByKeys< - Keys, Value, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, Value, Metadata>(_ keys: Keys) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>(keys: Keys) -> [YapItem] { + Value.Coder.Value == Value, + Metadata: NSCoding { return readWithMetadataAtIndexes(Value.indexesWithKeys(keys)) } @@ -91,12 +90,12 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readWithMetadataAll< - Value, Metadata where + Value, Metadata>() -> [YapItem?] where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>() -> [YapItem] { + Value.Coder.Value == Value, + Metadata: NSCoding { return readWithMetadataByKeys(keysInCollection(Value.collection)) } } @@ -110,12 +109,12 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readWithMetadataAtIndex< - Value, Metadata where + Value, Metadata>(_ index: YapDB.Index) -> YapItem? where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>(index: YapDB.Index) -> YapItem? { + Value.Coder.Value == Value, + Metadata: NSCoding { return read { $0.readWithMetadataAtIndex(index) } } @@ -126,14 +125,14 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readWithMetadataAtIndexes< - Indexes, Value, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Value, Metadata>(_ indexes: Indexes) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>(indexes: Indexes) -> [YapItem] { + Value.Coder.Value == Value, + Metadata: NSCoding { return read { $0.readWithMetadataAtIndexes(indexes) } } @@ -144,12 +143,12 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readWithMetadataByKey< - Value, Metadata where + Value, Metadata>(_ key: String) -> YapItem? where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>(key: String) -> YapItem? { + Value.Coder.Value == Value, + Metadata: NSCoding { return readWithMetadataAtIndex(Value.indexWithKey(key)) } @@ -160,14 +159,14 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readWithMetadataByKeys< - Keys, Value, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, Value, Metadata>(_ keys: Keys) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>(keys: Keys) -> [YapItem] { + Value.Coder.Value == Value, + Metadata: NSCoding { return readWithMetadataAtIndexes(Value.indexesWithKeys(keys)) } @@ -177,12 +176,12 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readWithMetadataAll< - Value, Metadata where + Value, Metadata>() -> [YapItem?] where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>() -> [YapItem] { + Value.Coder.Value == Value, + Metadata: NSCoding { return read { $0.readWithMetadataAll() } } } @@ -197,12 +196,12 @@ extension WriteTransactionType { - parameter item: the item to store. */ public func writeWithMetadata< - Value, Metadata where + Value, Metadata>(_ item: YapItem) -> YapItem where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>(item: YapItem) -> YapItem { + Value.Coder.Value == Value, + Metadata: NSCoding { writeAtIndex(item.value.index, object: item.value.encoded, metadata: item.metadata) return item } @@ -213,14 +212,14 @@ extension WriteTransactionType { - parameter items: a SequenceType of items to store. */ public func writeWithMetadata< - Items, Value, Metadata where - Items: SequenceType, - Items.Generator.Element == YapItem, + Items, Value, Metadata>(_ items: Items) -> [YapItem] where + Items: Sequence, + Items.Iterator.Element == YapItem, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>(items: Items) -> [YapItem] { + Value.Coder.Value == Value, + Metadata: NSCoding { return items.map(writeWithMetadata) } } @@ -233,12 +232,12 @@ extension ConnectionType { - parameter item: the item to store. */ public func writeWithMetadata< - Value, Metadata where + Value, Metadata>(_ item: YapItem) -> YapItem where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>(item: YapItem) -> YapItem { + Value.Coder.Value == Value, + Metadata: NSCoding { return write { $0.writeWithMetadata(item) } } @@ -248,14 +247,14 @@ extension ConnectionType { - parameter items: a SequenceType of items to store. */ public func writeWithMetadata< - Items, Value, Metadata where - Items: SequenceType, - Items.Generator.Element == YapItem, + Items, Value, Metadata>(_ items: Items) -> [YapItem] where + Items: Sequence, + Items.Iterator.Element == YapItem, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>(items: Items) -> [YapItem] { + Value.Coder.Value == Value, + Metadata: NSCoding { return write { $0.writeWithMetadata(items) } } @@ -267,12 +266,12 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWriteWithMetadata< - Value, Metadata where + Value, Metadata>(_ item: YapItem, queue: DispatchQueue = DispatchQueue.main, completion: ((YapItem) -> Void)? = .none) where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>(item: YapItem, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { + Value.Coder.Value == Value, + Metadata: NSCoding { asyncWrite({ $0.writeWithMetadata(item) }, queue: queue, completion: completion) } @@ -284,14 +283,14 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWriteWithMetadata< - Items, Value, Metadata where - Items: SequenceType, - Items.Generator.Element == YapItem, + Items, Value, Metadata>(_ items: Items, queue: DispatchQueue = DispatchQueue.main, completion: (([YapItem]) -> Void)? = .none) where + Items: Sequence, + Items.Iterator.Element == YapItem, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, - Metadata: NSCoding>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { + Value.Coder.Value == Value, + Metadata: NSCoding { asyncWrite({ $0.writeWithMetadata(items) }, queue: queue, completion: completion) } } diff --git a/Sources/Functional_ValueWIthValueMetadata.swift b/Sources/Functional_ValueWIthValueMetadata.swift index 19fb418..2147f6f 100644 --- a/Sources/Functional_ValueWIthValueMetadata.swift +++ b/Sources/Functional_ValueWIthValueMetadata.swift @@ -21,14 +21,14 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readWithMetadataAtIndex< - Value, Metadata where + Value, Metadata>(_ index: YapDB.Index) -> YapItem? where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> YapItem? { + Metadata.Coder.Value == Metadata { guard let item: Value = Value.decode(readAtIndex(index)) else { return nil } let metadata: Metadata? = readMetadataAtIndex(index) return YapItem(item, metadata) @@ -41,18 +41,17 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readWithMetadataAtIndexes< - Indexes, Value, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Value, Metadata>(_ indexes: Indexes) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [YapItem] { - // FIXME: using flatMap means the output length need not match the input length - return indexes.flatMap(readWithMetadataAtIndex) + Metadata.Coder.Value == Metadata { + return indexes.map(readWithMetadataAtIndex) } /** @@ -62,14 +61,14 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readWithMetadataByKey< - Value, Metadata where + Value, Metadata>(_ key: String) -> YapItem? where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> YapItem? { + Metadata.Coder.Value == Metadata { return readWithMetadataAtIndex(Value.indexWithKey(key)) } @@ -80,16 +79,16 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readWithMetadataByKeys< - Keys, Value, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, Value, Metadata>(_ keys: Keys) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [YapItem] { + Metadata.Coder.Value == Metadata { return readWithMetadataAtIndexes(Value.indexesWithKeys(keys)) } @@ -99,14 +98,14 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readWithMetadataAll< - Value, Metadata where + Value, Metadata>() -> [YapItem?] where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>() -> [YapItem] { + Metadata.Coder.Value == Metadata { return readWithMetadataByKeys(keysInCollection(Value.collection)) } } @@ -120,14 +119,14 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readWithMetadataAtIndex< - Value, Metadata where + Value, Metadata>(_ index: YapDB.Index) -> YapItem? where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> YapItem? { + Metadata.Coder.Value == Metadata { return read { $0.readWithMetadataAtIndex(index) } } @@ -138,16 +137,16 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readWithMetadataAtIndexes< - Indexes, Value, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Value, Metadata>(_ indexes: Indexes) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [YapItem] { + Metadata.Coder.Value == Metadata { return read { $0.readWithMetadataAtIndexes(indexes) } } @@ -158,14 +157,14 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readWithMetadataByKey< - Value, Metadata where + Value, Metadata>(_ key: String) -> YapItem? where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> YapItem? { + Metadata.Coder.Value == Metadata { return readWithMetadataAtIndex(Value.indexWithKey(key)) } @@ -176,16 +175,16 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readWithMetadataByKeys< - Keys, Value, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, Value, Metadata>(_ keys: Keys) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [YapItem] { + Metadata.Coder.Value == Metadata { return readWithMetadataAtIndexes(Value.indexesWithKeys(keys)) } @@ -195,14 +194,14 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readWithMetadataAll< - Value, Metadata where + Value, Metadata>() -> [YapItem?] where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>() -> [YapItem] { + Metadata.Coder.Value == Metadata { return read { $0.readWithMetadataAll() } } } @@ -217,14 +216,14 @@ extension WriteTransactionType { - parameter item: the item to store. */ public func writeWithMetadata< - Value, Metadata where + Value, Metadata>(_ item: YapItem) -> YapItem where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(item: YapItem) -> YapItem { + Metadata.Coder.Value == Metadata { writeAtIndex(item.value.index, object: item.value.encoded, metadata: item.metadata?.encoded) return item } @@ -235,16 +234,16 @@ extension WriteTransactionType { - parameter items: a SequenceType of items to store. */ public func writeWithMetadata< - Items, Value, Metadata where - Items: SequenceType, - Items.Generator.Element == YapItem, + Items, Value, Metadata>(_ items: Items) -> [YapItem] where + Items: Sequence, + Items.Iterator.Element == YapItem, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(items: Items) -> [YapItem] { + Metadata.Coder.Value == Metadata { return items.map(writeWithMetadata) } } @@ -257,14 +256,14 @@ extension ConnectionType { - parameter item: the item to store. */ public func writeWithMetadata< - Value, Metadata where + Value, Metadata>(_ item: YapItem) -> YapItem where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(item: YapItem) -> YapItem { + Metadata.Coder.Value == Metadata { return write { $0.writeWithMetadata(item) } } @@ -274,16 +273,16 @@ extension ConnectionType { - parameter items: a SequenceType of items to store. */ public func writeWithMetadata< - Items, Value, Metadata where - Items: SequenceType, - Items.Generator.Element == YapItem, + Items, Value, Metadata>(_ items: Items) -> [YapItem] where + Items: Sequence, + Items.Iterator.Element == YapItem, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(items: Items) -> [YapItem] { + Metadata.Coder.Value == Metadata { return write { $0.writeWithMetadata(items) } } @@ -295,14 +294,14 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWriteWithMetadata< - Value, Metadata where + Value, Metadata>(_ item: YapItem, queue: DispatchQueue = DispatchQueue.main, completion: ((YapItem) -> Void)? = .none) where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(item: YapItem, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { + Metadata.Coder.Value == Metadata { asyncWrite({ $0.writeWithMetadata(item) }, queue: queue, completion: completion) } @@ -314,16 +313,16 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWriteWithMetadata< - Items, Value, Metadata where - Items: SequenceType, - Items.Generator.Element == YapItem, + Items, Value, Metadata>(_ items: Items, queue: DispatchQueue = DispatchQueue.main, completion: (([YapItem]) -> Void)? = .none) where + Items: Sequence, + Items.Iterator.Element == YapItem, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value, + Value.Coder.Value == Value, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { + Metadata.Coder.Value == Metadata { asyncWrite({ $0.writeWithMetadata(items) }, queue: queue, completion: completion) } } diff --git a/Sources/Functional_ValueWithNoMetadata.swift b/Sources/Functional_ValueWithNoMetadata.swift index a29a136..156a4b9 100644 --- a/Sources/Functional_ValueWithNoMetadata.swift +++ b/Sources/Functional_ValueWithNoMetadata.swift @@ -21,12 +21,12 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readAtIndex< - Value where + Value>(_ index: YapDB.Index) -> Value? where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>(index: YapDB.Index) -> Value? { - return Value.decode(readAtIndex(index)) + Value.Coder.Value == Value { + return Value.decode(readAtIndex(index) as Any?) } /** @@ -36,14 +36,14 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, Value where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Value>(_ indexes: Indexes) -> [Value?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>(indexes: Indexes) -> [Value] { - return indexes.flatMap(readAtIndex) + Value.Coder.Value == Value { + return indexes.map(readAtIndex) } /** @@ -53,11 +53,11 @@ extension ReadTransactionType { - returns: an optional `ItemType` */ public func readByKey< - Value where + Value>(_ key: String) -> Value? where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>(key: String) -> Value? { + Value.Coder.Value == Value { return readAtIndex(Value.indexWithKey(key)) } @@ -68,13 +68,13 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, Value where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, Value>(_ keys: Keys) -> [Value?] where + Keys: Sequence, + Keys.Iterator.Element == String, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>(keys: Keys) -> [Value] { + Value.Coder.Value == Value { return readAtIndexes(Value.indexesWithKeys(keys)) } @@ -84,11 +84,11 @@ extension ReadTransactionType { - returns: an array of `ItemType` */ public func readAll< - Value where + Value>() -> [Value?] where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>() -> [Value] { + Value.Coder.Value == Value { return readByKeys(keysInCollection(Value.collection)) } } @@ -102,11 +102,11 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readAtIndex< - Value where + Value>(_ index: YapDB.Index) -> Value? where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>(index: YapDB.Index) -> Value? { + Value.Coder.Value == Value { return read { $0.readAtIndex(index) } } @@ -117,13 +117,13 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAtIndexes< - Indexes, Value where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Value>(_ indexes: Indexes) -> [Value?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>(indexes: Indexes) -> [Value] { + Value.Coder.Value == Value { return read { $0.readAtIndexes(indexes) } } @@ -134,11 +134,11 @@ extension ConnectionType { - returns: an optional `ItemType` */ public func readByKey< - Value where + Value>(_ key: String) -> Value? where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>(key: String) -> Value? { + Value.Coder.Value == Value { return readAtIndex(Value.indexWithKey(key)) } @@ -149,13 +149,13 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readByKeys< - Keys, Value where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, Value>(_ keys: Keys) -> [Value?] where + Keys: Sequence, + Keys.Iterator.Element == String, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>(keys: Keys) -> [Value] { + Value.Coder.Value == Value { return readAtIndexes(Value.indexesWithKeys(keys)) } @@ -165,11 +165,11 @@ extension ConnectionType { - returns: an array of `ItemType` */ public func readAll< - Value where + Value>() -> [Value?] where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>() -> [Value] { + Value.Coder.Value == Value { return read { $0.readAll() } } } @@ -184,12 +184,12 @@ extension WriteTransactionType { - parameter item: the item to store. */ public func write< - Value where + Value>(_ item: Value) -> Value where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>(item: Value) -> Value { - writeAtIndex(item.index, object: item.encoded, metadata: .None) + Value.Coder.Value == Value { + writeAtIndex(item.index, object: item.encoded, metadata: .none) return item } @@ -199,13 +199,13 @@ extension WriteTransactionType { - parameter items: a SequenceType of items to store. */ public func write< - Items, Value where - Items: SequenceType, - Items.Generator.Element == Value, + Items, Value>(_ items: Items) -> [Value] where + Items: Sequence, + Items.Iterator.Element == Value, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>(items: Items) -> [Value] { + Value.Coder.Value == Value { return items.map(write) } } @@ -218,11 +218,11 @@ extension ConnectionType { - parameter item: the item to store. */ public func write< - Value where + Value>(_ item: Value) -> Value where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>(item: Value) -> Value { + Value.Coder.Value == Value { return write { $0.write(item) } } @@ -232,13 +232,13 @@ extension ConnectionType { - parameter items: a SequenceType of items to store. */ public func write< - Items, Value where - Items: SequenceType, - Items.Generator.Element == Value, + Items, Value>(_ items: Items) -> [Value] where + Items: Sequence, + Items.Iterator.Element == Value, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>(items: Items) -> [Value] { + Value.Coder.Value == Value { return write { $0.write(items) } } @@ -250,11 +250,11 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - Value where + Value>(_ item: Value, queue: DispatchQueue = DispatchQueue.main, completion: ((Value) -> Void)? = .none) where Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>(item: Value, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (Value -> Void)? = .None) { + Value.Coder.Value == Value { asyncWrite({ $0.write(item) }, queue: queue, completion: completion) } @@ -266,13 +266,13 @@ extension ConnectionType { - parameter completion: a dispatch_block_t for completion. */ public func asyncWrite< - Items, Value where - Items: SequenceType, - Items.Generator.Element == Value, + Items, Value>(_ items: Items, queue: DispatchQueue = DispatchQueue.main, completion: (([Value]) -> Void)? = .none) where + Items: Sequence, + Items.Iterator.Element == Value, Value: Persistable, Value: ValueCoding, Value.Coder: NSCoding, - Value.Coder.ValueType == Value>(items: Items, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([Value] -> Void)? = .None) { + Value.Coder.Value == Value { asyncWrite({ $0.write(items) }, queue: queue, completion: completion) } } diff --git a/Sources/Persistable_AnyWithObjectMetadata.swift b/Sources/Persistable_AnyWithObjectMetadata.swift index fbe08ed..ed3e226 100644 --- a/Sources/Persistable_AnyWithObjectMetadata.swift +++ b/Sources/Persistable_AnyWithObjectMetadata.swift @@ -14,24 +14,24 @@ import ValueCoding extension Readable where ItemType: Persistable { - func metadataInTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> Metadata? { + func metadataInTransaction(_ transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> Metadata? { return transaction.readMetadataAtIndex(index) } - func metadataAtIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> Metadata? { + func metadataAtIndexInTransaction(_ index: YapDB.Index) -> (Database.Connection.ReadTransaction) -> Metadata? { return { self.metadataInTransaction($0, atIndex: index) } } - func metadataInTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> Metadata? { + func metadataInTransactionAtIndex(_ transaction: Database.Connection.ReadTransaction) -> (YapDB.Index) -> Metadata? { return { self.metadataInTransaction(transaction, atIndex: $0) } } func metadataAtIndexesInTransaction< - Indexes, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, - Metadata: NSCoding>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [Metadata] { - return { indexes.flatMap(self.metadataInTransactionAtIndex($0)) } + Indexes, Metadata>(_ indexes: Indexes) -> (Database.Connection.ReadTransaction) -> [Metadata?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, + Metadata: NSCoding { + return { indexes.map(self.metadataInTransactionAtIndex($0)) } } /** @@ -41,8 +41,8 @@ extension Readable where - returns: an optional `Metadata` */ public func metadataAtIndex< - Metadata where - Metadata: NSCoding>(index: YapDB.Index) -> Metadata? { + Metadata>(_ index: YapDB.Index) -> Metadata? where + Metadata: NSCoding { return sync(metadataAtIndexInTransaction(index)) } @@ -53,10 +53,10 @@ extension Readable where - returns: an array of `Metadata` */ public func metadataAtIndexes< - Indexes, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, - Metadata: NSCoding>(indexes: Indexes) -> [Metadata] { + Indexes, Metadata>(_ indexes: Indexes) -> [Metadata?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, + Metadata: NSCoding { return sync(metadataAtIndexesInTransaction(indexes)) } } diff --git a/Sources/Persistable_AnyWithValueMetadata.swift b/Sources/Persistable_AnyWithValueMetadata.swift index 9147903..ceeb1a1 100644 --- a/Sources/Persistable_AnyWithValueMetadata.swift +++ b/Sources/Persistable_AnyWithValueMetadata.swift @@ -15,37 +15,37 @@ extension Readable where ItemType: Persistable { func metadataInTransaction< - Metadata where + Metadata>(_ transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> Metadata? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> Metadata? { + Metadata.Coder.Value == Metadata { return transaction.readMetadataAtIndex(index) } func metadataAtIndexInTransaction< - Metadata where + Metadata>(_ index: YapDB.Index) -> (Database.Connection.ReadTransaction) -> Metadata? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> Database.Connection.ReadTransaction -> Metadata? { + Metadata.Coder.Value == Metadata { return { self.metadataInTransaction($0, atIndex: index) } } func metadataInTransactionAtIndex< - Metadata where + Metadata>(_ transaction: Database.Connection.ReadTransaction) -> (YapDB.Index) -> Metadata? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> Metadata? { + Metadata.Coder.Value == Metadata { return { self.metadataInTransaction(transaction, atIndex: $0) } } func metadataAtIndexesInTransaction< - Indexes, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Metadata>(_ indexes: Indexes) -> (Database.Connection.ReadTransaction) -> [Metadata?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [Metadata] { - return { indexes.flatMap(self.metadataInTransactionAtIndex($0)) } + Metadata.Coder.Value == Metadata { + return { indexes.map(self.metadataInTransactionAtIndex($0)) } } /** @@ -55,10 +55,10 @@ extension Readable where - returns: an optional `Metadata` */ public func metadataAtIndex< - Metadata where + Metadata>(_ index: YapDB.Index) -> Metadata? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> Metadata? { + Metadata.Coder.Value == Metadata { return sync(metadataAtIndexInTransaction(index)) } @@ -69,12 +69,12 @@ extension Readable where - returns: an array of `Metadata` */ public func metadataAtIndexes< - Indexes, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Metadata>(_ indexes: Indexes) -> [Metadata?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [Metadata] { + Metadata.Coder.Value == Metadata { return sync(metadataAtIndexesInTransaction(indexes)) } } diff --git a/Sources/Persistable_ObjectWithNoMetadata.swift b/Sources/Persistable_ObjectWithNoMetadata.swift index e390fd0..52072d6 100644 --- a/Sources/Persistable_ObjectWithNoMetadata.swift +++ b/Sources/Persistable_ObjectWithNoMetadata.swift @@ -22,7 +22,7 @@ extension Persistable where - parameter transaction: a YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write(transaction: WriteTransaction) -> Self { + public func write(_ transaction: WriteTransaction) -> Self { return transaction.write(self) } @@ -32,7 +32,7 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: the receiver. */ - public func write(connection: Connection) -> Self { + public func write(_ connection: Connection) -> Self { return connection.write(self) } @@ -42,7 +42,7 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (Self -> Void)? = .None) { + public func asyncWrite(_ connection: Connection, queue: DispatchQueue = DispatchQueue.main, completion: ((Self) -> Void)? = .none) { return connection.asyncWrite(self, queue: queue, completion: completion) } @@ -52,14 +52,14 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation(connection: Connection) -> NSOperation { - return NSBlockOperation { connection.write(self) } + public func writeOperation(_ connection: Connection) -> Operation { + return BlockOperation { _ = connection.write(self) } } } -extension SequenceType where - Generator.Element: Persistable, - Generator.Element: NSCoding { +extension Sequence where + Iterator.Element: Persistable, + Iterator.Element: NSCoding { /** Write the items using an existing transaction. @@ -67,7 +67,7 @@ extension SequenceType where - parameter transaction: a WriteTransactionType e.g. YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write(transaction: WriteTransaction) -> [Generator.Element] { + public func write(_ transaction: WriteTransaction) -> [Iterator.Element] { return transaction.write(self) } @@ -77,7 +77,7 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: the receiver. */ - public func write(connection: Connection) -> [Generator.Element] { + public func write(_ connection: Connection) -> [Iterator.Element] { return connection.write(self) } @@ -87,7 +87,7 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([Generator.Element] -> Void)? = .None) { + public func asyncWrite(_ connection: Connection, queue: DispatchQueue = DispatchQueue.main, completion: (([Iterator.Element]) -> Void)? = .none) { return connection.asyncWrite(self, queue: queue, completion: completion) } @@ -97,8 +97,8 @@ extension SequenceType where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation(connection: Connection) -> NSOperation { - return NSBlockOperation { connection.write(self) } + public func writeOperation(_ connection: Connection) -> Operation { + return BlockOperation { _ = connection.write(self) } } } @@ -108,43 +108,43 @@ extension Readable where ItemType: NSCoding, ItemType: Persistable { - func inTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> ItemType? { + func inTransaction(_ transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> ItemType? { return transaction.readAtIndex(index) } - func inTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> ItemType? { + func inTransactionAtIndex(_ transaction: Database.Connection.ReadTransaction) -> (YapDB.Index) -> ItemType? { return { self.inTransaction(transaction, atIndex: $0) } } - func atIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> ItemType? { + func atIndexInTransaction(_ index: YapDB.Index) -> (Database.Connection.ReadTransaction) -> ItemType? { return { self.inTransaction($0, atIndex: index) } } func atIndexesInTransaction< - Indexes where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [ItemType] { + Indexes>(_ indexes: Indexes) -> (Database.Connection.ReadTransaction) -> [ItemType?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index { let atIndex = inTransactionAtIndex - return { indexes.flatMap(atIndex($0)) } + return { indexes.map(atIndex($0)) } } - func inTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> ItemType? { + func inTransaction(_ transaction: Database.Connection.ReadTransaction, byKey key: String) -> ItemType? { return inTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } - func inTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> ItemType? { + func inTransactionByKey(_ transaction: Database.Connection.ReadTransaction) -> (String) -> ItemType? { return { self.inTransaction(transaction, byKey: $0) } } - func byKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> ItemType? { + func byKeyInTransaction(_ key: String) -> (Database.Connection.ReadTransaction) -> ItemType? { return { self.inTransaction($0, byKey: key) } } - func byKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [ItemType] { + func byKeysInTransaction(_ keys: [String]? = .none) -> (Database.Connection.ReadTransaction) -> [ItemType?] { let byKey = inTransactionByKey return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) - return keys.flatMap(byKey(transaction)) + return keys.map(byKey(transaction)) } } @@ -154,7 +154,7 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func atIndex(index: YapDB.Index) -> ItemType? { + public func atIndex(_ index: YapDB.Index) -> ItemType? { return sync(atIndexInTransaction(index)) } @@ -165,9 +165,9 @@ extension Readable where - returns: an array of `ItemType` */ public func atIndexes< - Indexes where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> [ItemType] { + Indexes>(_ indexes: Indexes) -> [ItemType?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index { return sync(atIndexesInTransaction(indexes)) } @@ -177,7 +177,7 @@ extension Readable where - parameter key: a String - returns: an optional `ItemType` */ - public func byKey(key: String) -> ItemType? { + public func byKey(_ key: String) -> ItemType? { return sync(byKeyInTransaction(key)) } @@ -188,9 +188,9 @@ extension Readable where - returns: an array of `ItemType` */ public func byKeys< - Keys where - Keys: SequenceType, - Keys.Generator.Element == String>(keys: Keys) -> [ItemType] { + Keys>(_ keys: Keys) -> [ItemType?] where + Keys: Sequence, + Keys.Iterator.Element == String { return sync(byKeysInTransaction(Array(keys))) } @@ -199,7 +199,7 @@ extension Readable where - returns: an array of `ItemType` */ - public func all() -> [ItemType] { + public func all() -> [ItemType?] { return sync(byKeysInTransaction()) } @@ -209,12 +209,18 @@ extension Readable where - parameter keys: a SequenceType of String values - returns: a tuple of type `([ItemType], [String])` */ - public func filterExisting(keys: [String]) -> (existing: [ItemType], missing: [String]) { + public func filterExisting(_ keys: [String]) -> (existing: [ItemType], missing: [String]) { let existingInTransaction = byKeysInTransaction(keys) return sync { transaction -> ([ItemType], [String]) in - let existing = existingInTransaction(transaction) - let existingKeys = existing.map(keyForPersistable) - let missingKeys = keys.filter { !existingKeys.contains($0) } + var missingKeys = [String]() + let maybeExisting = existingInTransaction(transaction) + let existing = zip(keys, maybeExisting).flatMap { zipped -> ItemType? in + guard let item = zipped.1 else { + missingKeys.append(zipped.0) + return nil + } + return item + } return (existing, missingKeys) } } diff --git a/Sources/Persistable_ObjectWithObjectMetadata.swift b/Sources/Persistable_ObjectWithObjectMetadata.swift index abc3f22..4f33c88 100644 --- a/Sources/Persistable_ObjectWithObjectMetadata.swift +++ b/Sources/Persistable_ObjectWithObjectMetadata.swift @@ -22,9 +22,9 @@ extension Persistable where - returns: the receiver. */ public func writeWithMetadata< - WriteTransaction, Metadata where + WriteTransaction, Metadata>(_ transaction: WriteTransaction, metadata: Metadata?) -> YapItem where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata?) -> YapItem { + Metadata: NSCoding { return transaction.writeWithMetadata(YapItem(self, metadata)) } @@ -35,9 +35,9 @@ extension Persistable where - returns: the receiver. */ public func writeWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: Metadata?) -> YapItem where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> YapItem { + Metadata: NSCoding { return connection.writeWithMetadata(YapItem(self, metadata)) } @@ -48,9 +48,9 @@ extension Persistable where - returns: a closure which receives as an argument the receiver of this function. */ public func asyncWriteWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: Metadata?, queue: DispatchQueue = DispatchQueue.main, completion: ((YapItem) -> Void)? = .none) where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { + Metadata: NSCoding { return connection.asyncWriteWithMetadata(YapItem(self, metadata), queue: queue, completion: completion) } @@ -61,16 +61,16 @@ extension Persistable where - returns: an `NSOperation` */ public func writeWithMetadataOperation< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: Metadata?) -> Operation where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> NSOperation { - return NSBlockOperation { connection.writeWithMetadata(YapItem(self, metadata)) } + Metadata: NSCoding { + return BlockOperation { _ = connection.writeWithMetadata(YapItem(self, metadata)) } } } -extension SequenceType where - Generator.Element: Persistable, - Generator.Element: NSCoding { +extension Sequence where + Iterator.Element: Persistable, + Iterator.Element: NSCoding { /** Zips the receiver with metadata into an array of YapItem. @@ -80,10 +80,10 @@ extension SequenceType where - returns: an array where Persistables and Metadata with corresponding indexes in `self` and `metadata` are joined in a `YapItem` */ public func yapItems< - Metadatas, Metadata where + Metadatas, Metadata>(with metadata: Metadatas) -> [YapItem] where Metadata: NSCoding, - Metadatas: SequenceType, - Metadatas.Generator.Element == Optional>(with metadata: Metadatas) -> [YapItem] { + Metadatas: Sequence, + Metadatas.Iterator.Element == Optional { return zip(self, metadata).map { YapItem($0, $1) } } @@ -94,9 +94,9 @@ extension SequenceType where - returns: the receiver. */ public func writeWithMetadata< - WriteTransaction, Metadata where + WriteTransaction, Metadata>(_ transaction: WriteTransaction, metadata: [Metadata?]) -> [YapItem] where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?]) -> [YapItem] { + Metadata: NSCoding { let items = yapItems(with: metadata) return transaction.writeWithMetadata(items) } @@ -108,9 +108,9 @@ extension SequenceType where - returns: the receiver. */ public func writeWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: [Metadata?]) -> [YapItem] where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> [YapItem] { + Metadata: NSCoding { let items = yapItems(with: metadata) return connection.writeWithMetadata(items) } @@ -122,9 +122,9 @@ extension SequenceType where - returns: a closure which receives as an argument the receiver of this function. */ public func asyncWriteWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: [Metadata?], queue: DispatchQueue = DispatchQueue.main, completion: (([YapItem]) -> Void)? = .none) where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { + Metadata: NSCoding { let items = yapItems(with: metadata) return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } @@ -136,11 +136,11 @@ extension SequenceType where - returns: an `NSOperation` */ public func writeWithMetadataOperation< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: [Metadata?]) -> Operation where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> NSOperation { + Metadata: NSCoding { let items = yapItems(with: metadata) - return NSBlockOperation { connection.writeWithMetadata(items) } + return BlockOperation { _ = connection.writeWithMetadata(items) } } } @@ -151,42 +151,42 @@ extension Readable where ItemType: NSCoding, ItemType: Persistable { - func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> YapItem? { + func withMetadataInTransaction(_ transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> YapItem? { return transaction.readWithMetadataAtIndex(index) } - func withMetadataInTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> YapItem? { + func withMetadataInTransactionAtIndex(_ transaction: Database.Connection.ReadTransaction) -> (YapDB.Index) -> YapItem? { return { self.withMetadataInTransaction(transaction, atIndex: $0) } } - func withMetadataAtIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> YapItem? { + func withMetadataAtIndexInTransaction(_ index: YapDB.Index) -> (Database.Connection.ReadTransaction) -> YapItem? { return { self.withMetadataInTransaction($0, atIndex: index) } } func withMetadataAtIndexesInTransaction< - Indexes, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, - Metadata: NSCoding>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [YapItem] { - return { indexes.flatMap(self.withMetadataInTransactionAtIndex($0)) } + Indexes, Metadata>(_ indexes: Indexes) -> (Database.Connection.ReadTransaction) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, + Metadata: NSCoding { + return { indexes.map(self.withMetadataInTransactionAtIndex($0)) } } - func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> YapItem? { + func withMetadataInTransaction(_ transaction: Database.Connection.ReadTransaction, byKey key: String) -> YapItem? { return withMetadataInTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } - func withMetadataInTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> YapItem? { + func withMetadataInTransactionByKey(_ transaction: Database.Connection.ReadTransaction) -> (String) -> YapItem? { return { self.withMetadataInTransaction(transaction, byKey: $0) } } - func withMetadataByKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> YapItem? { + func withMetadataByKeyInTransaction(_ key: String) -> (Database.Connection.ReadTransaction) -> YapItem? { return { self.withMetadataInTransaction($0, byKey: key) } } - func withMetadataByKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [YapItem] { + func withMetadataByKeysInTransaction(_ keys: [String]? = .none) -> (Database.Connection.ReadTransaction) -> [YapItem?] { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) - return keys.flatMap(self.withMetadataInTransactionByKey(transaction)) + return keys.map(self.withMetadataInTransactionByKey(transaction)) } } @@ -196,7 +196,7 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func withMetadataAtIndex(index: YapDB.Index) -> YapItem? { + public func withMetadataAtIndex(_ index: YapDB.Index) -> YapItem? { return sync(withMetadataAtIndexInTransaction(index)) } @@ -207,10 +207,10 @@ extension Readable where - returns: an array of `ItemType` */ public func withMetadataAtIndexes< - Indexes, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, - Metadata: NSCoding>(indexes: Indexes) -> [YapItem] { + Indexes, Metadata>(_ indexes: Indexes) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, + Metadata: NSCoding { return sync(withMetadataAtIndexesInTransaction(indexes)) } @@ -220,7 +220,7 @@ extension Readable where - parameter key: a String - returns: an optional `ItemType` */ - public func withMetadataByKey(key: String) -> YapItem? { + public func withMetadataByKey(_ key: String) -> YapItem? { return sync(withMetadataByKeyInTransaction(key)) } @@ -231,10 +231,10 @@ extension Readable where - returns: an array of `ItemType` */ public func withMetadataByKeys< - Keys, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, - Metadata: NSCoding>(keys: Keys) -> [YapItem] { + Keys, Metadata>(_ keys: Keys) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, + Metadata: NSCoding { return sync(withMetadataByKeysInTransaction(Array(keys))) } @@ -243,7 +243,7 @@ extension Readable where - returns: an array of `ItemType` */ - public func withMetadataAll() -> [YapItem] { + public func withMetadataAll() -> [YapItem?] { return sync(withMetadataByKeysInTransaction()) } @@ -253,12 +253,18 @@ extension Readable where - parameter keys: a SequenceType of String values - returns: a tuple of type `([ItemType], [String])` */ - public func withMetadataFilterExisting(keys: [String]) -> (existing: [YapItem], missing: [String]) { - let existingInTransaction: Database.Connection.ReadTransaction -> [YapItem] = withMetadataByKeysInTransaction(keys) + public func withMetadataFilterExisting(_ keys: [String]) -> (existing: [YapItem], missing: [String]) { + let existingInTransaction: (Database.Connection.ReadTransaction) -> [YapItem?] = withMetadataByKeysInTransaction(keys) return sync { transaction -> ([YapItem], [String]) in - let existing = existingInTransaction(transaction) - let existingKeys = existing.map {keyForPersistable($0.value)} - let missingKeys = keys.filter { !existingKeys.contains($0) } + var missingKeys = [String]() + let maybeExisting = existingInTransaction(transaction) + let existing = zip(keys, maybeExisting).flatMap { zipped -> YapItem? in + guard let item = zipped.1 else { + missingKeys.append(zipped.0) + return nil + } + return item + } return (existing, missingKeys) } } diff --git a/Sources/Persistable_ObjectWithValueMetadata.swift b/Sources/Persistable_ObjectWithValueMetadata.swift index 3ca949f..e71fa2e 100644 --- a/Sources/Persistable_ObjectWithValueMetadata.swift +++ b/Sources/Persistable_ObjectWithValueMetadata.swift @@ -23,11 +23,11 @@ extension Persistable where - returns: the receiver. */ public func writeWithMetadata< - WriteTransaction, Metadata where + WriteTransaction, Metadata>(_ transaction: WriteTransaction, metadata: Metadata?) -> YapItem where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata?) -> YapItem { + Metadata.Coder.Value == Metadata { return transaction.writeWithMetadata(YapItem(self, metadata)) } @@ -38,11 +38,11 @@ extension Persistable where - returns: the receiver. */ public func writeWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: Metadata?) -> YapItem where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> YapItem { + Metadata.Coder.Value == Metadata { return connection.writeWithMetadata(YapItem(self, metadata)) } @@ -53,11 +53,11 @@ extension Persistable where - returns: a closure which receives as an argument the receiver of this function. */ public func asyncWriteWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: Metadata?, queue: DispatchQueue = DispatchQueue.main, completion: ((YapItem) -> Void)? = .none) where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { + Metadata.Coder.Value == Metadata { return connection.asyncWriteWithMetadata(YapItem(self, metadata), queue: queue, completion: completion) } @@ -68,18 +68,18 @@ extension Persistable where - returns: an `NSOperation` */ public func writeWithMetadataOperation< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: Metadata?) -> Operation where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> NSOperation { - return NSBlockOperation { connection.writeWithMetadata(YapItem(self, metadata)) } + Metadata.Coder.Value == Metadata { + return BlockOperation { _ = connection.writeWithMetadata(YapItem(self, metadata)) } } } -extension SequenceType where - Generator.Element: Persistable, - Generator.Element: NSCoding { +extension Sequence where + Iterator.Element: Persistable, + Iterator.Element: NSCoding { /** Zips the receiver with metadata into an array of YapItem. @@ -89,12 +89,12 @@ extension SequenceType where - returns: an array where Persistables and Metadata with corresponding indexes in `self` and `metadata` are joined in a `YapItem` */ public func yapItems< - Metadatas, Metadata where + Metadatas, Metadata>(with metadata: Metadatas) -> [YapItem] where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata, - Metadatas: SequenceType, - Metadatas.Generator.Element == Optional>(with metadata: Metadatas) -> [YapItem] { + Metadata.Coder.Value == Metadata, + Metadatas: Sequence, + Metadatas.Iterator.Element == Optional { return zip(self, metadata).map { YapItem($0, $1) } } @@ -105,11 +105,11 @@ extension SequenceType where - returns: the receiver. */ public func writeWithMetadata< - WriteTransaction, Metadata where + WriteTransaction, Metadata>(_ transaction: WriteTransaction, metadata: [Metadata?]) -> [YapItem] where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?]) -> [YapItem] { + Metadata.Coder.Value == Metadata { let items = yapItems(with: metadata) return transaction.writeWithMetadata(items) } @@ -121,11 +121,11 @@ extension SequenceType where - returns: the receiver. */ public func writeWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: [Metadata?]) -> [YapItem] where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> [YapItem] { + Metadata.Coder.Value == Metadata { let items = yapItems(with: metadata) return connection.writeWithMetadata(items) } @@ -137,11 +137,11 @@ extension SequenceType where - returns: a closure which receives as an argument the receiver of this function. */ public func asyncWriteWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: [Metadata?], queue: DispatchQueue = DispatchQueue.main, completion: (([YapItem]) -> Void)? = .none) where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { + Metadata.Coder.Value == Metadata { let items = yapItems(with: metadata) return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } @@ -153,13 +153,13 @@ extension SequenceType where - returns: an `NSOperation` */ public func writeWithMetadataOperation< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: [Metadata?]) -> Operation where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> NSOperation { + Metadata.Coder.Value == Metadata { let items = yapItems(with: metadata) - return NSBlockOperation { connection.writeWithMetadata(items) } + return BlockOperation { _ = connection.writeWithMetadata(items) } } } @@ -170,71 +170,71 @@ extension Readable where ItemType: Persistable { func withMetadataInTransaction< - Metadata where + Metadata>(_ transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> YapItem? { + Metadata.Coder.Value == Metadata { return transaction.readWithMetadataAtIndex(index) } func withMetadataInTransactionAtIndex< - Metadata where + Metadata>(_ transaction: Database.Connection.ReadTransaction) -> (YapDB.Index) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> YapItem? { + Metadata.Coder.Value == Metadata { return { self.withMetadataInTransaction(transaction, atIndex: $0) } } func withMetadataAtIndexInTransaction< - Metadata where + Metadata>(_ index: YapDB.Index) -> (Database.Connection.ReadTransaction) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> Database.Connection.ReadTransaction -> YapItem? { + Metadata.Coder.Value == Metadata { return { self.withMetadataInTransaction($0, atIndex: index) } } func withMetadataAtIndexesInTransaction< - Indexes, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Metadata>(_ indexes: Indexes) -> (Database.Connection.ReadTransaction) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [YapItem] { - return { indexes.flatMap(self.withMetadataInTransactionAtIndex($0)) } + Metadata.Coder.Value == Metadata { + return { indexes.map(self.withMetadataInTransactionAtIndex($0)) } } func withMetadataInTransaction< - Metadata where + Metadata>(_ transaction: Database.Connection.ReadTransaction, byKey key: String) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, byKey key: String) -> YapItem? { + Metadata.Coder.Value == Metadata { return withMetadataInTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } func withMetadataInTransactionByKey< - Metadata where + Metadata>(_ transaction: Database.Connection.ReadTransaction) -> (String) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> String -> YapItem? { + Metadata.Coder.Value == Metadata { return { self.withMetadataInTransaction(transaction, byKey: $0) } } func withMetadataByKeyInTransaction< - Metadata where + Metadata>(_ key: String) -> (Database.Connection.ReadTransaction) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> Database.Connection.ReadTransaction -> YapItem? { + Metadata.Coder.Value == Metadata { return { self.withMetadataInTransaction($0, byKey: key) } } func withMetadataByKeysInTransaction< - Metadata where + Metadata>(_ keys: [String]? = .none) -> (Database.Connection.ReadTransaction) -> [YapItem?] where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [YapItem] { + Metadata.Coder.Value == Metadata { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) - return keys.flatMap(self.withMetadataInTransactionByKey(transaction)) + return keys.map(self.withMetadataInTransactionByKey(transaction)) } } @@ -245,10 +245,10 @@ extension Readable where - returns: an optional `ItemType` */ public func withMetadataAtIndex< - Metadata where + Metadata>(_ index: YapDB.Index) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> YapItem? { + Metadata.Coder.Value == Metadata { return sync(withMetadataAtIndexInTransaction(index)) } @@ -259,12 +259,12 @@ extension Readable where - returns: an array of `ItemType` */ public func withMetadataAtIndexes< - Indexes, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Metadata>(_ indexes: Indexes) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [YapItem] { + Metadata.Coder.Value == Metadata { return sync(withMetadataAtIndexesInTransaction(indexes)) } @@ -275,10 +275,10 @@ extension Readable where - returns: an optional `ItemType` */ public func withMetadataByKey< - Metadata where + Metadata>(_ key: String) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> YapItem? { + Metadata.Coder.Value == Metadata { return sync(withMetadataByKeyInTransaction(key)) } @@ -289,12 +289,12 @@ extension Readable where - returns: an array of `ItemType` */ public func withMetadataByKeys< - Keys, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, Metadata>(_ keys: Keys) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [YapItem] { + Metadata.Coder.Value == Metadata { return sync(withMetadataByKeysInTransaction(Array(keys))) } @@ -304,10 +304,10 @@ extension Readable where - returns: an array of `ItemType` */ public func withMetadataAll< - Metadata where + Metadata>() -> [YapItem?] where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>() -> [YapItem] { + Metadata.Coder.Value == Metadata { return sync(withMetadataByKeysInTransaction()) } @@ -318,15 +318,21 @@ extension Readable where - returns: a tuple of type `([ItemType], [String])` */ public func withMetadataFilterExisting< - Metadata where + Metadata>(_ keys: [String]) -> (existing: [YapItem], missing: [String]) where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: [String]) -> (existing: [YapItem], missing: [String]) { - let existingInTransaction: Database.Connection.ReadTransaction -> [YapItem] = withMetadataByKeysInTransaction(keys) + Metadata.Coder.Value == Metadata { + let existingInTransaction: (Database.Connection.ReadTransaction) -> [YapItem?] = withMetadataByKeysInTransaction(keys) return sync { transaction -> ([YapItem], [String]) in - let existing = existingInTransaction(transaction) - let existingKeys = existing.map {keyForPersistable($0.value)} - let missingKeys = keys.filter { !existingKeys.contains($0) } + var missingKeys = [String]() + let maybeExisting = existingInTransaction(transaction) + let existing = zip(keys, maybeExisting).flatMap { zipped -> YapItem? in + guard let item = zipped.1 else { + missingKeys.append(zipped.0) + return nil + } + return item + } return (existing, missingKeys) } } diff --git a/Sources/Persistable_Remove.swift b/Sources/Persistable_Remove.swift index c99913f..62b1ba9 100644 --- a/Sources/Persistable_Remove.swift +++ b/Sources/Persistable_Remove.swift @@ -12,7 +12,7 @@ extension Persistable { - parameter transaction: a `WriteTransactionType` transaction */ - public func remove(transaction: WriteTransaction) { + public func remove(_ transaction: WriteTransaction) { transaction.remove(self) } @@ -21,7 +21,7 @@ extension Persistable { - parameter connection: a `ConnectionType` connection */ - public func remove(connection: Connection) { + public func remove(_ connection: Connection) { connection.remove(self) } @@ -30,7 +30,7 @@ extension Persistable { - parameter connection: a `ConnectionType` connection */ - public func asyncRemove(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: dispatch_block_t? = .None) { + public func asyncRemove(_ connection: Connection, queue: DispatchQueue = DispatchQueue.main, completion: (()->())? = .none) { connection.asyncRemove(self, queue: queue, completion: completion) } @@ -40,20 +40,20 @@ extension Persistable { - parameter connection: a `ConnectionType` connection */ - public func removeOperation(connection: Connection) -> NSOperation { - return NSBlockOperation { connection.remove(self) } + public func removeOperation(_ connection: Connection) -> Operation { + return BlockOperation { connection.remove(self) } } } -extension SequenceType where -Generator.Element: Persistable { +extension Sequence where +Iterator.Element: Persistable { /** Removes the receiver using the write transaction. - parameter transaction: a `WriteTransactionType` transaction */ - public func remove(transaction: WriteTransaction) { + public func remove(_ transaction: WriteTransaction) { transaction.remove(self) } @@ -62,7 +62,7 @@ Generator.Element: Persistable { - parameter connection: a `ConnectionType` connection */ - public func remove(connection: Connection) { + public func remove(_ connection: Connection) { connection.remove(self) } @@ -71,7 +71,7 @@ Generator.Element: Persistable { - parameter connection: a `ConnectionType` connection */ - public func asyncRemove(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: dispatch_block_t? = .None) { + public func asyncRemove(_ connection: Connection, queue: DispatchQueue = DispatchQueue.main, completion: (()->())? = .none) { connection.asyncRemove(self, queue: queue, completion: completion) } @@ -81,8 +81,8 @@ Generator.Element: Persistable { - parameter connection: a `ConnectionType` connection */ - public func removeOperation(connection: Connection) -> NSOperation { - return NSBlockOperation { connection.remove(self) } + public func removeOperation(_ connection: Connection) -> Operation { + return BlockOperation { connection.remove(self) } } } diff --git a/Sources/Persistable_ValueWithNoMetadata.swift b/Sources/Persistable_ValueWithNoMetadata.swift index 3e19487..40f8225 100644 --- a/Sources/Persistable_ValueWithNoMetadata.swift +++ b/Sources/Persistable_ValueWithNoMetadata.swift @@ -14,7 +14,7 @@ import ValueCoding extension Persistable where Self: ValueCoding, Self.Coder: NSCoding, - Self.Coder.ValueType == Self { + Self.Coder.Value == Self { // Writing @@ -24,7 +24,7 @@ extension Persistable where - parameter transaction: a YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write(transaction: WriteTransaction) -> Self { + public func write(_ transaction: WriteTransaction) -> Self { return transaction.write(self) } @@ -34,7 +34,7 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: the receiver. */ - public func write(connection: Connection) -> Self { + public func write(_ connection: Connection) -> Self { return connection.write(self) } @@ -44,7 +44,7 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (Self -> Void)? = .None) { + public func asyncWrite(_ connection: Connection, queue: DispatchQueue = DispatchQueue.main, completion: ((Self) -> Void)? = .none) { return connection.asyncWrite(self, queue: queue, completion: completion) } @@ -54,16 +54,16 @@ extension Persistable where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation(connection: Connection) -> NSOperation { - return NSBlockOperation { connection.write(self) } + public func writeOperation(_ connection: Connection) -> Operation { + return BlockOperation { _ = connection.write(self) } } } -extension SequenceType where - Generator.Element: Persistable, - Generator.Element: ValueCoding, - Generator.Element.Coder: NSCoding, - Generator.Element.Coder.ValueType == Generator.Element { +extension Sequence where + Iterator.Element: Persistable, + Iterator.Element: ValueCoding, + Iterator.Element.Coder: NSCoding, + Iterator.Element.Coder.Value == Iterator.Element { /** Write the items using an existing transaction. @@ -71,7 +71,7 @@ extension SequenceType where - parameter transaction: a WriteTransactionType e.g. YapDatabaseReadWriteTransaction - returns: the receiver. */ - public func write(transaction: WriteTransaction) -> [Generator.Element] { + public func write(_ transaction: WriteTransaction) -> [Iterator.Element] { return transaction.write(self) } @@ -81,7 +81,7 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: the receiver. */ - public func write(connection: Connection) -> [Generator.Element] { + public func write(_ connection: Connection) -> [Iterator.Element] { return connection.write(self) } @@ -91,7 +91,7 @@ extension SequenceType where - parameter connection: a ConnectionType e.g. YapDatabaseConnection - returns: a closure which receives as an argument the receiver of this function. */ - public func asyncWrite(connection: Connection, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([Generator.Element] -> Void)? = .None) { + public func asyncWrite(_ connection: Connection, queue: DispatchQueue = DispatchQueue.main, completion: (([Iterator.Element]) -> Void)? = .none) { return connection.asyncWrite(self, queue: queue, completion: completion) } @@ -101,8 +101,8 @@ extension SequenceType where - parameter connection: a YapDatabaseConnection - returns: an `NSOperation` */ - public func writeOperation(connection: Connection) -> NSOperation { - return NSBlockOperation { connection.write(self) } + public func writeOperation(_ connection: Connection) -> Operation { + return BlockOperation { _ = connection.write(self) } } } @@ -112,45 +112,45 @@ extension Readable where ItemType: ValueCoding, ItemType: Persistable, ItemType.Coder: NSCoding, - ItemType.Coder.ValueType == ItemType { + ItemType.Coder.Value == ItemType { - func inTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> ItemType? { + func inTransaction(_ transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> ItemType? { return transaction.readAtIndex(index) } - func inTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> ItemType? { + func inTransactionAtIndex(_ transaction: Database.Connection.ReadTransaction) -> (YapDB.Index) -> ItemType? { return { self.inTransaction(transaction, atIndex: $0) } } - func atIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> ItemType? { + func atIndexInTransaction(_ index: YapDB.Index) -> (Database.Connection.ReadTransaction) -> ItemType? { return { self.inTransaction($0, atIndex: index) } } func atIndexesInTransaction< - Indexes where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [ItemType] { + Indexes>(_ indexes: Indexes) -> (Database.Connection.ReadTransaction) -> [ItemType?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index { let atIndex = inTransactionAtIndex - return { indexes.flatMap(atIndex($0)) } + return { indexes.map(atIndex($0)) } } - func inTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> ItemType? { + func inTransaction(_ transaction: Database.Connection.ReadTransaction, byKey key: String) -> ItemType? { return inTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } - func inTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> ItemType? { + func inTransactionByKey(_ transaction: Database.Connection.ReadTransaction) -> (String) -> ItemType? { return { self.inTransaction(transaction, byKey: $0) } } - func byKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> ItemType? { + func byKeyInTransaction(_ key: String) -> (Database.Connection.ReadTransaction) -> ItemType? { return { self.inTransaction($0, byKey: key) } } - func byKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [ItemType] { + func byKeysInTransaction(_ keys: [String]? = .none) -> (Database.Connection.ReadTransaction) -> [ItemType?] { let byKey = inTransactionByKey return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) - return keys.flatMap(byKey(transaction)) + return keys.map(byKey(transaction)) } } @@ -160,7 +160,7 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func atIndex(index: YapDB.Index) -> ItemType? { + public func atIndex(_ index: YapDB.Index) -> ItemType? { return sync(atIndexInTransaction(index)) } @@ -171,9 +171,9 @@ extension Readable where - returns: an array of `ItemType` */ public func atIndexes< - Indexes where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) -> [ItemType] { + Indexes>(_ indexes: Indexes) -> [ItemType?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index { return sync(atIndexesInTransaction(indexes)) } @@ -183,7 +183,7 @@ extension Readable where - parameter key: a String - returns: an optional `ItemType` */ - public func byKey(key: String) -> ItemType? { + public func byKey(_ key: String) -> ItemType? { return sync(byKeyInTransaction(key)) } @@ -194,9 +194,9 @@ extension Readable where - returns: an array of `ItemType` */ public func byKeys< - Keys where - Keys: SequenceType, - Keys.Generator.Element == String>(keys: Keys) -> [ItemType] { + Keys>(_ keys: Keys) -> [ItemType?] where + Keys: Sequence, + Keys.Iterator.Element == String { return sync(byKeysInTransaction(Array(keys))) } @@ -205,7 +205,7 @@ extension Readable where - returns: an array of `ItemType` */ - public func all() -> [ItemType] { + public func all() -> [ItemType?] { return sync(byKeysInTransaction()) } @@ -215,12 +215,18 @@ extension Readable where - parameter keys: a SequenceType of String values - returns: a tuple of type `([ItemType], [String])` */ - public func filterExisting(keys: [String]) -> (existing: [ItemType], missing: [String]) { + public func filterExisting(_ keys: [String]) -> (existing: [ItemType], missing: [String]) { let existingInTransaction = byKeysInTransaction(keys) return sync { transaction -> ([ItemType], [String]) in - let existing = existingInTransaction(transaction) - let existingKeys = existing.map(keyForPersistable) - let missingKeys = keys.filter { !existingKeys.contains($0) } + var missingKeys = [String]() + let maybeExisting = existingInTransaction(transaction) + let existing = zip(keys, maybeExisting).flatMap { zipped -> ItemType? in + guard let item = zipped.1 else { + missingKeys.append(zipped.0) + return nil + } + return item + } return (existing, missingKeys) } } diff --git a/Sources/Persistable_ValueWithObjectMetadata.swift b/Sources/Persistable_ValueWithObjectMetadata.swift index bbabc82..4af8229 100644 --- a/Sources/Persistable_ValueWithObjectMetadata.swift +++ b/Sources/Persistable_ValueWithObjectMetadata.swift @@ -14,7 +14,7 @@ import ValueCoding extension Persistable where Self: ValueCoding, Self.Coder: NSCoding, - Self.Coder.ValueType == Self { + Self.Coder.Value == Self { // Writing @@ -25,9 +25,9 @@ extension Persistable where - returns: the receiver. */ public func writeWithMetadata< - WriteTransaction, Metadata where + WriteTransaction, Metadata>(_ transaction: WriteTransaction, metadata: Metadata?) -> YapItem where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: Metadata?) -> YapItem { + Metadata: NSCoding { return transaction.writeWithMetadata(YapItem(self, metadata)) } @@ -38,9 +38,9 @@ extension Persistable where - returns: the receiver. */ public func writeWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: Metadata?) -> YapItem where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> YapItem { + Metadata: NSCoding { return connection.writeWithMetadata(YapItem(self, metadata)) } @@ -51,9 +51,9 @@ extension Persistable where - returns: a closure which receives as an argument the receiver of this function. */ public func asyncWriteWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: Metadata?, queue: DispatchQueue = DispatchQueue.main, completion: ((YapItem) -> Void)? = .none) where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { + Metadata: NSCoding { return connection.asyncWriteWithMetadata(YapItem(self, metadata), queue: queue, completion: completion) } @@ -64,18 +64,18 @@ extension Persistable where - returns: an `NSOperation` */ public func writeWithMetadataOperation< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: Metadata?) -> Operation where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: Metadata?) -> NSOperation { - return NSBlockOperation { connection.writeWithMetadata(YapItem(self, metadata)) } + Metadata: NSCoding { + return BlockOperation { _ = connection.writeWithMetadata(YapItem(self, metadata)) } } } -extension SequenceType where - Generator.Element: Persistable, - Generator.Element: ValueCoding, - Generator.Element.Coder: NSCoding, - Generator.Element.Coder.ValueType == Generator.Element { +extension Sequence where + Iterator.Element: Persistable, + Iterator.Element: ValueCoding, + Iterator.Element.Coder: NSCoding, + Iterator.Element.Coder.Value == Iterator.Element { /** Zips the receiver with metadata into an array of YapItem. @@ -85,10 +85,10 @@ extension SequenceType where - returns: an array where Persistables and Metadata with corresponding indexes in `self` and `metadata` are joined in a `YapItem` */ public func yapItems< - Metadatas, Metadata where + Metadatas, Metadata>(with metadata: Metadatas) -> [YapItem] where Metadata: NSCoding, - Metadatas: SequenceType, - Metadatas.Generator.Element == Optional>(with metadata: Metadatas) -> [YapItem] { + Metadatas: Sequence, + Metadatas.Iterator.Element == Optional { return zip(self, metadata).map { YapItem($0, $1) } } @@ -99,9 +99,9 @@ extension SequenceType where - returns: the receiver. */ public func writeWithMetadata< - WriteTransaction, Metadata where + WriteTransaction, Metadata>(_ transaction: WriteTransaction, metadata: [Metadata?]) -> [YapItem] where WriteTransaction: WriteTransactionType, - Metadata: NSCoding>(transaction: WriteTransaction, metadata: [Metadata?]) -> [YapItem] { + Metadata: NSCoding { let items = yapItems(with: metadata) return transaction.writeWithMetadata(items) } @@ -113,9 +113,9 @@ extension SequenceType where - returns: the receiver. */ public func writeWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: [Metadata?]) -> [YapItem] where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> [YapItem] { + Metadata: NSCoding { let items = yapItems(with: metadata) return connection.writeWithMetadata(items) } @@ -127,9 +127,9 @@ extension SequenceType where - returns: a closure which receives as an argument the receiver of this function. */ public func asyncWriteWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: [Metadata?], queue: DispatchQueue = DispatchQueue.main, completion: (([YapItem]) -> Void)? = .none) where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { + Metadata: NSCoding { let items = yapItems(with: metadata) return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } @@ -141,11 +141,11 @@ extension SequenceType where - returns: an `NSOperation` */ public func writeWithMetadataOperation< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: [Metadata?]) -> Operation where Connection: ConnectionType, - Metadata: NSCoding>(connection: Connection, metadata: [Metadata?]) -> NSOperation { + Metadata: NSCoding { let items = yapItems(with: metadata) - return NSBlockOperation { connection.writeWithMetadata(items) } + return BlockOperation { _ = connection.writeWithMetadata(items) } } } @@ -156,44 +156,44 @@ extension Readable where ItemType: ValueCoding, ItemType: Persistable, ItemType.Coder: NSCoding, - ItemType.Coder.ValueType == ItemType { + ItemType.Coder.Value == ItemType { - func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> YapItem? { + func withMetadataInTransaction(_ transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> YapItem? { return transaction.readWithMetadataAtIndex(index) } - func withMetadataInTransactionAtIndex(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> YapItem? { + func withMetadataInTransactionAtIndex(_ transaction: Database.Connection.ReadTransaction) -> (YapDB.Index) -> YapItem? { return { self.withMetadataInTransaction(transaction, atIndex: $0) } } - func withMetadataAtIndexInTransaction(index: YapDB.Index) -> Database.Connection.ReadTransaction -> YapItem? { + func withMetadataAtIndexInTransaction(_ index: YapDB.Index) -> (Database.Connection.ReadTransaction) -> YapItem? { return { self.withMetadataInTransaction($0, atIndex: index) } } func withMetadataAtIndexesInTransaction< - Indexes, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, - Metadata: NSCoding>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [YapItem] { - return { indexes.flatMap(self.withMetadataInTransactionAtIndex($0)) } + Indexes, Metadata>(_ indexes: Indexes) -> (Database.Connection.ReadTransaction) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, + Metadata: NSCoding { + return { indexes.map(self.withMetadataInTransactionAtIndex($0)) } } - func withMetadataInTransaction(transaction: Database.Connection.ReadTransaction, byKey key: String) -> YapItem? { + func withMetadataInTransaction(_ transaction: Database.Connection.ReadTransaction, byKey key: String) -> YapItem? { return withMetadataInTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } - func withMetadataInTransactionByKey(transaction: Database.Connection.ReadTransaction) -> String -> YapItem? { + func withMetadataInTransactionByKey(_ transaction: Database.Connection.ReadTransaction) -> (String) -> YapItem? { return { self.withMetadataInTransaction(transaction, byKey: $0) } } - func withMetadataByKeyInTransaction(key: String) -> Database.Connection.ReadTransaction -> YapItem? { + func withMetadataByKeyInTransaction(_ key: String) -> (Database.Connection.ReadTransaction) -> YapItem? { return { self.withMetadataInTransaction($0, byKey: key) } } - func withMetadataByKeysInTransaction(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [YapItem] { + func withMetadataByKeysInTransaction(_ keys: [String]? = .none) -> (Database.Connection.ReadTransaction) -> [YapItem?] { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) - return keys.flatMap(self.withMetadataInTransactionByKey(transaction)) + return keys.map(self.withMetadataInTransactionByKey(transaction)) } } @@ -203,7 +203,7 @@ extension Readable where - parameter index: a YapDB.Index - returns: an optional `ItemType` */ - public func withMetadataAtIndex(index: YapDB.Index) -> YapItem? { + public func withMetadataAtIndex(_ index: YapDB.Index) -> YapItem? { return sync(withMetadataAtIndexInTransaction(index)) } @@ -214,10 +214,10 @@ extension Readable where - returns: an array of `ItemType` */ public func withMetadataAtIndexes< - Indexes, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, - Metadata: NSCoding>(indexes: Indexes) -> [YapItem] { + Indexes, Metadata>(_ indexes: Indexes) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, + Metadata: NSCoding { return sync(withMetadataAtIndexesInTransaction(indexes)) } @@ -227,7 +227,7 @@ extension Readable where - parameter key: a String - returns: an optional `ItemType` */ - public func withMetadataByKey(key: String) -> YapItem? { + public func withMetadataByKey(_ key: String) -> YapItem? { return sync(withMetadataByKeyInTransaction(key)) } @@ -238,10 +238,10 @@ extension Readable where - returns: an array of `ItemType` */ public func withMetadataByKeys< - Keys, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, - Metadata: NSCoding>(keys: Keys) -> [YapItem] { + Keys, Metadata>(_ keys: Keys) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, + Metadata: NSCoding { return sync(withMetadataByKeysInTransaction(Array(keys))) } @@ -250,7 +250,7 @@ extension Readable where - returns: an array of `ItemType` */ - public func withMetadataAll() -> [YapItem] { + public func withMetadataAll() -> [YapItem?] { return sync(withMetadataByKeysInTransaction()) } @@ -260,12 +260,18 @@ extension Readable where - parameter keys: a SequenceType of String values - returns: a tuple of type `([ItemType], [String])` */ - public func withMetadataFilterExisting(keys: [String]) -> (existing: [YapItem], missing: [String]) { - let existingInTransaction: Database.Connection.ReadTransaction -> [YapItem] = withMetadataByKeysInTransaction(keys) + public func withMetadataFilterExisting(_ keys: [String]) -> (existing: [YapItem], missing: [String]) { + let existingInTransaction: (Database.Connection.ReadTransaction) -> [YapItem?] = withMetadataByKeysInTransaction(keys) return sync { transaction -> ([YapItem], [String]) in - let existing = existingInTransaction(transaction) - let existingKeys = existing.map {keyForPersistable($0.value)} - let missingKeys = keys.filter { !existingKeys.contains($0) } + var missingKeys = [String]() + let maybeExisting = existingInTransaction(transaction) + let existing = zip(keys, maybeExisting).flatMap { zipped -> YapItem? in + guard let item = zipped.1 else { + missingKeys.append(zipped.0) + return nil + } + return item + } return (existing, missingKeys) } } diff --git a/Sources/Persistable_ValueWithValueMetadata.swift b/Sources/Persistable_ValueWithValueMetadata.swift index c7c4912..4d60aff 100644 --- a/Sources/Persistable_ValueWithValueMetadata.swift +++ b/Sources/Persistable_ValueWithValueMetadata.swift @@ -14,7 +14,7 @@ import ValueCoding extension Persistable where Self: ValueCoding, Self.Coder: NSCoding, - Self.Coder.ValueType == Self { + Self.Coder.Value == Self { // Writing @@ -25,11 +25,11 @@ extension Persistable where - returns: the receiver. */ public func writeWithMetadata< - WriteTransaction, Metadata where + WriteTransaction, Metadata>(_ transaction: WriteTransaction, metadata: Metadata?) -> YapItem where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: Metadata?) -> YapItem { + Metadata.Coder.Value == Metadata { return transaction.writeWithMetadata(YapItem(self, metadata)) } @@ -40,11 +40,11 @@ extension Persistable where - returns: the receiver. */ public func writeWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: Metadata?) -> YapItem where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> YapItem { + Metadata.Coder.Value == Metadata { return connection.writeWithMetadata(YapItem(self, metadata)) } @@ -55,11 +55,11 @@ extension Persistable where - returns: a closure which receives as an argument the receiver of this function. */ public func asyncWriteWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: Metadata?, queue: DispatchQueue = DispatchQueue.main, completion: ((YapItem) -> Void)? = .none) where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (YapItem -> Void)? = .None) { + Metadata.Coder.Value == Metadata { return connection.asyncWriteWithMetadata(YapItem(self, metadata), queue: queue, completion: completion) } @@ -70,20 +70,20 @@ extension Persistable where - returns: an `NSOperation` */ public func writeWithMetadataOperation< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: Metadata?) -> Operation where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: Metadata?) -> NSOperation { - return NSBlockOperation { connection.writeWithMetadata(YapItem(self, metadata)) } + Metadata.Coder.Value == Metadata { + return BlockOperation { _ = connection.writeWithMetadata(YapItem(self, metadata)) } } } -extension SequenceType where - Generator.Element: Persistable, - Generator.Element: ValueCoding, - Generator.Element.Coder: NSCoding, - Generator.Element.Coder.ValueType == Generator.Element { +extension Sequence where + Iterator.Element: Persistable, + Iterator.Element: ValueCoding, + Iterator.Element.Coder: NSCoding, + Iterator.Element.Coder.Value == Iterator.Element { /** Zips the receiver with metadata into an array of YapItem. @@ -93,12 +93,12 @@ extension SequenceType where - returns: an array where Persistables and Metadata with corresponding indexes in `self` and `metadata` are joined in a `YapItem` */ public func yapItems< - Metadatas, Metadata where + Metadatas, Metadata>(with metadata: Metadatas) -> [YapItem] where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata, - Metadatas: SequenceType, - Metadatas.Generator.Element == Optional>(with metadata: Metadatas) -> [YapItem] { + Metadata.Coder.Value == Metadata, + Metadatas: Sequence, + Metadatas.Iterator.Element == Optional { return zip(self, metadata).map { YapItem($0, $1) } } @@ -109,11 +109,11 @@ extension SequenceType where - returns: the receiver. */ public func writeWithMetadata< - WriteTransaction, Metadata where + WriteTransaction, Metadata>(_ transaction: WriteTransaction, metadata: [Metadata?]) -> [YapItem] where WriteTransaction: WriteTransactionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: WriteTransaction, metadata: [Metadata?]) -> [YapItem] { + Metadata.Coder.Value == Metadata { let items = yapItems(with: metadata) return transaction.writeWithMetadata(items) } @@ -125,11 +125,11 @@ extension SequenceType where - returns: the receiver. */ public func writeWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: [Metadata?]) -> [YapItem] where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> [YapItem] { + Metadata.Coder.Value == Metadata { let items = yapItems(with: metadata) return connection.writeWithMetadata(items) } @@ -141,11 +141,11 @@ extension SequenceType where - returns: a closure which receives as an argument the receiver of this function. */ public func asyncWriteWithMetadata< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: [Metadata?], queue: DispatchQueue = DispatchQueue.main, completion: (([YapItem]) -> Void)? = .none) where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?], queue: dispatch_queue_t = dispatch_get_main_queue(), completion: ([YapItem] -> Void)? = .None) { + Metadata.Coder.Value == Metadata { let items = yapItems(with: metadata) return connection.asyncWriteWithMetadata(items, queue: queue, completion: completion) } @@ -157,13 +157,13 @@ extension SequenceType where - returns: an `NSOperation` */ public func writeWithMetadataOperation< - Connection, Metadata where + Connection, Metadata>(_ connection: Connection, metadata: [Metadata?]) -> Operation where Connection: ConnectionType, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(connection: Connection, metadata: [Metadata?]) -> NSOperation { + Metadata.Coder.Value == Metadata { let items = yapItems(with: metadata) - return NSBlockOperation { connection.writeWithMetadata(items) } + return BlockOperation { _ = connection.writeWithMetadata(items) } } } @@ -173,74 +173,74 @@ extension Readable where ItemType: ValueCoding, ItemType: Persistable, ItemType.Coder: NSCoding, - ItemType.Coder.ValueType == ItemType { + ItemType.Coder.Value == ItemType { func withMetadataInTransaction< - Metadata where + Metadata>(_ transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, atIndex index: YapDB.Index) -> YapItem? { + Metadata.Coder.Value == Metadata { return transaction.readWithMetadataAtIndex(index) } func withMetadataInTransactionAtIndex< - Metadata where + Metadata>(_ transaction: Database.Connection.ReadTransaction) -> (YapDB.Index) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> YapDB.Index -> YapItem? { + Metadata.Coder.Value == Metadata { return { self.withMetadataInTransaction(transaction, atIndex: $0) } } func withMetadataAtIndexInTransaction< - Metadata where + Metadata>(_ index: YapDB.Index) -> (Database.Connection.ReadTransaction) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> Database.Connection.ReadTransaction -> YapItem? { + Metadata.Coder.Value == Metadata { return { self.withMetadataInTransaction($0, atIndex: index) } } func withMetadataAtIndexesInTransaction< - Indexes, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Metadata>(_ indexes: Indexes) -> (Database.Connection.ReadTransaction) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> Database.Connection.ReadTransaction -> [YapItem] { - return { indexes.flatMap(self.withMetadataInTransactionAtIndex($0)) } + Metadata.Coder.Value == Metadata { + return { indexes.map(self.withMetadataInTransactionAtIndex($0)) } } func withMetadataInTransaction< - Metadata where + Metadata>(_ transaction: Database.Connection.ReadTransaction, byKey key: String) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction, byKey key: String) -> YapItem? { + Metadata.Coder.Value == Metadata { return withMetadataInTransaction(transaction, atIndex: ItemType.indexWithKey(key)) } func withMetadataInTransactionByKey< - Metadata where + Metadata>(_ transaction: Database.Connection.ReadTransaction) -> (String) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(transaction: Database.Connection.ReadTransaction) -> String -> YapItem? { + Metadata.Coder.Value == Metadata { return { self.withMetadataInTransaction(transaction, byKey: $0) } } func withMetadataByKeyInTransaction< - Metadata where + Metadata>(_ key: String) -> (Database.Connection.ReadTransaction) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> Database.Connection.ReadTransaction -> YapItem? { + Metadata.Coder.Value == Metadata { return { self.withMetadataInTransaction($0, byKey: key) } } func withMetadataByKeysInTransaction< - Metadata where + Metadata>(_ keys: [String]? = .none) -> (Database.Connection.ReadTransaction) -> [YapItem?] where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: [String]? = .None) -> Database.Connection.ReadTransaction -> [YapItem] { + Metadata.Coder.Value == Metadata { return { transaction in let keys = keys ?? transaction.keysInCollection(ItemType.collection) - return keys.flatMap(self.withMetadataInTransactionByKey(transaction)) + return keys.map(self.withMetadataInTransactionByKey(transaction)) } } @@ -251,10 +251,10 @@ extension Readable where - returns: an optional `ItemType` */ public func withMetadataAtIndex< - Metadata where + Metadata>(_ index: YapDB.Index) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(index: YapDB.Index) -> YapItem? { + Metadata.Coder.Value == Metadata { return sync(withMetadataAtIndexInTransaction(index)) } @@ -265,12 +265,12 @@ extension Readable where - returns: an array of `ItemType` */ public func withMetadataAtIndexes< - Indexes, Metadata where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index, + Indexes, Metadata>(_ indexes: Indexes) -> [YapItem?] where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(indexes: Indexes) -> [YapItem] { + Metadata.Coder.Value == Metadata { return sync(withMetadataAtIndexesInTransaction(indexes)) } @@ -281,10 +281,10 @@ extension Readable where - returns: an optional `ItemType` */ public func withMetadataByKey< - Metadata where + Metadata>(_ key: String) -> YapItem? where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(key: String) -> YapItem? { + Metadata.Coder.Value == Metadata { return sync(withMetadataByKeyInTransaction(key)) } @@ -295,12 +295,12 @@ extension Readable where - returns: an array of `ItemType` */ public func withMetadataByKeys< - Keys, Metadata where - Keys: SequenceType, - Keys.Generator.Element == String, + Keys, Metadata>(_ keys: Keys) -> [YapItem?] where + Keys: Sequence, + Keys.Iterator.Element == String, Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: Keys) -> [YapItem] { + Metadata.Coder.Value == Metadata { return sync(withMetadataByKeysInTransaction(Array(keys))) } @@ -310,10 +310,10 @@ extension Readable where - returns: an array of `ItemType` */ public func withMetadataAll< - Metadata where + Metadata>() -> [YapItem?] where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>() -> [YapItem] { + Metadata.Coder.Value == Metadata { return sync(withMetadataByKeysInTransaction()) } @@ -324,15 +324,21 @@ extension Readable where - returns: a tuple of type `([ItemType], [String])` */ public func withMetadataFilterExisting< - Metadata where + Metadata>(_ keys: [String]) -> (existing: [YapItem], missing: [String]) where Metadata: ValueCoding, Metadata.Coder: NSCoding, - Metadata.Coder.ValueType == Metadata>(keys: [String]) -> (existing: [YapItem], missing: [String]) { - let existingInTransaction: Database.Connection.ReadTransaction -> [YapItem] = withMetadataByKeysInTransaction(keys) + Metadata.Coder.Value == Metadata { + let existingInTransaction: (Database.Connection.ReadTransaction) -> [YapItem?] = withMetadataByKeysInTransaction(keys) return sync { transaction -> ([YapItem], [String]) in - let existing = existingInTransaction(transaction) - let existingKeys = existing.map {keyForPersistable($0.value)} - let missingKeys = keys.filter { !existingKeys.contains($0) } + var missingKeys = [String]() + let maybeExisting = existingInTransaction(transaction) + let existing = zip(keys, maybeExisting).flatMap { zipped -> YapItem? in + guard let item = zipped.1 else { + missingKeys.append(zipped.0) + return nil + } + return item + } return (existing, missingKeys) } } diff --git a/Sources/Read.swift b/Sources/Read.swift index 4af4f59..d228c1f 100644 --- a/Sources/Read.swift +++ b/Sources/Read.swift @@ -26,17 +26,17 @@ public struct Read: Readable { let reader: Handle public var transaction: D.Connection.ReadTransaction? { - if case let .Transaction(transaction) = reader { + if case let .transaction(transaction) = reader { return transaction } - return .None + return .none } public var connection: D.Connection { switch reader { - case .Transaction(_): + case .transaction(_): fatalError("Attempting to get connection from a transaction.") - case .Connection(let connection): + case .connection(let connection): return connection default: return database.makeNewConnection() @@ -44,22 +44,22 @@ public struct Read: Readable { } internal var database: D { - if case let .Database(database) = reader { + if case let .database(database) = reader { return database } fatalError("Attempting to get database from \(reader)") } internal init(_ transaction: D.Connection.ReadTransaction) { - reader = .Transaction(transaction) + reader = .transaction(transaction) } internal init(_ connection: D.Connection) { - reader = .Connection(connection) + reader = .connection(connection) } internal init(_ database: D) { - reader = .Database(database) + reader = .database(database) } } @@ -83,7 +83,7 @@ extension Persistable { - parameter transaction: a type conforming to ReadTransactionType such as YapDatabaseReadTransaction */ - public static func read(transaction: D.Connection.ReadTransaction) -> Read { + public static func read(_ transaction: D.Connection.ReadTransaction) -> Read { return Read(transaction) } @@ -105,11 +105,11 @@ extension Persistable { - parameter connection: a type conforming to ConnectionType such as YapDatabaseConnection. */ - public static func read(connection: D.Connection) -> Read { + public static func read(_ connection: D.Connection) -> Read { return Read(connection) } - internal static func read(database: D) -> Read { + internal static func read(_ database: D) -> Read { return Read(database) } } @@ -118,7 +118,7 @@ extension Readable where ItemType: Persistable { - func sync(block: (Database.Connection.ReadTransaction) -> T) -> T { + func sync(_ block: @escaping (Database.Connection.ReadTransaction) -> T) -> T { if let transaction = transaction { return block(transaction) } diff --git a/Sources/YapDB.swift b/Sources/YapDB.swift index 7534482..bfe133e 100644 --- a/Sources/YapDB.swift +++ b/Sources/YapDB.swift @@ -2,13 +2,19 @@ // Created by Daniel Thorpe on 22/04/2015. // +import YapDatabase +import YapDatabase.YapDatabaseView +import YapDatabase.YapDatabaseFilteredView +import YapDatabase.YapDatabaseFullTextSearch +import YapDatabase.YapDatabaseSecondaryIndex + protocol YapDatabaseViewProducer { func createDatabaseView() -> YapDatabaseView } protocol YapDatabaseExtensionRegistrar { - func isRegisteredInDatabase(database: YapDatabase) -> Bool - func registerInDatabase(database: YapDatabase, withConnection: YapDatabaseConnection?) + func isRegisteredInDatabase(_ database: YapDatabase) -> Bool + func registerInDatabase(_ database: YapDatabase, withConnection: YapDatabaseConnection?) } extension YapDB { @@ -20,26 +26,26 @@ extension YapDB { */ public enum Fetch: YapDatabaseExtensionRegistrar { - case View(YapDB.View) - case Filter(YapDB.Filter) - case Search(YapDB.SearchResults) - case Index(YapDB.SecondaryIndex) + case view(YapDB.View) + case filter(YapDB.Filter) + case search(YapDB.SearchResults) + case index(YapDB.SecondaryIndex) public var name: String { switch self { - case let .View(view): return view.name - case let .Filter(filter): return filter.name - case let .Search(search): return search.name - case let .Index(index): return index.name + case let .view(view): return view.name + case let .filter(filter): return filter.name + case let .search(search): return search.name + case let .index(index): return index.name } } var registrar: YapDatabaseExtensionRegistrar { switch self { - case let .View(view): return view - case let .Filter(filter): return filter - case let .Search(search): return search - case let .Index(index): return index + case let .view(view): return view + case let .filter(filter): return filter + case let .search(search): return search + case let .index(index): return index } } @@ -49,7 +55,7 @@ extension YapDB { - parameter database: a YapDatabase instance - returns: a Bool */ - public func isRegisteredInDatabase(database: YapDatabase) -> Bool { + public func isRegisteredInDatabase(_ database: YapDatabase) -> Bool { return registrar.isRegisteredInDatabase(database) } @@ -59,7 +65,7 @@ extension YapDB { - parameter database: a YapDatabase instance - parameter connection: an optional YapDatabaseConnection, defaults to .None */ - public func registerInDatabase(database: YapDatabase, withConnection connection: YapDatabaseConnection? = .None) { + public func registerInDatabase(_ database: YapDatabase, withConnection connection: YapDatabaseConnection? = .none) { registrar.registerInDatabase(database, withConnection: connection) } @@ -69,7 +75,7 @@ extension YapDB { - parameter database: a YapDatabase instance - parameter connection: an optional YapDatabaseConnection, defaults to .None */ - public func createViewMappings(mappings: Mappings, inDatabase database: YapDatabase, withConnection connection: YapDatabaseConnection? = .None) -> YapDatabaseViewMappings { + public func createViewMappings(_ mappings: Mappings, inDatabase database: YapDatabase, withConnection connection: YapDatabaseConnection? = .none) -> YapDatabaseViewMappings { registerInDatabase(database, withConnection: connection) return mappings.createMappingsWithViewName(name) } @@ -78,13 +84,13 @@ extension YapDB { extension YapDB { - public class BaseExtension { + open class BaseExtension { let name: String let version: String let collections: Set? let persistent: Bool - init(name n: String, version v: String = "1.0", persistent p: Bool = true, collections c: [String]? = .None) { + init(name n: String, version v: String = "1.0", persistent p: Bool = true, collections c: [String]? = .none) { name = n version = v persistent = p @@ -97,15 +103,15 @@ extension YapDB { - parameter database: a YapDatabase instance - returns: A Bool */ - public func isRegisteredInDatabase(database: YapDatabase) -> Bool { - return (database.registeredExtension(name) as? YapDatabaseView) != .None + public func isRegisteredInDatabase(_ database: YapDatabase) -> Bool { + return (database.registeredExtension(name) as? YapDatabaseExtension) != .none } } /** The base class for other YapDatabaseView wrapper types. */ - public class BaseView: BaseExtension { + open class BaseView: BaseExtension { var options: YapDatabaseViewOptions { get { @@ -124,7 +130,7 @@ extension YapDB { A wrapper around YapDatabaseView. It can be constructed with a name, which is the name the extension is registered under, a grouping enum type and a sorting enum type. */ - public class View: BaseView, YapDatabaseViewProducer, YapDatabaseExtensionRegistrar { + open class View: BaseView, YapDatabaseViewProducer, YapDatabaseExtensionRegistrar { /** An enum to make creating YapDatabaseViewGrouping easier. E.g. @@ -135,25 +141,25 @@ extension YapDB { */ public enum Grouping { - case ByKey(YapDatabaseViewGroupingWithKeyBlock) - case ByObject(YapDatabaseViewGroupingWithObjectBlock) - case ByMetadata(YapDatabaseViewGroupingWithMetadataBlock) - case ByRow(YapDatabaseViewGroupingWithRowBlock) + case byKey(YapDatabaseViewGroupingWithKeyBlock) + case byObject(YapDatabaseViewGroupingWithObjectBlock) + case byMetadata(YapDatabaseViewGroupingWithMetadataBlock) + case byRow(YapDatabaseViewGroupingWithRowBlock) - func object(withOptions opts: YapDatabaseBlockInvoke? = .None) -> YapDatabaseViewGrouping { + func object(withOptions opts: YapDatabaseBlockInvoke? = .none) -> YapDatabaseViewGrouping { if let opts = opts { switch self { - case let .ByKey(block): return YapDatabaseViewGrouping.withOptions(opts, keyBlock: block) - case let .ByObject(block): return YapDatabaseViewGrouping.withOptions(opts, objectBlock: block) - case let .ByMetadata(block): return YapDatabaseViewGrouping.withOptions(opts, metadataBlock: block) - case let .ByRow(block): return YapDatabaseViewGrouping.withOptions(opts, rowBlock: block) + case let .byKey(block): return YapDatabaseViewGrouping.withOptions(opts, keyBlock: block) + case let .byObject(block): return YapDatabaseViewGrouping.withOptions(opts, objectBlock: block) + case let .byMetadata(block): return YapDatabaseViewGrouping.withOptions(opts, metadataBlock: block) + case let .byRow(block): return YapDatabaseViewGrouping.withOptions(opts, rowBlock: block) } } else { switch self { - case let .ByKey(block): return YapDatabaseViewGrouping.withKeyBlock(block) - case let .ByObject(block): return YapDatabaseViewGrouping.withObjectBlock(block) - case let .ByMetadata(block): return YapDatabaseViewGrouping.withMetadataBlock(block) - case let .ByRow(block): return YapDatabaseViewGrouping.withRowBlock(block) + case let .byKey(block): return YapDatabaseViewGrouping.withKeyBlock(block) + case let .byObject(block): return YapDatabaseViewGrouping.withObjectBlock(block) + case let .byMetadata(block): return YapDatabaseViewGrouping.withMetadataBlock(block) + case let .byRow(block): return YapDatabaseViewGrouping.withRowBlock(block) } } } @@ -163,25 +169,25 @@ extension YapDB { An enum to make creating YapDatabaseViewSorting easier. */ public enum Sorting { - case ByKey(YapDatabaseViewSortingWithKeyBlock) - case ByObject(YapDatabaseViewSortingWithObjectBlock) - case ByMetadata(YapDatabaseViewSortingWithMetadataBlock) - case ByRow(YapDatabaseViewSortingWithRowBlock) + case byKey(YapDatabaseViewSortingWithKeyBlock) + case byObject(YapDatabaseViewSortingWithObjectBlock) + case byMetadata(YapDatabaseViewSortingWithMetadataBlock) + case byRow(YapDatabaseViewSortingWithRowBlock) - func object(withOptions opts: YapDatabaseBlockInvoke? = .None) -> YapDatabaseViewSorting { + func object(withOptions opts: YapDatabaseBlockInvoke? = .none) -> YapDatabaseViewSorting { if let opts = opts { switch self { - case let .ByKey(block): return YapDatabaseViewSorting.withOptions(opts, keyBlock: block) - case let .ByObject(block): return YapDatabaseViewSorting.withOptions(opts, objectBlock: block) - case let .ByMetadata(block): return YapDatabaseViewSorting.withOptions(opts, metadataBlock: block) - case let .ByRow(block): return YapDatabaseViewSorting.withOptions(opts, rowBlock: block) + case let .byKey(block): return YapDatabaseViewSorting.withOptions(opts, keyBlock: block) + case let .byObject(block): return YapDatabaseViewSorting.withOptions(opts, objectBlock: block) + case let .byMetadata(block): return YapDatabaseViewSorting.withOptions(opts, metadataBlock: block) + case let .byRow(block): return YapDatabaseViewSorting.withOptions(opts, rowBlock: block) } } else { switch self { - case let .ByKey(block): return YapDatabaseViewSorting.withKeyBlock(block) - case let .ByObject(block): return YapDatabaseViewSorting.withObjectBlock(block) - case let .ByMetadata(block): return YapDatabaseViewSorting.withMetadataBlock(block) - case let .ByRow(block): return YapDatabaseViewSorting.withRowBlock(block) + case let .byKey(block): return YapDatabaseViewSorting.withKeyBlock(block) + case let .byObject(block): return YapDatabaseViewSorting.withObjectBlock(block) + case let .byMetadata(block): return YapDatabaseViewSorting.withMetadataBlock(block) + case let .byRow(block): return YapDatabaseViewSorting.withRowBlock(block) } } } @@ -203,9 +209,9 @@ extension YapDB { - parameter collections: an optional array of collections which is used to white list the collections searched when populating the view. */ public init(name: String, - grouping g: Grouping, groupingOptions go: YapDatabaseBlockInvoke? = .None, - sorting s: Sorting, sortingOptions so: YapDatabaseBlockInvoke? = .None, - version: String = "1.0", persistent: Bool = true, collections: [String]? = .None) { + grouping g: Grouping, groupingOptions go: YapDatabaseBlockInvoke? = .none, + sorting s: Sorting, sortingOptions so: YapDatabaseBlockInvoke? = .none, + version: String = "1.0", persistent: Bool = true, collections: [String]? = .none) { grouping = g groupingOptions = go sorting = s @@ -217,13 +223,13 @@ extension YapDB { return YapDatabaseView(grouping: grouping.object(withOptions: groupingOptions), sorting: sorting.object(withOptions: sortingOptions), versionTag: version, options: options) } - func registerInDatabase(database: YapDatabase, withConnection connection: YapDatabaseConnection? = .None) { + func registerInDatabase(_ database: YapDatabase, withConnection connection: YapDatabaseConnection? = .none) { if !isRegisteredInDatabase(database) { if let connection = connection { - database.registerExtension(createDatabaseView(), withName: name, connection: connection) + database.register(createDatabaseView(), withName: name, connection: connection) } else { - database.registerExtension(createDatabaseView(), withName: name) + database.register(createDatabaseView(), withName: name) } } } @@ -240,31 +246,31 @@ extension YapDB { is a YapDB.Fetch type. This allows for filtering of other filters, and even filtering of search results. */ - public class Filter: BaseView, YapDatabaseViewProducer, YapDatabaseExtensionRegistrar { + open class Filter: BaseView, YapDatabaseViewProducer, YapDatabaseExtensionRegistrar { /** An enum to make creating YapDatabaseViewFiltering easier. */ public enum Filtering { - case ByKey(YapDatabaseViewFilteringWithKeyBlock) - case ByObject(YapDatabaseViewFilteringWithObjectBlock) - case ByMetadata(YapDatabaseViewFilteringWithMetadataBlock) - case ByRow(YapDatabaseViewFilteringWithRowBlock) + case byKey(YapDatabaseViewFilteringWithKeyBlock) + case byObject(YapDatabaseViewFilteringWithObjectBlock) + case byMetadata(YapDatabaseViewFilteringWithMetadataBlock) + case byRow(YapDatabaseViewFilteringWithRowBlock) - func object(withOptions ops: YapDatabaseBlockInvoke? = .None) -> YapDatabaseViewFiltering { + func object(withOptions ops: YapDatabaseBlockInvoke? = .none) -> YapDatabaseViewFiltering { if let ops = ops { switch self { - case .ByKey(let block): return YapDatabaseViewFiltering.withOptions(ops, keyBlock: block) - case .ByObject(let block): return YapDatabaseViewFiltering.withOptions(ops, objectBlock: block) - case .ByMetadata(let block): return YapDatabaseViewFiltering.withOptions(ops, metadataBlock: block) - case .ByRow(let block): return YapDatabaseViewFiltering.withOptions(ops, rowBlock: block) + case .byKey(let block): return YapDatabaseViewFiltering.withOptions(ops, keyBlock: block) + case .byObject(let block): return YapDatabaseViewFiltering.withOptions(ops, objectBlock: block) + case .byMetadata(let block): return YapDatabaseViewFiltering.withOptions(ops, metadataBlock: block) + case .byRow(let block): return YapDatabaseViewFiltering.withOptions(ops, rowBlock: block) } } else { switch self { - case .ByKey(let block): return YapDatabaseViewFiltering.withKeyBlock(block) - case .ByObject(let block): return YapDatabaseViewFiltering.withObjectBlock(block) - case .ByMetadata(let block): return YapDatabaseViewFiltering.withMetadataBlock(block) - case .ByRow(let block): return YapDatabaseViewFiltering.withRowBlock(block) + case .byKey(let block): return YapDatabaseViewFiltering.withKeyBlock(block) + case .byObject(let block): return YapDatabaseViewFiltering.withObjectBlock(block) + case .byMetadata(let block): return YapDatabaseViewFiltering.withMetadataBlock(block) + case .byRow(let block): return YapDatabaseViewFiltering.withRowBlock(block) } } } @@ -284,8 +290,8 @@ extension YapDB { - parameter persistent: a Bool, defaults to true - meaning that the contents of the view will be stored in YapDatabase between launches. - parameter collections: an optional array of collections which is used to white list the collections searched when populating the view. */ - public init(name: String, parent p: YapDB.Fetch, filtering f: Filtering, filteringOptions fo: YapDatabaseBlockInvoke? = .None, - version: String = "1.0", persistent: Bool = true, collections: [String]? = .None) { + public init(name: String, parent p: YapDB.Fetch, filtering f: Filtering, filteringOptions fo: YapDatabaseBlockInvoke? = .none, + version: String = "1.0", persistent: Bool = true, collections: [String]? = .none) { parent = p filtering = f filteringOptions = fo @@ -296,14 +302,14 @@ extension YapDB { return YapDatabaseFilteredView(parentViewName: parent.name, filtering: filtering.object(withOptions: filteringOptions), versionTag: version, options: options) } - func registerInDatabase(database: YapDatabase, withConnection connection: YapDatabaseConnection? = .None) { + func registerInDatabase(_ database: YapDatabase, withConnection connection: YapDatabaseConnection? = .none) { if !isRegisteredInDatabase(database) { parent.registerInDatabase(database, withConnection: connection) if let connection = connection { - database.registerExtension(createDatabaseView(), withName: name, connection: connection) + database.register(createDatabaseView(), withName: name, connection: connection) } else { - database.registerExtension(createDatabaseView(), withName: name) + database.register(createDatabaseView(), withName: name) } } } @@ -320,23 +326,23 @@ extension YapDB { In this case, the parent is a YapDB.Fetch type. This allows for searching of other filters, and even searching inside search results. */ - public class SearchResults: BaseView, YapDatabaseViewProducer, YapDatabaseExtensionRegistrar { + open class SearchResults: BaseView, YapDatabaseViewProducer, YapDatabaseExtensionRegistrar { /** An enum to make creating YapDatabaseFullTextSearchHandler easier. */ public enum Handler { - case ByKey(YapDatabaseFullTextSearchWithKeyBlock) - case ByObject(YapDatabaseFullTextSearchWithObjectBlock) - case ByMetadata(YapDatabaseFullTextSearchWithMetadataBlock) - case ByRow(YapDatabaseFullTextSearchWithRowBlock) + case byKey(YapDatabaseFullTextSearchWithKeyBlock) + case byObject(YapDatabaseFullTextSearchWithObjectBlock) + case byMetadata(YapDatabaseFullTextSearchWithMetadataBlock) + case byRow(YapDatabaseFullTextSearchWithRowBlock) public var object: YapDatabaseFullTextSearchHandler { switch self { - case let .ByKey(block): return YapDatabaseFullTextSearchHandler.withKeyBlock(block) - case let .ByObject(block): return YapDatabaseFullTextSearchHandler.withObjectBlock(block) - case let .ByMetadata(block): return YapDatabaseFullTextSearchHandler.withMetadataBlock(block) - case let .ByRow(block): return YapDatabaseFullTextSearchHandler.withRowBlock(block) + case let .byKey(block): return YapDatabaseFullTextSearchHandler.withKeyBlock(block) + case let .byObject(block): return YapDatabaseFullTextSearchHandler.withObjectBlock(block) + case let .byMetadata(block): return YapDatabaseFullTextSearchHandler.withMetadataBlock(block) + case let .byRow(block): return YapDatabaseFullTextSearchHandler.withRowBlock(block) } } } @@ -358,7 +364,7 @@ extension YapDB { - parameter persistent: a Bool, defaults to true - meaning that the contents of the view will be stored in YapDatabase between launches. - parameter collections: an optional array of collections which is used to white list the collections searched when populating the view. */ - public init(name: String, parent p: YapDB.Fetch, search: String, columnNames cn: [String], handler h: Handler, version: String = "1.0", persistent: Bool = true, collections: [String]? = .None) { + public init(name: String, parent p: YapDB.Fetch, search: String, columnNames cn: [String], handler h: Handler, version: String = "1.0", persistent: Bool = true, collections: [String]? = .none) { parent = p searchName = search columnNames = cn @@ -367,28 +373,28 @@ extension YapDB { } func createDatabaseView() -> YapDatabaseView { - return YapDatabaseSearchResultsView(fullTextSearchName: searchName, parentViewName: parent.name, versionTag: version, options: .None) + return YapDatabaseSearchResultsView(fullTextSearchName: searchName, parentViewName: parent.name, versionTag: version, options: .none) } - func registerInDatabase(database: YapDatabase, withConnection connection: YapDatabaseConnection? = .None) { + func registerInDatabase(_ database: YapDatabase, withConnection connection: YapDatabaseConnection? = .none) { - if (database.registeredExtension(searchName) as? YapDatabaseFullTextSearch) == .None { - let fullTextSearch = YapDatabaseFullTextSearch(columnNames: columnNames, handler: handler.object, versionTag: version) + if (database.registeredExtension(searchName) as? YapDatabaseFullTextSearch) == .none { + let fullTextSearch = YapDatabaseFullTextSearch(columnNames: columnNames, options: nil, handler: handler.object, versionTag: version) if let connection = connection { - database.registerExtension(fullTextSearch, withName: searchName, connection: connection) + database.register(fullTextSearch, withName: searchName, connection: connection) } else { - database.registerExtension(fullTextSearch, withName: searchName) + database.register(fullTextSearch, withName: searchName) } } if !isRegisteredInDatabase(database) { parent.registerInDatabase(database, withConnection: connection) if let connection = connection { - database.registerExtension(createDatabaseView(), withName: name, connection: connection) + database.register(createDatabaseView(), withName: name, connection: connection) } else { - database.registerExtension(createDatabaseView(), withName: name) + database.register(createDatabaseView(), withName: name) } } } @@ -405,20 +411,20 @@ extension YapDB { is similar to a full text search extension. It features a handler, which must be provided to update a dictionary used to index records. */ - public class SecondaryIndex: BaseExtension, YapDatabaseExtensionRegistrar { + open class SecondaryIndex: BaseExtension, YapDatabaseExtensionRegistrar { public enum Handler { - case ByKey(YapDatabaseSecondaryIndexWithKeyBlock) - case ByObject(YapDatabaseSecondaryIndexWithObjectBlock) - case ByMetadata(YapDatabaseSecondaryIndexWithMetadataBlock) - case ByRow(YapDatabaseSecondaryIndexWithRowBlock) + case byKey(YapDatabaseSecondaryIndexWithKeyBlock) + case byObject(YapDatabaseSecondaryIndexWithObjectBlock) + case byMetadata(YapDatabaseSecondaryIndexWithMetadataBlock) + case byRow(YapDatabaseSecondaryIndexWithRowBlock) public var object: YapDatabaseSecondaryIndexHandler { switch self { - case let .ByKey(block): return YapDatabaseSecondaryIndexHandler.withKeyBlock(block) - case let .ByObject(block): return YapDatabaseSecondaryIndexHandler.withObjectBlock(block) - case let .ByMetadata(block): return YapDatabaseSecondaryIndexHandler.withMetadataBlock(block) - case let .ByRow(block): return YapDatabaseSecondaryIndexHandler.withRowBlock(block) + case let .byKey(block): return YapDatabaseSecondaryIndexHandler.withKeyBlock(block) + case let .byObject(block): return YapDatabaseSecondaryIndexHandler.withObjectBlock(block) + case let .byMetadata(block): return YapDatabaseSecondaryIndexHandler.withMetadataBlock(block) + case let .byRow(block): return YapDatabaseSecondaryIndexHandler.withRowBlock(block) } } } @@ -443,19 +449,19 @@ extension YapDB { func setup() -> YapDatabaseSecondaryIndexSetup { let setup = YapDatabaseSecondaryIndexSetup() for (column, indexType) in columnTypes { - setup.addColumn(column, withType: indexType) + setup.addColumn(column, with: indexType) } return setup } - public func registerInDatabase(database: YapDatabase, withConnection connection: YapDatabaseConnection?) { + open func registerInDatabase(_ database: YapDatabase, withConnection connection: YapDatabaseConnection?) { if !isRegisteredInDatabase(database) { let secondaryIndex = YapDatabaseSecondaryIndex(setup: setup(), handler: handler.object, versionTag: version, options: options) if let connection = connection { - database.registerExtension(secondaryIndex, withName: name, connection: connection) + database.register(secondaryIndex, withName: name, connection: connection) } else { - database.registerExtension(secondaryIndex, withName: name) + database.register(secondaryIndex, withName: name) } } } @@ -467,9 +473,9 @@ extension YapDB { public struct Mappings { public enum Kind { - case Composed(YapDatabaseViewMappings) - case Groups([String]) - case Dynamic((filter: YapDatabaseViewMappingGroupFilter, sorter: YapDatabaseViewMappingGroupSort)) + case composed(YapDatabaseViewMappings) + case groups([String]) + case dynamic((filter: YapDatabaseViewMappingGroupFilter, sorter: YapDatabaseViewMappingGroupSort)) } public static var passThroughFilter: YapDatabaseViewMappingGroupFilter { @@ -482,25 +488,25 @@ extension YapDB { let kind: Kind - public init(filter f: YapDatabaseViewMappingGroupFilter = Mappings.passThroughFilter, sort s: YapDatabaseViewMappingGroupSort = Mappings.caseInsensitiveGroupSort) { - kind = .Dynamic((f, s)) + public init(filter f: @escaping YapDatabaseViewMappingGroupFilter = Mappings.passThroughFilter, sort s: @escaping YapDatabaseViewMappingGroupSort = Mappings.caseInsensitiveGroupSort) { + kind = .dynamic((f, s)) } public init(groups: [String]) { - kind = .Groups(groups) + kind = .groups(groups) } public init(composed: YapDatabaseViewMappings) { - kind = .Composed(composed) + kind = .composed(composed) } - func createMappingsWithViewName(viewName: String) -> YapDatabaseViewMappings { + func createMappingsWithViewName(_ viewName: String) -> YapDatabaseViewMappings { switch kind { - case .Composed(let mappings): + case .composed(let mappings): return mappings - case .Groups(let groups): + case .groups(let groups): return YapDatabaseViewMappings(groups: groups, view: viewName) - case .Dynamic(let (filter: filter, sorter: sorter)): + case .dynamic(let (filter: filter, sorter: sorter)): return YapDatabaseViewMappings(groupFilterBlock: filter, sortBlock: sorter, view: viewName) } } @@ -521,25 +527,25 @@ extension YapDB { return fetch.name } - public init(fetch f: Fetch, mappings m: Mappings = Mappings(), block b: MappingsConfigurationBlock? = .None) { + public init(fetch f: Fetch, mappings m: Mappings = Mappings(), block b: MappingsConfigurationBlock? = .none) { fetch = f mappings = m block = b } public init(view: YapDB.View) { - self.init(fetch: .View(view)) + self.init(fetch: .view(view)) } public init(filter: YapDB.Filter) { - self.init(fetch: .Filter(filter)) + self.init(fetch: .filter(filter)) } public init(search: YapDB.SearchResults) { - self.init(fetch: .Search(search)) + self.init(fetch: .search(search)) } - public func createMappingsRegisteredInDatabase(database: YapDatabase, withConnection connection: YapDatabaseConnection? = .None) -> YapDatabaseViewMappings { + public func createMappingsRegisteredInDatabase(_ database: YapDatabase, withConnection connection: YapDatabaseConnection? = .none) -> YapDatabaseViewMappings { let databaseViewMappings = fetch.createViewMappings(mappings, inDatabase: database, withConnection: connection) block?(databaseViewMappings) return databaseViewMappings @@ -550,20 +556,20 @@ extension YapDB { extension YapDB { - public class Search { - public typealias Query = (searchTerm: String) -> String + open class Search { + public typealias Query = (_ searchTerm: String) -> String let database: YapDatabase let connection: YapDatabaseConnection let queues: [(String, YapDatabaseSearchQueue)] let query: Query - public init(db: YapDatabase, views: [YapDB.Fetch], query q: Query) { + public init(db: YapDatabase, views: [YapDB.Fetch], query q: @escaping Query) { database = db connection = db.newConnection() let _views = views.filter { fetch in switch fetch { - case .Index(_): return false + case .index(_): return false default: return true } } @@ -574,18 +580,18 @@ extension YapDB { query = q } - public convenience init(db: YapDatabase, view: YapDB.Fetch, query: Query) { + public convenience init(db: YapDatabase, view: YapDB.Fetch, query: @escaping Query) { self.init(db: db, views: [view], query: query) } - public func usingTerm(term: String) { + open func usingTerm(_ term: String) { for (_, queue) in queues { - queue.enqueueQuery(query(searchTerm: term)) + queue.enqueueQuery(query(term)) } - connection.asyncReadWriteWithBlock { [queues = self.queues] transaction in + connection.asyncReadWrite { [queues = self.queues] transaction in for (name, queue) in queues { if let searchResultsViewTransaction = transaction.ext(name) as? YapDatabaseSearchResultsViewTransaction { - searchResultsViewTransaction.performSearchWithQueue(queue) + searchResultsViewTransaction.performSearch(with: queue) } else { assertionFailure("Error: Attempting search using results view with name: \(name) which isn't a registered database extension.") diff --git a/Sources/YapDatabaseExtensions.swift b/Sources/YapDatabaseExtensions.swift index b6cf121..77e5282 100644 --- a/Sources/YapDatabaseExtensions.swift +++ b/Sources/YapDatabaseExtensions.swift @@ -22,8 +22,8 @@ public struct YapDB { - returns: a String representing the path to a database in the given search directory, with the given name/suffix. */ - public static func pathToDatabase(directory: NSSearchPathDirectory, name: String, suffix: String? = .None) -> String { - let paths = NSSearchPathForDirectoriesInDomains(directory, .UserDomainMask, true) + public static func pathToDatabase(_ directory: FileManager.SearchPathDirectory, name: String, suffix: String? = .none) -> String { + let paths = NSSearchPathForDirectoriesInDomains(directory, .userDomainMask, true) let directory: String = paths.first ?? NSTemporaryDirectory() let filename: String = { if let suffix = suffix { @@ -32,7 +32,7 @@ public struct YapDB { return "\(name).sqlite" }() - return (directory as NSString).stringByAppendingPathComponent(filename) + return (directory as NSString).appendingPathComponent(filename) } /// Type of closure which can perform operations on newly created/opened database instances. @@ -73,8 +73,8 @@ public struct YapDB { - returns: the YapDatabase instance. */ - public static func databaseNamed(name: String, operations: DatabaseOperationsBlock? = .None) -> YapDatabase { - let db = YapDatabase(path: pathToDatabase(.DocumentDirectory, name: name, suffix: .None)) + public static func databaseNamed(_ name: String, operations: DatabaseOperationsBlock? = .none) -> YapDatabase { + let db = YapDatabase(path: pathToDatabase(.documentDirectory, name: name, suffix: .none)) operations?(db) return db } @@ -105,11 +105,11 @@ public struct YapDB { - returns: the YapDatabase instance. */ - public static func testDatabase(file: String = #file, test: String = #function, operations: DatabaseOperationsBlock? = .None) -> YapDatabase { - let path = pathToDatabase(.CachesDirectory, name: (file as NSString).lastPathComponent, suffix: test.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "()"))) + public static func testDatabase(_ file: String = #file, test: String = #function, operations: DatabaseOperationsBlock? = .none) -> YapDatabase { + let path = pathToDatabase(.cachesDirectory, name: (file as NSString).lastPathComponent, suffix: test.trimmingCharacters(in: CharacterSet(charactersIn: "()"))) assert(!path.isEmpty, "Path should not be empty.") do { - try NSFileManager.defaultManager().removeItemAtPath(path) + try FileManager.default.removeItem(atPath: path) } catch { } @@ -193,10 +193,6 @@ Identifier for your String identifiers. */ public typealias Identifier = String -extension Identifier: CustomStringConvertible { - public var description: String { return self } -} - // MARK: - Persistable /** @@ -219,7 +215,7 @@ extension Persistable { - parameter key: a `String` - returns: a `YapDB.Index` value. */ - public static func indexWithKey(key: String) -> YapDB.Index { + public static func indexWithKey(_ key: String) -> YapDB.Index { return YapDB.Index(collection: collection, key: key) } @@ -234,9 +230,9 @@ extension Persistable { - returns: an array of `YapDB.Index` values. */ public static func indexesWithKeys< - Keys where - Keys: SequenceType, - Keys.Generator.Element == String>(keys: Keys) -> [YapDB.Index] { + Keys>(_ keys: Keys) -> [YapDB.Index] where + Keys: Sequence, + Keys.Iterator.Element == String { return Set(keys).map { YapDB.Index(collection: collection, key: $0) } } @@ -256,17 +252,17 @@ extension Persistable { - returns: a `YapDB.Index`. */ public var index: YapDB.Index { - return self.dynamicType.indexWithKey(key) + return type(of: self).indexWithKey(key) } } // MARK: Functions -public func keyForPersistable(persistable: P) -> String { +public func keyForPersistable(_ persistable: P) -> String { return persistable.key } -public func indexForPersistable(persistable: P) -> YapDB.Index { +public func indexForPersistable(_ persistable: P) -> YapDB.Index { return persistable.index } @@ -281,7 +277,7 @@ public protocol ReadTransactionType { - parameter collection: a String. Not optional. - returns: an array of String values. */ - func keysInCollection(collection: String) -> [String] + func keysInCollection(_ collection: String) -> [String] /** Read the object at the index. @@ -289,7 +285,7 @@ public protocol ReadTransactionType { - parameter index: a YapDB.Index. - returns: an `AnyObject` if an item existing in the database for this index. */ - func readAtIndex(index: YapDB.Index) -> AnyObject? + func readAtIndex(_ index: YapDB.Index) -> AnyObject? /** Read the metadata at the index. @@ -297,7 +293,7 @@ public protocol ReadTransactionType { - parameter index: a YapDB.Index. - returns: an `AnyObject` if a metadata item existing in the database for this index. */ - func readMetadataAtIndex(index: YapDB.Index) -> AnyObject? + func readMetadataAtIndex(_ index: YapDB.Index) -> AnyObject? } /// A facade interface for a write transaction. @@ -310,7 +306,7 @@ public protocol WriteTransactionType: ReadTransactionType { - parameter object: the `AnyObject` which will be written. - parameter metadata: an optional `AnyObject` which will be written as metadata. */ - func writeAtIndex(index: YapDB.Index, object: AnyObject, metadata: AnyObject?) + func writeAtIndex(_ index: YapDB.Index, object: AnyObject, metadata: AnyObject?) /** Remove the sequence object from the database at the indexes (if it exists), including metadata @@ -318,9 +314,9 @@ public protocol WriteTransactionType: ReadTransactionType { - parameter indexes: the `[YapDB.Index]` to remove. */ func removeAtIndexes< - Indexes where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) + Indexes>(_ indexes: Indexes) where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index } /// A facade interface for a database connection. @@ -339,7 +335,7 @@ public protocol ConnectionType { - parameter block: a closure which receives YapDatabaseReadTransaction and returns T - returns: An instance of T */ - func read(block: ReadTransaction -> T) -> T + func read(_ block: @escaping (ReadTransaction) -> T) -> T /** Synchronously writes to the database on the connection. The closure receives @@ -352,7 +348,7 @@ public protocol ConnectionType { - parameter block: a closure which receives YapDatabaseReadWriteTransaction and returns T - returns: An instance of T */ - func write(block: WriteTransaction -> T) -> T + func write(_ block: @escaping (WriteTransaction) -> T) -> T /** Asynchronously reads from the database on the connection. The closure receives @@ -368,7 +364,7 @@ public protocol ConnectionType { - parameter queue: a dispatch_queue_t, defaults to main queue, can be ommitted in most cases. - parameter completion: a closure which receives T and returns Void. */ - func asyncRead(block: ReadTransaction -> T, queue: dispatch_queue_t, completion: (T) -> Void) + func asyncRead(_ block: @escaping (ReadTransaction) -> T, queue: DispatchQueue, completion: @escaping (T) -> Void) /** Asynchronously writes to the database on the connection. The closure receives @@ -384,7 +380,7 @@ public protocol ConnectionType { - parameter queue: a dispatch_queue_t, defaults to main queue, can be ommitted in most cases. - parameter completion: a closure which receives T and returns Void. */ - func asyncWrite(block: WriteTransaction -> T, queue: dispatch_queue_t, completion: (T -> Void)?) + func asyncWrite(_ block: @escaping (WriteTransaction) -> T, queue: DispatchQueue, completion: ((T) -> Void)?) /** Execute a read/write block inside a `NSOperation`. The block argument receives a @@ -400,7 +396,7 @@ public protocol ConnectionType { - parameter block: a closure of type (YapDatabaseReadWriteTransaction) -> Void - returns: an `NSOperation`. */ - func writeBlockOperation(block: WriteTransaction -> Void) -> NSOperation + func writeBlockOperation(_ block: @escaping (WriteTransaction) -> Void) -> Operation } /// A facade interface for a database. @@ -410,9 +406,9 @@ public protocol DatabaseType { } internal enum Handle { - case Transaction(D.Connection.ReadTransaction) - case Connection(D.Connection) - case Database(D) + case transaction(D.Connection.ReadTransaction) + case connection(D.Connection) + case database(D) } // MARK: - YapDatabaseReadTransaction @@ -425,8 +421,8 @@ extension YapDatabaseReadTransaction: ReadTransactionType { - parameter collection: a String. - returns: an array of String values. */ - public func keysInCollection(collection: String) -> [String] { - return allKeysInCollection(collection) + public func keysInCollection(_ collection: String) -> [String] { + return allKeys(inCollection: collection) } /** @@ -435,8 +431,8 @@ extension YapDatabaseReadTransaction: ReadTransactionType { - parameter index: The YapDB.Index value. - returns: An optional AnyObject. */ - public func readAtIndex(index: YapDB.Index) -> AnyObject? { - return objectForKey(index.key, inCollection: index.collection) + public func readAtIndex(_ index: YapDB.Index) -> AnyObject? { + return object(forKey: index.key, inCollection: index.collection) as AnyObject? } /** @@ -445,8 +441,8 @@ extension YapDatabaseReadTransaction: ReadTransactionType { - parameter index: The YapDB.Index value. - returns: An optional AnyObject. */ - public func readMetadataAtIndex(index: YapDB.Index) -> AnyObject? { - return metadataForKey(index.key, inCollection: index.collection) + public func readMetadataAtIndex(_ index: YapDB.Index) -> AnyObject? { + return metadata(forKey: index.key, inCollection: index.collection) as AnyObject? } } @@ -454,7 +450,7 @@ extension YapDatabaseReadTransaction: ReadTransactionType { extension YapDatabaseReadWriteTransaction: WriteTransactionType { - public func writeAtIndex(index: YapDB.Index, object: AnyObject, metadata: AnyObject? = .None) { + public func writeAtIndex(_ index: YapDB.Index, object: AnyObject, metadata: AnyObject? = .none) { if let metadata: AnyObject = metadata { setObject(object, forKey: index.key, inCollection: index.collection, withMetadata: metadata) } @@ -463,14 +459,14 @@ extension YapDatabaseReadWriteTransaction: WriteTransactionType { } } - func removeAtIndex(index: YapDB.Index) { - removeObjectForKey(index.key, inCollection: index.collection) + func removeAtIndex(_ index: YapDB.Index) { + removeObject(forKey: index.key, inCollection: index.collection) } public func removeAtIndexes< - Indexes where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) { + Indexes>(_ indexes: Indexes) where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index { indexes.forEach(removeAtIndex) } } @@ -488,9 +484,9 @@ extension YapDatabaseConnection: ConnectionType { - parameter block: a closure which receives YapDatabaseReadTransaction and returns T - returns: An instance of T */ - public func read(block: YapDatabaseReadTransaction -> T) -> T { - var result: T! = .None - readWithBlock { result = block($0) } + public func read(_ block: @escaping (YapDatabaseReadTransaction) -> T) -> T { + var result: T! = .none + self.read { result = block($0) } return result } @@ -505,9 +501,9 @@ extension YapDatabaseConnection: ConnectionType { - parameter block: a closure which receives YapDatabaseReadWriteTransaction and returns T - returns: An instance of T */ - public func write(block: YapDatabaseReadWriteTransaction -> T) -> T { - var result: T! = .None - readWriteWithBlock { result = block($0) } + public func write(_ block: @escaping (YapDatabaseReadWriteTransaction) -> T) -> T { + var result: T! = .none + readWrite { result = block($0) } return result } @@ -525,9 +521,9 @@ extension YapDatabaseConnection: ConnectionType { - parameter queue: a dispatch_queue_t, defaults to main queue, can be ommitted in most cases. - parameter completion: a closure which receives T and returns Void. */ - public func asyncRead(block: YapDatabaseReadTransaction -> T, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (T) -> Void) { - var result: T! = .None - asyncReadWithBlock({ result = block($0) }, completionQueue: queue) { completion(result) } + public func asyncRead(_ block: @escaping (YapDatabaseReadTransaction) -> T, queue: DispatchQueue = DispatchQueue.main, completion: @escaping (T) -> Void) { + var result: T! = .none + self.asyncRead({ result = block($0) }, completionQueue: queue) { completion(result) } } /** @@ -544,9 +540,9 @@ extension YapDatabaseConnection: ConnectionType { - parameter queue: a dispatch_queue_t, defaults to main queue, can be ommitted in most cases. - parameter completion: a closure which receives T and returns Void. */ - public func asyncWrite(block: YapDatabaseReadWriteTransaction -> T, queue: dispatch_queue_t = dispatch_get_main_queue(), completion: (T -> Void)?) { - var result: T! = .None - asyncReadWriteWithBlock({ result = block($0) }, completionQueue: queue) { completion?(result) } + public func asyncWrite(_ block: @escaping (YapDatabaseReadWriteTransaction) -> T, queue: DispatchQueue = DispatchQueue.main, completion: ((T) -> Void)?) { + var result: T! = .none + asyncReadWrite({ result = block($0) }, completionQueue: queue) { completion?(result) } } /** @@ -563,8 +559,8 @@ extension YapDatabaseConnection: ConnectionType { - parameter block: a closure of type (YapDatabaseReadWriteTransaction) -> Void - returns: an `NSOperation`. */ - public func writeBlockOperation(block: (YapDatabaseReadWriteTransaction) -> Void) -> NSOperation { - return NSBlockOperation { self.readWriteWithBlock(block) } + public func writeBlockOperation(_ block: @escaping (YapDatabaseReadWriteTransaction) -> Void) -> Operation { + return BlockOperation { self.readWrite(block) } } } @@ -607,7 +603,7 @@ extension YapDB.Index: ValueCoding { // MARK: Coders -public final class YapDBIndexCoder: NSObject, NSCoding, CodingType { +public final class YapDBIndexCoder: NSObject, NSCoding, CodingProtocol { public let value: YapDB.Index public init(_ v: YapDB.Index) { @@ -615,33 +611,33 @@ public final class YapDBIndexCoder: NSObject, NSCoding, CodingType { } public required init(coder aDecoder: NSCoder) { - let collection = aDecoder.decodeObjectForKey("collection") as! String - let key = aDecoder.decodeObjectForKey("key") as! String + let collection = aDecoder.decodeObject(forKey: "collection") as! String + let key = aDecoder.decodeObject(forKey: "key") as! String value = YapDB.Index(collection: collection, key: key) } - public func encodeWithCoder(aCoder: NSCoder) { - aCoder.encodeObject(value.collection, forKey: "collection") - aCoder.encodeObject(value.key, forKey: "key") + public func encode(with aCoder: NSCoder) { + aCoder.encode(value.collection, forKey: "collection") + aCoder.encode(value.key, forKey: "key") } } // MARK: - Deprecations -@available(*, unavailable, renamed="Persistable") +@available(*, unavailable, renamed: "Persistable") public typealias MetadataPersistable = Persistable -@available(*, unavailable, renamed="Persistable") +@available(*, unavailable, renamed: "Persistable") public typealias ObjectMetadataPersistable = Persistable -@available(*, unavailable, renamed="Persistable") +@available(*, unavailable, renamed: "Persistable") public typealias ValueMetadataPersistable = Persistable -@available(*, unavailable, renamed="ValueCoding") +@available(*, unavailable, renamed: "ValueCoding") public typealias Saveable = ValueCoding -@available(*, unavailable, renamed="CodingType") -public typealias Archiver = CodingType - - +@available(*, unavailable, renamed: "CodingProtocol") +public typealias CodingType = CodingProtocol +@available(*, unavailable, renamed: "CodingProtocol") +public typealias Archiver = CodingProtocol diff --git a/Supporting Files/Version.xcconfig b/Supporting Files/Version.xcconfig index cd3083e..bf8fe70 100644 --- a/Supporting Files/Version.xcconfig +++ b/Supporting Files/Version.xcconfig @@ -1 +1 @@ -YAP_DATABASE_EXTENSIONS_VERSION = 2.6.0 +YAP_DATABASE_EXTENSIONS_VERSION = 3.0.0 diff --git a/Supporting Files/YapDatabaseExtensions.xcconfig b/Supporting Files/YapDatabaseExtensions.xcconfig index bcbbfc1..3c1ebd9 100644 --- a/Supporting Files/YapDatabaseExtensions.xcconfig +++ b/Supporting Files/YapDatabaseExtensions.xcconfig @@ -11,8 +11,8 @@ INFOPLIST_FILE_framework = $(SRCROOT)/Supporting Files/Info.plist INFOPLIST_FILE_xctest = $(SRCROOT)/Tests/Info.plist INFOPLIST_FILE = $(INFOPLIST_FILE_$(WRAPPER_EXTENSION)) -PRODUCT_BUNDLE_IDENTIFIER_framework = me.danthorpe.YapDatabaseExtensions -PRODUCT_BUNDLE_IDENTIFIER_xctest = me.danthorpe.YapDatabaseExtensionsTests +PRODUCT_BUNDLE_IDENTIFIER_framework = com.roepcke.YapDatabaseExtensions +PRODUCT_BUNDLE_IDENTIFIER_xctest = com.roepcke.YapDatabaseExtensionsTests PRODUCT_BUNDLE_IDENTIFIER = $(PRODUCT_BUNDLE_IDENTIFIER_$(WRAPPER_EXTENSION)) PRODUCT_NAME_framework = YapDatabaseExtensions @@ -23,7 +23,7 @@ APPLICATION_EXTENSION_API_ONLY_framework = YES APPLICATION_EXTENSION_API_ONLY_xctest = NO APPLICATION_EXTENSION_API_ONLY = $(APPLICATION_EXTENSION_API_ONLY_$(WRAPPER_EXTENSION)) -SWIFT_VERSION = 2.3 +SWIFT_VERSION = 3.0 // Build Settings SUPPORTED_PLATFORMS = macosx iphoneos appletvos watchos appletvsimulator iphonesimulator watchsimulator diff --git a/Tests/Models.swift b/Tests/Models.swift index 22a2efa..f447a0a 100644 --- a/Tests/Models.swift +++ b/Tests/Models.swift @@ -12,8 +12,8 @@ import YapDatabase import YapDatabaseExtensions public enum Barcode: Equatable { - case UPCA(Int, Int, Int, Int) - case QRCode(String) + case upca(Int, Int, Int, Int) + case qrCode(String) } public struct Product: Identifiable, Equatable { @@ -61,13 +61,13 @@ public class NamedEntity: NSObject, NSCoding { } public required init?(coder aDecoder: NSCoder) { - identifier = aDecoder.decodeObjectForKey("identifier") as! Identifier - name = aDecoder.decodeObjectForKey("name") as! String + identifier = aDecoder.decodeObject(forKey: "identifier") as! Identifier + name = aDecoder.decodeObject(forKey: "name") as! String } - public func encodeWithCoder(aCoder: NSCoder) { - aCoder.encodeObject(identifier, forKey: "identifier") - aCoder.encodeObject(name, forKey: "name") + public func encode(with aCoder: NSCoder) { + aCoder.encode(identifier, forKey: "identifier") + aCoder.encode(name, forKey: "name") } } @@ -86,9 +86,9 @@ public class Manager: NamedEntity { public func == (a: Barcode, b: Barcode) -> Bool { switch (a, b) { - case let (.UPCA(aNS, aM, aP, aC), .UPCA(bNS, bM, bP, bC)): + case let (.upca(aNS, aM, aP, aC), .upca(bNS, bM, bP, bC)): return (aNS == bNS) && (aM == bM) && (aP == bP) && (aC == bC) - case let (.QRCode(aCode), .QRCode(bCode)): + case let (.qrCode(aCode), .qrCode(bCode)): return aCode == bCode default: return false @@ -164,9 +164,9 @@ extension Barcode: Persistable { public var identifier: Int { switch self { - case let .UPCA(numberSystem, manufacturer, product, check): + case let .upca(numberSystem, manufacturer, product, check): return "\(numberSystem).\(manufacturer).\(product).\(check)".hashValue - case let .QRCode(code): + case let .qrCode(code): return code.hashValue } } @@ -220,12 +220,12 @@ extension Manager: Persistable { extension Barcode: ValueCoding { public typealias Coder = BarcodeCoder - enum Kind: Int { case UPCA = 1, QRCode } + enum Kind: Int { case upca = 1, qrCode } var kind: Kind { switch self { - case UPCA(_): return Kind.UPCA - case QRCode(_): return Kind.QRCode + case .upca(_): return Kind.upca + case .qrCode(_): return Kind.qrCode } } } @@ -253,7 +253,7 @@ extension Manager.Metadata: ValueCoding { // MARK: - Coders -public class BarcodeCoder: NSObject, NSCoding, CodingType { +public class BarcodeCoder: NSObject, NSCoding, CodingProtocol { public let value: Barcode public required init(_ v: Barcode) { @@ -261,17 +261,17 @@ public class BarcodeCoder: NSObject, NSCoding, CodingType { } public required init?(coder aDecoder: NSCoder) { - if let kind = Barcode.Kind(rawValue: aDecoder.decodeIntegerForKey("kind")) { + if let kind = Barcode.Kind(rawValue: aDecoder.decodeInteger(forKey: "kind")) { switch kind { - case .UPCA: - let numberSystem = aDecoder.decodeIntegerForKey("numberSystem") - let manufacturer = aDecoder.decodeIntegerForKey("manufacturer") - let product = aDecoder.decodeIntegerForKey("product") - let check = aDecoder.decodeIntegerForKey("check") - value = .UPCA(numberSystem, manufacturer, product, check) - case .QRCode: - let code = aDecoder.decodeObjectForKey("code") as! String - value = .QRCode(code) + case .upca: + let numberSystem = aDecoder.decodeInteger(forKey: "numberSystem") + let manufacturer = aDecoder.decodeInteger(forKey: "manufacturer") + let product = aDecoder.decodeInteger(forKey: "product") + let check = aDecoder.decodeInteger(forKey: "check") + value = .upca(numberSystem, manufacturer, product, check) + case .qrCode: + let code = aDecoder.decodeObject(forKey: "code") as! String + value = .qrCode(code) } } else { @@ -279,21 +279,21 @@ public class BarcodeCoder: NSObject, NSCoding, CodingType { } } - public func encodeWithCoder(aCoder: NSCoder) { - aCoder.encodeInteger(value.kind.rawValue, forKey: "kind") + public func encode(with aCoder: NSCoder) { + aCoder.encode(value.kind.rawValue, forKey: "kind") switch value { - case let .UPCA(numberSystem, manufacturer, product, check): - aCoder.encodeInteger(numberSystem, forKey: "numberSystem") - aCoder.encodeInteger(manufacturer, forKey: "manufacturer") - aCoder.encodeInteger(product, forKey: "product") - aCoder.encodeInteger(check, forKey: "check") - case let .QRCode(code): - aCoder.encodeObject(code, forKey: "code") + case let .upca(numberSystem, manufacturer, product, check): + aCoder.encode(numberSystem, forKey: "numberSystem") + aCoder.encode(manufacturer, forKey: "manufacturer") + aCoder.encode(product, forKey: "product") + aCoder.encode(check, forKey: "check") + case let .qrCode(code): + aCoder.encode(code, forKey: "code") } } } -public class ProductCategoryCoder: NSObject, NSCoding, CodingType { +public class ProductCategoryCoder: NSObject, NSCoding, CodingProtocol { public let value: Product.Category public required init(_ v: Product.Category) { @@ -301,18 +301,18 @@ public class ProductCategoryCoder: NSObject, NSCoding, CodingType { } public required init?(coder aDecoder: NSCoder) { - let identifier = aDecoder.decodeIntegerForKey("identifier") - let name = aDecoder.decodeObjectForKey("name") as? String + let identifier = aDecoder.decodeInteger(forKey: "identifier") + let name = aDecoder.decodeObject(forKey: "name") as? String value = Product.Category(identifier: identifier, name: name!) } - public func encodeWithCoder(aCoder: NSCoder) { - aCoder.encodeInteger(value.identifier, forKey: "identifier") - aCoder.encodeObject(value.name, forKey: "name") + public func encode(with aCoder: NSCoder) { + aCoder.encode(value.identifier, forKey: "identifier") + aCoder.encode(value.name, forKey: "name") } } -public class ProductMetadataCoder: NSObject, NSCoding, CodingType { +public class ProductMetadataCoder: NSObject, NSCoding, CodingProtocol { public let value: Product.Metadata public required init(_ v: Product.Metadata) { @@ -320,16 +320,16 @@ public class ProductMetadataCoder: NSObject, NSCoding, CodingType { } public required init?(coder aDecoder: NSCoder) { - let categoryIdentifier = aDecoder.decodeIntegerForKey("categoryIdentifier") + let categoryIdentifier = aDecoder.decodeInteger(forKey: "categoryIdentifier") value = Product.Metadata(categoryIdentifier: categoryIdentifier) } - public func encodeWithCoder(aCoder: NSCoder) { - aCoder.encodeInteger(value.categoryIdentifier, forKey: "categoryIdentifier") + public func encode(with aCoder: NSCoder) { + aCoder.encode(value.categoryIdentifier, forKey: "categoryIdentifier") } } -public class ProductCoder: NSObject, NSCoding, CodingType { +public class ProductCoder: NSObject, NSCoding, CodingProtocol { public let value: Product public required init(_ v: Product) { @@ -337,20 +337,20 @@ public class ProductCoder: NSObject, NSCoding, CodingType { } public required init?(coder aDecoder: NSCoder) { - let identifier = aDecoder.decodeObjectForKey("identifier") as! String - let name = aDecoder.decodeObjectForKey("name") as! String - let barcode = Barcode.decode(aDecoder.decodeObjectForKey("barcode")) + let identifier = aDecoder.decodeObject(forKey: "identifier") as! String + let name = aDecoder.decodeObject(forKey: "name") as! String + let barcode = Barcode.decode(aDecoder.decodeObject(forKey: "barcode")) value = Product(identifier: identifier, name: name, barcode: barcode!) } - public func encodeWithCoder(aCoder: NSCoder) { - aCoder.encodeObject(value.identifier, forKey: "identifier") - aCoder.encodeObject(value.name, forKey: "name") - aCoder.encodeObject(value.barcode.encoded, forKey: "barcode") + public func encode(with aCoder: NSCoder) { + aCoder.encode(value.identifier, forKey: "identifier") + aCoder.encode(value.name, forKey: "name") + aCoder.encode(value.barcode.encoded, forKey: "barcode") } } -public class InventoryCoder: NSObject, NSCoding, CodingType { +public class InventoryCoder: NSObject, NSCoding, CodingProtocol { public let value: Inventory public required init(_ v: Inventory) { @@ -358,17 +358,17 @@ public class InventoryCoder: NSObject, NSCoding, CodingType { } public required init?(coder aDecoder: NSCoder) { - let product = Product.decode(aDecoder.decodeObjectForKey("product")) + let product = Product.decode(aDecoder.decodeObject(forKey: "product")) value = Inventory(product: product!) } - public func encodeWithCoder(aCoder: NSCoder) { - aCoder.encodeObject(value.product.encoded, forKey: "product") + public func encode(with aCoder: NSCoder) { + aCoder.encode(value.product.encoded, forKey: "product") } } -public class ManagerMetadataCoder: NSObject, NSCoding, CodingType { +public class ManagerMetadataCoder: NSObject, NSCoding, CodingProtocol { public let value: Manager.Metadata public required init(_ v: Manager.Metadata) { @@ -376,12 +376,12 @@ public class ManagerMetadataCoder: NSObject, NSCoding, CodingType { } public required init?(coder aDecoder: NSCoder) { - let numberOfDirectReports = aDecoder.decodeIntegerForKey("numberOfDirectReports") + let numberOfDirectReports = aDecoder.decodeInteger(forKey: "numberOfDirectReports") value = Manager.Metadata(numberOfDirectReports: numberOfDirectReports) } - public func encodeWithCoder(aCoder: NSCoder) { - aCoder.encodeInteger(value.numberOfDirectReports, forKey: "numberOfDirectReports") + public func encode(with aCoder: NSCoder) { + aCoder.encode(value.numberOfDirectReports, forKey: "numberOfDirectReports") } } @@ -389,7 +389,7 @@ public class ManagerMetadataCoder: NSObject, NSCoding, CodingType { public func products() -> YapDB.Fetch { - let grouping: YapDB.View.Grouping = .ByMetadata({ (_, collection, key, metadata) -> String! in + let grouping: YapDB.View.Grouping = .byMetadata({ (_, collection, key, metadata) -> String! in if collection == Product.collection { if let metadata = Product.Metadata.decode(metadata) { return "category: \(metadata.categoryIdentifier)" @@ -398,13 +398,13 @@ public func products() -> YapDB.Fetch { return nil }) - let sorting: YapDB.View.Sorting = .ByObject({ (_, group, collection1, key1, object1, collection2, key2, object2) -> NSComparisonResult in + let sorting: YapDB.View.Sorting = .byObject({ (_, group, collection1, key1, object1, collection2, key2, object2) -> ComparisonResult in if let product1 = Product.decode(object1) { if let product2 = Product.decode(object2) { return product1.name.caseInsensitiveCompare(product2.name) } } - return .OrderedSame + return .orderedSame }) let view = YapDB.View( @@ -413,7 +413,7 @@ public func products() -> YapDB.Fetch { sorting: sorting, collections: [Product.collection]) - return .View(view) + return .view(view) } diff --git a/Tests/ObjectWithNoMetadataTests.swift b/Tests/ObjectWithNoMetadataTests.swift index 5c125d6..21e6963 100644 --- a/Tests/ObjectWithNoMetadataTests.swift +++ b/Tests/ObjectWithNoMetadataTests.swift @@ -29,8 +29,8 @@ class ObjectWithNoMetadataTests: XCTestCase { var reader: Read! - var dispatchQueue: dispatch_queue_t! - var operationQueue: NSOperationQueue! + var dispatchQueue: DispatchQueue! + var operationQueue: OperationQueue! override func setUp() { super.setUp() @@ -50,8 +50,8 @@ class ObjectWithNoMetadataTests: XCTestCase { connection.writeTransaction = writeTransaction database.connection = connection - dispatchQueue = dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0) - operationQueue = NSOperationQueue() + dispatchQueue = DispatchQueue.global(qos: .default) + operationQueue = OperationQueue() } override func tearDown() { @@ -90,7 +90,7 @@ class ObjectWithNoMetadataTests: XCTestCase { readTransaction.keys = keys } - func checkTransactionDidWriteItem(result: TypeUnderTest) { + func checkTransactionDidWriteItem(_ result: TypeUnderTest) { XCTAssertEqual(result.identifier, item.identifier) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) @@ -98,15 +98,15 @@ class ObjectWithNoMetadataTests: XCTestCase { XCTAssertNil(writeTransaction.didWriteAtIndexes[0].2) } - func checkTransactionDidWriteItems(result: [TypeUnderTest]) { + func checkTransactionDidWriteItems(_ result: [TypeUnderTest]) { XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) - XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) + XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sorted(), indexes.map { $0.key }.sorted()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertFalse(result.isEmpty) XCTAssertEqual(Set(result), Set(items)) } - func checkTransactionDidReadItem(result: TypeUnderTest?) -> Bool { + func checkTransactionDidReadItem(_ result: TypeUnderTest?) -> Bool { guard let result = result else { return false } @@ -115,12 +115,12 @@ class ObjectWithNoMetadataTests: XCTestCase { return true } - func checkTransactionDidReadItems(result: [TypeUnderTest]) -> Bool { - if result.isEmpty { + func checkTransactionDidReadItems(_ result: [TypeUnderTest?]) -> Bool { + if result.flatMap({$0}).isEmpty { return false } XCTAssertEqual(Set(readTransaction.didReadAtIndexes), Set(indexes)) - XCTAssertEqual(result.count, items.count) + XCTAssertEqual(result.flatMap({$0}).count, items.count) return true } @@ -287,24 +287,24 @@ class Functional_Write_ObjectWithNoMetadataTests: ObjectWithNoMetadataTests { func test__connection__async_write_item() { var result: TypeUnderTest! - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") connection.asyncWrite(item) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItem(result) XCTAssertTrue(connection.didAsyncWrite) } func test__connection__async_write_items() { var result: [TypeUnderTest] = [] - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") connection.asyncWrite(items) { received in result = received expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItems(result) XCTAssertTrue(connection.didAsyncWrite) } @@ -339,23 +339,23 @@ class Functional_Remove_ObjectWithNoMetadataTests: ObjectWithNoMetadataTests { } func test__connection_async_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() connection.asyncRemove(item) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_async_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() connection.asyncRemove(items) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didAsyncWrite) } @@ -554,8 +554,8 @@ class Persistable_Read_ObjectWithNoMetadataTests: ObjectWithNoMetadataTests { reader = Read(readTransaction) let (items, missing) = reader.filterExisting(keys) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.identifier }, items.prefixUpTo(1).map { $0.identifier }) - XCTAssertEqual(missing, Array(keys.suffixFrom(1))) + XCTAssertEqual(items.map { $0.identifier }, items.prefix(upTo: 1).map { $0.identifier }) + XCTAssertEqual(missing, Array(keys.suffix(from: 1))) } // Reading - With Connection @@ -633,8 +633,8 @@ class Persistable_Read_ObjectWithNoMetadataTests: ObjectWithNoMetadataTests { let (items, missing) = reader.filterExisting(keys) XCTAssertTrue(connection.didRead) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.identifier }, items.prefixUpTo(1).map { $0.identifier }) - XCTAssertEqual(missing, Array(keys.suffixFrom(1))) + XCTAssertEqual(items.map { $0.identifier }, items.prefix(upTo: 1).map { $0.identifier }) + XCTAssertEqual(missing, Array(keys.suffix(from: 1))) } } @@ -651,20 +651,20 @@ class Persistable_Write_ObjectWithNoMetadataTests: ObjectWithNoMetadataTests { } func test__item_persistable__write_async_using_connection() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") var result: TypeUnderTest! = nil item.asyncWrite(connection) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItem(result) XCTAssertTrue(connection.didAsyncWrite) } func test__item_persistable__write_using_opertion() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") let operation = item.writeOperation(connection) operation.completionBlock = { @@ -672,7 +672,7 @@ class Persistable_Write_ObjectWithNoMetadataTests: ObjectWithNoMetadataTests { } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].1.identifier, item.identifier) @@ -690,20 +690,20 @@ class Persistable_Write_ObjectWithNoMetadataTests: ObjectWithNoMetadataTests { } func test__items_persistable__write_async_using_connection() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") var result: [TypeUnderTest] = [] items.asyncWrite(connection) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItems(result) XCTAssertTrue(connection.didAsyncWrite) } func test__items_persistable__write_using_opertion() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") let operation = items.writeOperation(connection) operation.completionBlock = { @@ -711,9 +711,9 @@ class Persistable_Write_ObjectWithNoMetadataTests: ObjectWithNoMetadataTests { } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) - XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) + XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sorted(), indexes.map { $0.key }.sorted()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertTrue(connection.didWrite) } @@ -735,25 +735,25 @@ class Persistable_Remove_ObjectWithNoMetadataTests: ObjectWithNoMetadataTests { } func test__connection_async_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() item.asyncRemove(connection) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_operation_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() let operation = item.removeOperation(connection) operation.completionBlock = { expectation.fulfill() } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didWrite) } @@ -773,25 +773,25 @@ class Persistable_Remove_ObjectWithNoMetadataTests: ObjectWithNoMetadataTests { } func test__connection_async_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() items.asyncRemove(connection) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_operation_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() let operation = items.removeOperation(connection) operation.completionBlock = { expectation.fulfill() } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didWrite) } diff --git a/Tests/ObjectWithObjectMetadataTests.swift b/Tests/ObjectWithObjectMetadataTests.swift index ea690a2..21bd9f6 100644 --- a/Tests/ObjectWithObjectMetadataTests.swift +++ b/Tests/ObjectWithObjectMetadataTests.swift @@ -32,8 +32,8 @@ class ObjectWithObjectMetadataTests: XCTestCase { var reader: Read! - var dispatchQueue: dispatch_queue_t! - var operationQueue: NSOperationQueue! + var dispatchQueue: DispatchQueue! + var operationQueue: OperationQueue! override func setUp() { super.setUp() @@ -53,8 +53,8 @@ class ObjectWithObjectMetadataTests: XCTestCase { connection.writeTransaction = writeTransaction database.connection = connection - dispatchQueue = dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0) - operationQueue = NSOperationQueue() + dispatchQueue = DispatchQueue.global(qos: .default) + operationQueue = OperationQueue() } override func tearDown() { @@ -86,12 +86,12 @@ class ObjectWithObjectMetadataTests: XCTestCase { TypeUnderTest(id: "beatle-4", name: "Ringo") ] metadatas = [metadata] - items.suffixFrom(1).forEach { _ in metadatas.append(NSDate()) } + items.suffix(from: 1).forEach { _ in metadatas.append(NSDate()) } } func configureForReadingSingle() { readTransaction.object = item - readTransaction.metadata = metadata + readTransaction.metadata = metadata as AnyObject? } func configureForReadingMultiple() { @@ -100,7 +100,7 @@ class ObjectWithObjectMetadataTests: XCTestCase { readTransaction.keys = keys } - func checkTransactionDidWriteItem(result: YapItem) { + func checkTransactionDidWriteItem(_ result: YapItem) { XCTAssertEqual(result.value.identifier, item.identifier) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) @@ -108,15 +108,15 @@ class ObjectWithObjectMetadataTests: XCTestCase { XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].2 as? NSDate, metadata) } - func checkTransactionDidWriteItems(result: [YapItem]) { + func checkTransactionDidWriteItems(_ result: [YapItem]) { XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) - XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) + XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sorted(), indexes.map { $0.key }.sorted()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertFalse(result.isEmpty) XCTAssertEqual(Set(result.map({$0.value})), Set(items)) } - func checkTransactionDidReadItem(result: YapItem?) -> Bool { + func checkTransactionDidReadItem(_ result: YapItem?) -> Bool { guard let result = result else { return false } @@ -127,12 +127,12 @@ class ObjectWithObjectMetadataTests: XCTestCase { return true } - func checkTransactionDidReadItems(result: [YapItem]) -> Bool { - if result.isEmpty { + func checkTransactionDidReadItems(_ result: [YapItem?]) -> Bool { + if result.flatMap({$0}).isEmpty { return false } XCTAssertEqual(Set(readTransaction.didReadAtIndexes), Set(indexes)) - XCTAssertEqual(result.count, items.count) + XCTAssertEqual(result.flatMap({$0}).count, items.count) return true } @@ -299,24 +299,24 @@ class Functional_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTe func test__connection__async_write_item() { var result: YapItem! - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") connection.asyncWriteWithMetadata(YapItem(item, metadata)) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItem(result) XCTAssertTrue(connection.didAsyncWrite) } func test__connection__async_write_items() { var result: [YapItem] = [] - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") connection.asyncWriteWithMetadata(items.yapItems(with: metadatas)) { received in result = received expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItems(result) XCTAssertTrue(connection.didAsyncWrite) } @@ -351,23 +351,23 @@ class Functional_Remove_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataT } func test__connection_async_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() connection.asyncRemove(item) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_async_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() connection.asyncRemove(items) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didAsyncWrite) } @@ -443,14 +443,14 @@ class Persistable_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTe func test__reader__in_transaction_at_index_2() { configureForReadingSingle() reader = Read(readTransaction) - let atIndex: YapDB.Index -> YapItem? = reader.withMetadataInTransactionAtIndex(readTransaction) + let atIndex: (YapDB.Index) -> YapItem? = reader.withMetadataInTransactionAtIndex(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(atIndex(index))) } func test__reader__at_index_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataAtIndexInTransaction(index) + let inTransaction: (TestableReadTransaction) -> YapItem? = reader.withMetadataAtIndexInTransaction(index) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -474,14 +474,14 @@ class Persistable_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTe func test__reader__in_transaction_by_key_2() { configureForReadingSingle() reader = Read(readTransaction) - let byKey: String -> YapItem? = reader.withMetadataInTransactionByKey(readTransaction) + let byKey: (String) -> YapItem? = reader.withMetadataInTransactionByKey(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(byKey(key))) } func test__reader__by_key_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataByKeyInTransaction(key) + let inTransaction: (TestableReadTransaction) -> YapItem? = reader.withMetadataByKeyInTransaction(key) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -566,8 +566,8 @@ class Persistable_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTe reader = Read(readTransaction) let (items, missing): ([YapItem], [String]) = reader.withMetadataFilterExisting(keys) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.value.identifier }, items.prefixUpTo(1).map { $0.value.identifier }) - XCTAssertEqual(missing, Array(keys.suffixFrom(1))) + XCTAssertEqual(items.map { $0.value.identifier }, items.prefix(upTo: 1).map { $0.value.identifier }) + XCTAssertEqual(missing, Array(keys.suffix(from: 1))) } // Reading - With Connection @@ -645,8 +645,8 @@ class Persistable_Read_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataTe let (items, missing) = reader.filterExisting(keys) XCTAssertTrue(connection.didRead) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.identifier }, items.prefixUpTo(1).map { $0.identifier }) - XCTAssertEqual(missing, Array(keys.suffixFrom(1))) + XCTAssertEqual(items.map { $0.identifier }, items.prefix(upTo: 1).map { $0.identifier }) + XCTAssertEqual(missing, Array(keys.suffix(from: 1))) } } @@ -663,20 +663,20 @@ class Persistable_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataT } func test__item_persistable__write_async_using_connection() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") var result: YapItem! = nil item.asyncWriteWithMetadata(connection, metadata: metadata) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItem(result) XCTAssertTrue(connection.didAsyncWrite) } func test__item_persistable__write_using_opertion() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") let operation = item.writeWithMetadataOperation(connection, metadata: metadata) operation.completionBlock = { @@ -684,7 +684,7 @@ class Persistable_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataT } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].1.identifier, item.identifier) @@ -702,20 +702,20 @@ class Persistable_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataT } func test__items_persistable__write_async_using_connection() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") var result: [YapItem] = [] items.asyncWriteWithMetadata(connection, metadata: metadatas) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItems(result) XCTAssertTrue(connection.didAsyncWrite) } func test__items_persistable__write_using_opertion() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") let operation = items.writeOperation(connection) operation.completionBlock = { @@ -723,9 +723,9 @@ class Persistable_Write_ObjectWithObjectMetadataTests: ObjectWithObjectMetadataT } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) - XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) + XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sorted(), indexes.map { $0.key }.sorted()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertTrue(connection.didWrite) } @@ -747,25 +747,25 @@ class Persistable_Remove_ObjectWithObjectMetadataTests: ObjectWithObjectMetadata } func test__connection_async_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() item.asyncRemove(connection) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_operation_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() let operation = item.removeOperation(connection) operation.completionBlock = { expectation.fulfill() } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didWrite) } @@ -785,25 +785,25 @@ class Persistable_Remove_ObjectWithObjectMetadataTests: ObjectWithObjectMetadata } func test__connection_async_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() items.asyncRemove(connection) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_operation_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() let operation = items.removeOperation(connection) operation.completionBlock = { expectation.fulfill() } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didWrite) } diff --git a/Tests/ObjectWithValueMetadataTests.swift b/Tests/ObjectWithValueMetadataTests.swift index 7fb12a4..65336db 100644 --- a/Tests/ObjectWithValueMetadataTests.swift +++ b/Tests/ObjectWithValueMetadataTests.swift @@ -32,8 +32,8 @@ class ObjectWithValueMetadataTests: XCTestCase { var reader: Read! - var dispatchQueue: dispatch_queue_t! - var operationQueue: NSOperationQueue! + var dispatchQueue: DispatchQueue! + var operationQueue: OperationQueue! override func setUp() { super.setUp() @@ -53,8 +53,8 @@ class ObjectWithValueMetadataTests: XCTestCase { connection.writeTransaction = writeTransaction database.connection = connection - dispatchQueue = dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0) - operationQueue = NSOperationQueue() + dispatchQueue = DispatchQueue.global(qos: .default) + operationQueue = OperationQueue() } override func tearDown() { @@ -104,7 +104,7 @@ class ObjectWithValueMetadataTests: XCTestCase { readTransaction.keys = keys } - func checkTransactionDidWriteItem(result: YapItem) { + func checkTransactionDidWriteItem(_ result: YapItem) { XCTAssertEqual(result.value.identifier, item.identifier) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) @@ -112,15 +112,15 @@ class ObjectWithValueMetadataTests: XCTestCase { XCTAssertEqual(MetadataTypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].2), metadata) } - func checkTransactionDidWriteItems(result: [YapItem]) { + func checkTransactionDidWriteItems(_ result: [YapItem]) { XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) - XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) + XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sorted(), indexes.map { $0.key }.sorted()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertFalse(result.isEmpty) XCTAssertEqual(Set(result.map({$0.value})), Set(items)) } - func checkTransactionDidReadItem(result: YapItem?) -> Bool { + func checkTransactionDidReadItem(_ result: YapItem?) -> Bool { guard let result = result else { return false } @@ -131,12 +131,12 @@ class ObjectWithValueMetadataTests: XCTestCase { return true } - func checkTransactionDidReadItems(result: [YapItem]) -> Bool { - if result.isEmpty { + func checkTransactionDidReadItems(_ result: [YapItem?]) -> Bool { + if result.flatMap({$0}).isEmpty { return false } XCTAssertEqual(Set(readTransaction.didReadAtIndexes), Set(indexes)) - XCTAssertEqual(result.count, items.count) + XCTAssertEqual(result.flatMap({$0}).count, items.count) return true } @@ -303,24 +303,24 @@ class Functional_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTest func test__connection__async_write_item() { var result: YapItem! - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") connection.asyncWriteWithMetadata(YapItem(item, metadata)) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItem(result) XCTAssertTrue(connection.didAsyncWrite) } func test__connection__async_write_items() { var result: [YapItem] = [] - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") connection.asyncWriteWithMetadata(items.yapItems(with: metadatas)) { received in result = received expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItems(result) XCTAssertTrue(connection.didAsyncWrite) } @@ -355,23 +355,23 @@ class Functional_Remove_ObjectWithValueMetadataTests: ObjectWithValueMetadataTes } func test__connection_async_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() connection.asyncRemove(item) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_async_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() connection.asyncRemove(items) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didAsyncWrite) } @@ -447,14 +447,14 @@ class Persistable_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTest func test__reader__in_transaction_at_index_2() { configureForReadingSingle() reader = Read(readTransaction) - let atIndex: YapDB.Index -> YapItem? = reader.withMetadataInTransactionAtIndex(readTransaction) + let atIndex: (YapDB.Index) -> YapItem? = reader.withMetadataInTransactionAtIndex(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(atIndex(index))) } func test__reader__at_index_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataAtIndexInTransaction(index) + let inTransaction: (TestableReadTransaction) -> YapItem? = reader.withMetadataAtIndexInTransaction(index) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -478,14 +478,14 @@ class Persistable_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTest func test__reader__in_transaction_by_key_2() { configureForReadingSingle() reader = Read(readTransaction) - let byKey: String -> YapItem? = reader.withMetadataInTransactionByKey(readTransaction) + let byKey: (String) -> YapItem? = reader.withMetadataInTransactionByKey(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(byKey(key))) } func test__reader__by_key_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataByKeyInTransaction(key) + let inTransaction: (TestableReadTransaction) -> YapItem? = reader.withMetadataByKeyInTransaction(key) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -570,8 +570,8 @@ class Persistable_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTest reader = Read(readTransaction) let (items, missing): ([YapItem], [String]) = reader.withMetadataFilterExisting(keys) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.value.identifier }, items.prefixUpTo(1).map { $0.value.identifier }) - XCTAssertEqual(missing, Array(keys.suffixFrom(1))) + XCTAssertEqual(items.map { $0.value.identifier }, items.prefix(upTo: 1).map { $0.value.identifier }) + XCTAssertEqual(missing, Array(keys.suffix(from: 1))) } // Reading - With Connection @@ -649,8 +649,8 @@ class Persistable_Read_ObjectWithValueMetadataTests: ObjectWithValueMetadataTest let (items, missing) = reader.filterExisting(keys) XCTAssertTrue(connection.didRead) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.identifier }, items.prefixUpTo(1).map { $0.identifier }) - XCTAssertEqual(missing, Array(keys.suffixFrom(1))) + XCTAssertEqual(items.map { $0.identifier }, items.prefix(upTo: 1).map { $0.identifier }) + XCTAssertEqual(missing, Array(keys.suffix(from: 1))) } } @@ -667,20 +667,20 @@ class Persistable_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTes } func test__item_persistable__write_async_using_connection() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") var result: YapItem! = nil item.asyncWriteWithMetadata(connection, metadata: metadata) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItem(result) XCTAssertTrue(connection.didAsyncWrite) } func test__item_persistable__write_using_opertion() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") let operation = item.writeWithMetadataOperation(connection, metadata: metadata) operation.completionBlock = { @@ -688,7 +688,7 @@ class Persistable_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTes } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].1.identifier, item.identifier) @@ -706,20 +706,20 @@ class Persistable_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTes } func test__items_persistable__write_async_using_connection() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") var result: [YapItem] = [] items.asyncWriteWithMetadata(connection, metadata: metadatas) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItems(result) XCTAssertTrue(connection.didAsyncWrite) } func test__items_persistable__write_using_opertion() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") let operation = items.writeOperation(connection) operation.completionBlock = { @@ -727,9 +727,9 @@ class Persistable_Write_ObjectWithValueMetadataTests: ObjectWithValueMetadataTes } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) - XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) + XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sorted(), indexes.map { $0.key }.sorted()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertTrue(connection.didWrite) } @@ -751,25 +751,25 @@ class Persistable_Remove_ObjectWithValueMetadataTests: ObjectWithValueMetadataTe } func test__connection_async_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() item.asyncRemove(connection) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_operation_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() let operation = item.removeOperation(connection) operation.completionBlock = { expectation.fulfill() } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didWrite) } @@ -789,25 +789,25 @@ class Persistable_Remove_ObjectWithValueMetadataTests: ObjectWithValueMetadataTe } func test__connection_async_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() items.asyncRemove(connection) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_operation_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() let operation = items.removeOperation(connection) operation.completionBlock = { expectation.fulfill() } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didWrite) } diff --git a/Tests/ReadWriteTests.swift b/Tests/ReadWriteTests.swift index f526e8d..71f048c 100644 --- a/Tests/ReadWriteTests.swift +++ b/Tests/ReadWriteTests.swift @@ -58,8 +58,8 @@ class ReadWriteBaseTests: XCTestCase { ] } - func writeItemsToDatabase(db: YapDatabase) { - db.makeNewConnection().writeWithMetadata(items.yapItems(with: metadatas)) + func writeItemsToDatabase(_ db: YapDatabase) { + _ = db.makeNewConnection().writeWithMetadata(items.yapItems(with: metadatas)) } } @@ -70,7 +70,7 @@ class ReadTests: ReadWriteBaseTests { func test__initialize_with_transaction() { let db = YapDB.testDatabase() let connection = db.makeNewConnection() - connection.readWithBlock { transaction in + connection.read { transaction in self.reader = Read(transaction) XCTAssertNotNil(self.reader) } @@ -95,7 +95,7 @@ class ReadTests: ReadWriteBaseTests { func test__getting_reader_from_persistable_with_transaction() { let db = YapDB.testDatabase() let connection = db.makeNewConnection() - connection.readWithBlock { transaction in + connection.read { transaction in self.reader = Employee.read(transaction) XCTAssertNotNil(self.reader) } diff --git a/Tests/Support.swift b/Tests/Support.swift index b256da5..b0d5f66 100644 --- a/Tests/Support.swift +++ b/Tests/Support.swift @@ -12,7 +12,7 @@ import Foundation class TestableReadTransaction { var keys: [String] = [] - var didKeysInCollection: String? = .None + var didKeysInCollection: String? = .none var object: AnyObject? { get { return objects.first } @@ -53,7 +53,7 @@ class TestableReadTransaction { currentReadIndex += 1 return object } - return .None + return .none } func getNextMetadata() -> AnyObject? { @@ -62,23 +62,23 @@ class TestableReadTransaction { currentMetadataReadIndex += 1 return object } - return .None + return .none } } extension TestableReadTransaction: ReadTransactionType { - func keysInCollection(collection: String) -> [String] { + func keysInCollection(_ collection: String) -> [String] { didKeysInCollection = collection return keys } - func readAtIndex(index: YapDB.Index) -> AnyObject? { + func readAtIndex(_ index: YapDB.Index) -> AnyObject? { didReadAtIndexes.append(index) return getNextObject() } - func readMetadataAtIndex(index: YapDB.Index) -> AnyObject? { + func readMetadataAtIndex(_ index: YapDB.Index) -> AnyObject? { didReadMetadataAtIndexes.append(index) return getNextMetadata() } @@ -93,14 +93,14 @@ class TestableWriteTransaction: TestableReadTransaction { extension TestableWriteTransaction: WriteTransactionType { - func writeAtIndex(index: YapDB.Index, object: AnyObject, metadata: AnyObject?) { + func writeAtIndex(_ index: YapDB.Index, object: AnyObject, metadata: AnyObject?) { didWriteAtIndexes.append((index, object, metadata)) } func removeAtIndexes< - Indexes where - Indexes: SequenceType, - Indexes.Generator.Element == YapDB.Index>(indexes: Indexes) { + Indexes>(_ indexes: Indexes) where + Indexes: Sequence, + Indexes.Iterator.Element == YapDB.Index { didRemoveAtIndexes = Array(indexes) } } @@ -118,34 +118,34 @@ class TestableConnection { extension TestableConnection: ConnectionType { - func read(block: TestableReadTransaction -> T) -> T { + func read(_ block: @escaping (TestableReadTransaction) -> T) -> T { didRead = true return block(readTransaction) } - func write(block: TestableWriteTransaction -> T) -> T { + func write(_ block: @escaping (TestableWriteTransaction) -> T) -> T { didWrite = true return block(writeTransaction) } - func asyncRead(block: TestableReadTransaction -> T, queue: dispatch_queue_t, completion: (T) -> Void) { + func asyncRead(_ block: @escaping (TestableReadTransaction) -> T, queue: DispatchQueue, completion: @escaping (T) -> Void) { didAsyncRead = true - dispatch_async(queue) { [transaction = self.readTransaction] in + queue.async { [transaction = self.readTransaction] in completion(block(transaction)) } } - func asyncWrite(block: TestableWriteTransaction -> T, queue: dispatch_queue_t, completion: (T -> Void)? = .None) { + func asyncWrite(_ block: @escaping (TestableWriteTransaction) -> T, queue: DispatchQueue, completion: ((T) -> Void)? = .none) { didAsyncWrite = true - dispatch_async(queue) { [transaction = self.writeTransaction] in + queue.async { [transaction = self.writeTransaction] in let result = block(transaction) completion?(result) } } - func writeBlockOperation(block: TestableWriteTransaction -> Void) -> NSOperation { + func writeBlockOperation(_ block: @escaping (TestableWriteTransaction) -> Void) -> Operation { didWriteBlockOperation = true - return NSBlockOperation { block(self.writeTransaction) } + return BlockOperation { block(self.writeTransaction) } } } diff --git a/Tests/ValueWithNoMetadataTests.swift b/Tests/ValueWithNoMetadataTests.swift index dc15538..4d1d89d 100644 --- a/Tests/ValueWithNoMetadataTests.swift +++ b/Tests/ValueWithNoMetadataTests.swift @@ -31,8 +31,8 @@ class ValueWithNoMetadataTests: XCTestCase { var reader: Read! - var dispatchQueue: dispatch_queue_t! - var operationQueue: NSOperationQueue! + var dispatchQueue: DispatchQueue! + var operationQueue: OperationQueue! override func setUp() { super.setUp() @@ -52,8 +52,8 @@ class ValueWithNoMetadataTests: XCTestCase { connection.writeTransaction = writeTransaction database.connection = connection - dispatchQueue = dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0) - operationQueue = NSOperationQueue() + dispatchQueue = DispatchQueue.global(qos: .default) + operationQueue = OperationQueue() } override func tearDown() { @@ -74,12 +74,12 @@ class ValueWithNoMetadataTests: XCTestCase { } func createPersistables() { - item = .QRCode("I have no idea what the string of a QR Code might look like") + item = .qrCode("I have no idea what the string of a QR Code might look like") items = [ item, - .UPCA(23, 42, 78, 93), - .QRCode("This is a different QR Code!"), - .UPCA(68, 59, 147, 291) + .upca(23, 42, 78, 93), + .qrCode("This is a different QR Code!"), + .upca(68, 59, 147, 291) ] } @@ -92,7 +92,7 @@ class ValueWithNoMetadataTests: XCTestCase { readTransaction.keys = keys } - func checkTransactionDidWriteItem(result: TypeUnderTest) { + func checkTransactionDidWriteItem(_ result: TypeUnderTest) { XCTAssertEqual(result.identifier, item.identifier) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) @@ -100,15 +100,15 @@ class ValueWithNoMetadataTests: XCTestCase { XCTAssertNil(writeTransaction.didWriteAtIndexes[0].2) } - func checkTransactionDidWriteItems(result: [TypeUnderTest]) { + func checkTransactionDidWriteItems(_ result: [TypeUnderTest]) { XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) - XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) + XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sorted(), indexes.map { $0.key }.sorted()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertFalse(result.isEmpty) XCTAssertEqual(Set(result), Set(items)) } - func checkTransactionDidReadItem(result: TypeUnderTest?) -> Bool { + func checkTransactionDidReadItem(_ result: TypeUnderTest?) -> Bool { guard let result = result else { return false } @@ -117,12 +117,12 @@ class ValueWithNoMetadataTests: XCTestCase { return true } - func checkTransactionDidReadItems(result: [TypeUnderTest]) -> Bool { - if result.isEmpty { + func checkTransactionDidReadItems(_ result: [TypeUnderTest?]) -> Bool { + if result.flatMap({$0}).isEmpty { return false } XCTAssertEqual(Set(readTransaction.didReadAtIndexes), Set(indexes)) - XCTAssertEqual(result.count, items.count) + XCTAssertEqual(result.flatMap({$0}).count, items.count) return true } @@ -289,24 +289,24 @@ class Functional_Write_ValueWithNoMetadataTests: ValueWithNoMetadataTests { func test__connection__async_write_item() { var result: TypeUnderTest! - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") connection.asyncWrite(item) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItem(result) XCTAssertTrue(connection.didAsyncWrite) } func test__connection__async_write_items() { var result: [TypeUnderTest] = [] - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") connection.asyncWrite(items) { received in result = received expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItems(result) XCTAssertTrue(connection.didAsyncWrite) } @@ -341,23 +341,23 @@ class Functional_Remove_ValueWithNoMetadataTests: ValueWithNoMetadataTests { } func test__connection_async_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() connection.asyncRemove(item) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_async_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() connection.asyncRemove(items) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didAsyncWrite) } @@ -556,8 +556,8 @@ class Persistable_Read_ValueWithNoMetadataTests: ValueWithNoMetadataTests { reader = Read(readTransaction) let (items, missing) = reader.filterExisting(keys) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.identifier }, items.prefixUpTo(1).map { $0.identifier }) - XCTAssertEqual(missing, Array(keys.suffixFrom(1))) + XCTAssertEqual(items.map { $0.identifier }, items.prefix(upTo: 1).map { $0.identifier }) + XCTAssertEqual(missing, Array(keys.suffix(from: 1))) } // Reading - With Connection @@ -635,8 +635,8 @@ class Persistable_Read_ValueWithNoMetadataTests: ValueWithNoMetadataTests { let (items, missing) = reader.filterExisting(keys) XCTAssertTrue(connection.didRead) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.identifier }, items.prefixUpTo(1).map { $0.identifier }) - XCTAssertEqual(missing, Array(keys.suffixFrom(1))) + XCTAssertEqual(items.map { $0.identifier }, items.prefix(upTo: 1).map { $0.identifier }) + XCTAssertEqual(missing, Array(keys.suffix(from: 1))) } } @@ -653,20 +653,20 @@ class Persistable_Write_ValueWithNoMetadataTests: ValueWithNoMetadataTests { } func test__item_persistable__write_async_using_connection() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") var result: TypeUnderTest! = nil item.asyncWrite(connection) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItem(result) XCTAssertTrue(connection.didAsyncWrite) } func test__item_persistable__write_using_opertion() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") let operation = item.writeOperation(connection) operation.completionBlock = { @@ -674,7 +674,7 @@ class Persistable_Write_ValueWithNoMetadataTests: ValueWithNoMetadataTests { } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(TypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].1)!, item) @@ -692,20 +692,20 @@ class Persistable_Write_ValueWithNoMetadataTests: ValueWithNoMetadataTests { } func test__items_persistable__write_async_using_connection() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") var result: [TypeUnderTest] = [] items.asyncWrite(connection) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItems(result) XCTAssertTrue(connection.didAsyncWrite) } func test__items_persistable__write_using_opertion() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") let operation = items.writeOperation(connection) operation.completionBlock = { @@ -713,9 +713,9 @@ class Persistable_Write_ValueWithNoMetadataTests: ValueWithNoMetadataTests { } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) - XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) + XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sorted(), indexes.map { $0.key }.sorted()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertTrue(connection.didWrite) } @@ -737,25 +737,25 @@ class Persistable_Remove_ValueWithNoMetadataTests: ValueWithNoMetadataTests { } func test__connection_async_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() item.asyncRemove(connection) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_operation_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() let operation = item.removeOperation(connection) operation.completionBlock = { expectation.fulfill() } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didWrite) } @@ -775,25 +775,25 @@ class Persistable_Remove_ValueWithNoMetadataTests: ValueWithNoMetadataTests { } func test__connection_async_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() items.asyncRemove(connection) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_operation_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() let operation = items.removeOperation(connection) operation.completionBlock = { expectation.fulfill() } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didWrite) } diff --git a/Tests/ValueWithObjectMetadataTests.swift b/Tests/ValueWithObjectMetadataTests.swift index b954985..aa3fd3d 100644 --- a/Tests/ValueWithObjectMetadataTests.swift +++ b/Tests/ValueWithObjectMetadataTests.swift @@ -32,8 +32,8 @@ class ValueWithObjectMetadataTests: XCTestCase { var reader: Read! - var dispatchQueue: dispatch_queue_t! - var operationQueue: NSOperationQueue! + var dispatchQueue: DispatchQueue! + var operationQueue: OperationQueue! override func setUp() { super.setUp() @@ -53,8 +53,8 @@ class ValueWithObjectMetadataTests: XCTestCase { connection.writeTransaction = writeTransaction database.connection = connection - dispatchQueue = dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0) - operationQueue = NSOperationQueue() + dispatchQueue = DispatchQueue.global(qos: .default) + operationQueue = OperationQueue() } override func tearDown() { @@ -81,30 +81,30 @@ class ValueWithObjectMetadataTests: XCTestCase { Product( identifier: "vodka-123", name: "Belvidere", - barcode: .UPCA(1, 2, 3, 4) + barcode: .upca(1, 2, 3, 4) ), Product( identifier: "gin-123", name: "Boxer Gin", - barcode: .UPCA(5, 10, 15, 20) + barcode: .upca(5, 10, 15, 20) ), Product( identifier: "rum-123", name: "Mount Gay Rum", - barcode: .UPCA(12, 24, 39, 48) + barcode: .upca(12, 24, 39, 48) ), Product( identifier: "gin-234", name: "Monkey 47", - barcode: .UPCA(31, 62, 93, 124) + barcode: .upca(31, 62, 93, 124) ) ] metadatas = [ - NSNumber(integer: 12), - NSNumber(integer: 13), - NSNumber(integer: 14), - NSNumber(integer: 15) + NSNumber(value: 12 as Int), + NSNumber(value: 13 as Int), + NSNumber(value: 14 as Int), + NSNumber(value: 15 as Int) ] items = products.map { TypeUnderTest(product: $0) } item = items[0] @@ -122,7 +122,7 @@ class ValueWithObjectMetadataTests: XCTestCase { readTransaction.keys = keys } - func checkTransactionDidWriteItem(result: YapItem) { + func checkTransactionDidWriteItem(_ result: YapItem) { XCTAssertEqual(result.value.identifier, item.identifier) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) @@ -130,15 +130,15 @@ class ValueWithObjectMetadataTests: XCTestCase { XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].2 as? NSNumber, metadata) } - func checkTransactionDidWriteItems(result: [YapItem]) { + func checkTransactionDidWriteItems(_ result: [YapItem]) { XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) - XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) + XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sorted(), indexes.map { $0.key }.sorted()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertFalse(result.isEmpty) XCTAssertEqual(Set(result.map({$0.value})), Set(items)) } - func checkTransactionDidReadItem(result: YapItem?) -> Bool { + func checkTransactionDidReadItem(_ result: YapItem?) -> Bool { XCTAssertEqual(readTransaction.didReadAtIndex, index) guard let result = result else { return false @@ -149,16 +149,16 @@ class ValueWithObjectMetadataTests: XCTestCase { return true } - func checkTransactionDidReadItems(result: [YapItem]) -> Bool { - if result.isEmpty { + func checkTransactionDidReadItems(_ result: [YapItem?]) -> Bool { + if result.flatMap({$0}).isEmpty { return false } XCTAssertEqual(Set(readTransaction.didReadAtIndexes), Set(indexes)) - XCTAssertEqual(result.count, items.count) + XCTAssertEqual(result.flatMap({$0}).count, items.count) return true } - func checkTransactionDidReadMetadataItem(result: MetadataTypeUnderTest?) -> Bool { + func checkTransactionDidReadMetadataItem(_ result: MetadataTypeUnderTest?) -> Bool { XCTAssertNil(readTransaction.didReadAtIndex) guard let result = result else { return false @@ -168,13 +168,13 @@ class ValueWithObjectMetadataTests: XCTestCase { return true } - func checkTransactionDidReadMetadataItems(result: [MetadataTypeUnderTest]) -> Bool { + func checkTransactionDidReadMetadataItems(_ result: [MetadataTypeUnderTest?]) -> Bool { XCTAssertTrue(readTransaction.didReadAtIndexes.isEmpty) - if result.isEmpty { + if result.flatMap({$0}).isEmpty { return false } XCTAssertEqual(Set(readTransaction.didReadMetadataAtIndexes), Set(indexes)) - XCTAssertEqual(result.count, items.count) + XCTAssertEqual(result.flatMap({$0}).count, items.count) return true } @@ -385,24 +385,24 @@ class Functional_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTest func test__connection__async_write_item() { var result: YapItem! - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") connection.asyncWriteWithMetadata(YapItem(item, metadata)) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItem(result) XCTAssertTrue(connection.didAsyncWrite) } func test__connection__async_write_items() { var result: [YapItem] = [] - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") connection.asyncWriteWithMetadata(items.yapItems(with: metadatas)) { received in result = received expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItems(result) XCTAssertTrue(connection.didAsyncWrite) } @@ -437,23 +437,23 @@ class Functional_Remove_ValueWithObjectMetadataTests: ValueWithObjectMetadataTes } func test__connection_async_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() connection.asyncRemove(item) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_async_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() connection.asyncRemove(items) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didAsyncWrite) } @@ -529,14 +529,14 @@ class Persistable_Read_ValueWithObjectMetadataTests: ValueWithObjectMetadataTest func test__reader__in_transaction_at_index_2() { configureForReadingSingle() reader = Read(readTransaction) - let atIndex: YapDB.Index -> YapItem? = reader.withMetadataInTransactionAtIndex(readTransaction) + let atIndex: (YapDB.Index) -> YapItem? = reader.withMetadataInTransactionAtIndex(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(atIndex(index))) } func test__reader__at_index_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataAtIndexInTransaction(index) + let inTransaction: (TestableReadTransaction) -> YapItem? = reader.withMetadataAtIndexInTransaction(index) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -560,14 +560,14 @@ class Persistable_Read_ValueWithObjectMetadataTests: ValueWithObjectMetadataTest func test__reader__in_transaction_by_key_2() { configureForReadingSingle() reader = Read(readTransaction) - let byKey: String -> YapItem? = reader.withMetadataInTransactionByKey(readTransaction) + let byKey: (String) -> YapItem? = reader.withMetadataInTransactionByKey(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(byKey(key))) } func test__reader__by_key_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataByKeyInTransaction(key) + let inTransaction: (TestableReadTransaction) -> YapItem? = reader.withMetadataByKeyInTransaction(key) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -652,8 +652,8 @@ class Persistable_Read_ValueWithObjectMetadataTests: ValueWithObjectMetadataTest reader = Read(readTransaction) let (items, missing): ([YapItem], [String]) = reader.withMetadataFilterExisting(keys) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.value.identifier }, items.prefixUpTo(1).map { $0.value.identifier }) - XCTAssertEqual(missing, Array(keys.suffixFrom(1))) + XCTAssertEqual(items.map { $0.value.identifier }, items.prefix(upTo: 1).map { $0.value.identifier }) + XCTAssertEqual(missing, Array(keys.suffix(from: 1))) } // Reading - With Connection @@ -731,8 +731,8 @@ class Persistable_Read_ValueWithObjectMetadataTests: ValueWithObjectMetadataTest let (items, missing) = reader.filterExisting(keys) XCTAssertTrue(connection.didRead) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.identifier }, items.prefixUpTo(1).map { $0.identifier }) - XCTAssertEqual(missing, Array(keys.suffixFrom(1))) + XCTAssertEqual(items.map { $0.identifier }, items.prefix(upTo: 1).map { $0.identifier }) + XCTAssertEqual(missing, Array(keys.suffix(from: 1))) } } @@ -800,20 +800,20 @@ class Persistable_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTes } func test__item_persistable__write_async_using_connection() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") var result: YapItem! = nil item.asyncWriteWithMetadata(connection, metadata: metadata) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItem(result) XCTAssertTrue(connection.didAsyncWrite) } func test__item_persistable__write_using_opertion() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") let operation = item.writeWithMetadataOperation(connection, metadata: metadata) operation.completionBlock = { @@ -821,7 +821,7 @@ class Persistable_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTes } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(TypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].1)!, item) @@ -839,20 +839,20 @@ class Persistable_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTes } func test__items_persistable__write_async_using_connection() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") var result: [YapItem] = [] items.asyncWriteWithMetadata(connection, metadata: metadatas) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItems(result) XCTAssertTrue(connection.didAsyncWrite) } func test__items_persistable__write_using_opertion() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") let operation = items.writeWithMetadataOperation(connection, metadata: metadatas) operation.completionBlock = { @@ -860,9 +860,9 @@ class Persistable_Write_ValueWithObjectMetadataTests: ValueWithObjectMetadataTes } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) - XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) + XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sorted(), indexes.map { $0.key }.sorted()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertTrue(connection.didWrite) } @@ -884,25 +884,25 @@ class Persistable_Remove_ValueWithObjectMetadataTests: ValueWithObjectMetadataTe } func test__connection_async_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() item.asyncRemove(connection) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_operation_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() let operation = item.removeOperation(connection) operation.completionBlock = { expectation.fulfill() } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didWrite) } @@ -922,25 +922,25 @@ class Persistable_Remove_ValueWithObjectMetadataTests: ValueWithObjectMetadataTe } func test__connection_async_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() items.asyncRemove(connection) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_operation_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() let operation = items.removeOperation(connection) operation.completionBlock = { expectation.fulfill() } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didWrite) } diff --git a/Tests/ValueWithValueMetadataTests.swift b/Tests/ValueWithValueMetadataTests.swift index 4b1b257..5453217 100644 --- a/Tests/ValueWithValueMetadataTests.swift +++ b/Tests/ValueWithValueMetadataTests.swift @@ -32,8 +32,8 @@ class ValueWithValueMetadataTests: XCTestCase { var reader: Read! - var dispatchQueue: dispatch_queue_t! - var operationQueue: NSOperationQueue! + var dispatchQueue: DispatchQueue! + var operationQueue: OperationQueue! override func setUp() { super.setUp() @@ -53,8 +53,8 @@ class ValueWithValueMetadataTests: XCTestCase { connection.writeTransaction = writeTransaction database.connection = connection - dispatchQueue = dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0) - operationQueue = NSOperationQueue() + dispatchQueue = DispatchQueue.global(qos: .default) + operationQueue = OperationQueue() } override func tearDown() { @@ -80,7 +80,7 @@ class ValueWithValueMetadataTests: XCTestCase { item = TypeUnderTest( identifier: "vodka-123", name: "Belvidere", - barcode: .UPCA(1, 2, 3, 4) + barcode: .upca(1, 2, 3, 4) ) metadata = TypeUnderTest.Metadata(categoryIdentifier: 1) items = [ @@ -88,17 +88,17 @@ class ValueWithValueMetadataTests: XCTestCase { TypeUnderTest( identifier: "gin-123", name: "Boxer Gin", - barcode: .UPCA(5, 10, 15, 20) + barcode: .upca(5, 10, 15, 20) ), TypeUnderTest( identifier: "rum-123", name: "Mount Gay Rum", - barcode: .UPCA(12, 24, 39, 48) + barcode: .upca(12, 24, 39, 48) ), TypeUnderTest( identifier: "gin-234", name: "Monkey 47", - barcode: .UPCA(31, 62, 93, 124) + barcode: .upca(31, 62, 93, 124) ) ] metadatas = [ @@ -120,7 +120,7 @@ class ValueWithValueMetadataTests: XCTestCase { readTransaction.keys = keys } - func checkTransactionDidWriteItem(result: YapItem) { + func checkTransactionDidWriteItem(_ result: YapItem) { XCTAssertEqual(result.value.identifier, item.identifier) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) @@ -128,15 +128,15 @@ class ValueWithValueMetadataTests: XCTestCase { XCTAssertEqual(MetadataTypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].2), metadata) } - func checkTransactionDidWriteItems(result: [YapItem]) { + func checkTransactionDidWriteItems(_ result: [YapItem]) { XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) - XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) + XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sorted(), indexes.map { $0.key }.sorted()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertFalse(result.isEmpty) XCTAssertEqual(Set(result.map({$0.value})), Set(items)) } - func checkTransactionDidReadItem(result: YapItem?) -> Bool { + func checkTransactionDidReadItem(_ result: YapItem?) -> Bool { XCTAssertEqual(readTransaction.didReadAtIndex, index) guard let result = result else { return false @@ -147,16 +147,16 @@ class ValueWithValueMetadataTests: XCTestCase { return true } - func checkTransactionDidReadItems(result: [YapItem]) -> Bool { - if result.isEmpty { + func checkTransactionDidReadItems(_ result: [YapItem?]) -> Bool { + if result.flatMap({$0}).isEmpty { return false } XCTAssertEqual(Set(readTransaction.didReadAtIndexes), Set(indexes)) - XCTAssertEqual(result.count, items.count) + XCTAssertEqual(result.flatMap({$0}).count, items.count) return true } - func checkTransactionDidReadMetadataItem(result: MetadataTypeUnderTest?) -> Bool { + func checkTransactionDidReadMetadataItem(_ result: MetadataTypeUnderTest?) -> Bool { XCTAssertNil(readTransaction.didReadAtIndex) guard let result = result else { return false @@ -166,13 +166,13 @@ class ValueWithValueMetadataTests: XCTestCase { return true } - func checkTransactionDidReadMetadataItems(result: [MetadataTypeUnderTest]) -> Bool { + func checkTransactionDidReadMetadataItems(_ result: [MetadataTypeUnderTest?]) -> Bool { XCTAssertTrue(readTransaction.didReadAtIndexes.isEmpty) - if result.isEmpty { + if result.flatMap({$0}).isEmpty { return false } XCTAssertEqual(Set(readTransaction.didReadMetadataAtIndexes), Set(indexes)) - XCTAssertEqual(result.count, items.count) + XCTAssertEqual(result.flatMap({$0}).count, items.count) return true } @@ -382,24 +382,24 @@ class Functional_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests func test__connection__async_write_item() { var result: YapItem! - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") connection.asyncWriteWithMetadata(YapItem(item, metadata)) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItem(result) XCTAssertTrue(connection.didAsyncWrite) } func test__connection__async_write_items() { var result: [YapItem] = [] - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") connection.asyncWriteWithMetadata(items.yapItems(with: metadatas)) { received in result = received expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItems(result) XCTAssertTrue(connection.didAsyncWrite) } @@ -434,23 +434,23 @@ class Functional_Remove_ValueWithValueMetadataTests: ValueWithValueMetadataTests } func test__connection_async_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() connection.asyncRemove(item) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_async_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() connection.asyncRemove(items) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didAsyncWrite) } @@ -526,14 +526,14 @@ class Persistable_Read_ValueWithValueMetadataTests: ValueWithValueMetadataTests func test__reader__in_transaction_at_index_2() { configureForReadingSingle() reader = Read(readTransaction) - let atIndex: YapDB.Index -> YapItem? = reader.withMetadataInTransactionAtIndex(readTransaction) + let atIndex: (YapDB.Index) -> YapItem? = reader.withMetadataInTransactionAtIndex(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(atIndex(index))) } func test__reader__at_index_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataAtIndexInTransaction(index) + let inTransaction: (TestableReadTransaction) -> YapItem? = reader.withMetadataAtIndexInTransaction(index) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -557,14 +557,14 @@ class Persistable_Read_ValueWithValueMetadataTests: ValueWithValueMetadataTests func test__reader__in_transaction_by_key_2() { configureForReadingSingle() reader = Read(readTransaction) - let byKey: String -> YapItem? = reader.withMetadataInTransactionByKey(readTransaction) + let byKey: (String) -> YapItem? = reader.withMetadataInTransactionByKey(readTransaction) XCTAssertTrue(checkTransactionDidReadItem(byKey(key))) } func test__reader__by_key_in_transaction() { configureForReadingSingle() reader = Read(readTransaction) - let inTransaction: TestableReadTransaction -> YapItem? = reader.withMetadataByKeyInTransaction(key) + let inTransaction: (TestableReadTransaction) -> YapItem? = reader.withMetadataByKeyInTransaction(key) XCTAssertTrue(checkTransactionDidReadItem(inTransaction(readTransaction))) } @@ -649,8 +649,8 @@ class Persistable_Read_ValueWithValueMetadataTests: ValueWithValueMetadataTests reader = Read(readTransaction) let (items, missing): ([YapItem], [String]) = reader.withMetadataFilterExisting(keys) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.value.identifier }, items.prefixUpTo(1).map { $0.value.identifier }) - XCTAssertEqual(missing, Array(keys.suffixFrom(1))) + XCTAssertEqual(items.map { $0.value.identifier }, items.prefix(upTo: 1).map { $0.value.identifier }) + XCTAssertEqual(missing, Array(keys.suffix(from: 1))) } // Reading - With Connection @@ -728,8 +728,8 @@ class Persistable_Read_ValueWithValueMetadataTests: ValueWithValueMetadataTests let (items, missing) = reader.filterExisting(keys) XCTAssertTrue(connection.didRead) XCTAssertEqual(readTransaction.didReadAtIndexes.first!, indexes.first!) - XCTAssertEqual(items.map { $0.identifier }, items.prefixUpTo(1).map { $0.identifier }) - XCTAssertEqual(missing, Array(keys.suffixFrom(1))) + XCTAssertEqual(items.map { $0.identifier }, items.prefix(upTo: 1).map { $0.identifier }) + XCTAssertEqual(missing, Array(keys.suffix(from: 1))) } } @@ -797,20 +797,20 @@ class Persistable_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests } func test__item_persistable__write_async_using_connection() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") var result: YapItem! = nil item.asyncWriteWithMetadata(connection, metadata: metadata) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItem(result) XCTAssertTrue(connection.didAsyncWrite) } func test__item_persistable__write_using_opertion() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") let operation = item.writeWithMetadataOperation(connection, metadata: metadata) operation.completionBlock = { @@ -818,7 +818,7 @@ class Persistable_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) XCTAssertEqual(writeTransaction.didWriteAtIndexes[0].0, index) XCTAssertEqual(TypeUnderTest.decode(writeTransaction.didWriteAtIndexes[0].1)!, item) @@ -836,20 +836,20 @@ class Persistable_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests } func test__items_persistable__write_async_using_connection() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") var result: [YapItem] = [] items.asyncWriteWithMetadata(connection, metadata: metadatas) { tmp in result = tmp expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidWriteItems(result) XCTAssertTrue(connection.didAsyncWrite) } func test__items_persistable__write_using_opertion() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") let operation = items.writeWithMetadataOperation(connection, metadata: metadatas) operation.completionBlock = { @@ -857,9 +857,9 @@ class Persistable_Write_ValueWithValueMetadataTests: ValueWithValueMetadataTests } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) XCTAssertFalse(writeTransaction.didWriteAtIndexes.isEmpty) - XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sort(), indexes.map { $0.key }.sort()) + XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.0.key }.sorted(), indexes.map { $0.key }.sorted()) XCTAssertEqual(writeTransaction.didWriteAtIndexes.map { $0.2 }.count, items.count) XCTAssertTrue(connection.didWrite) } @@ -881,25 +881,25 @@ class Persistable_Remove_ValueWithValueMetadataTests: ValueWithValueMetadataTest } func test__connection_async_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() item.asyncRemove(connection) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_operation_remove_item() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingSingle() let operation = item.removeOperation(connection) operation.completionBlock = { expectation.fulfill() } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItem() XCTAssertTrue(connection.didWrite) } @@ -919,25 +919,25 @@ class Persistable_Remove_ValueWithValueMetadataTests: ValueWithValueMetadataTest } func test__connection_async_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() items.asyncRemove(connection) { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didAsyncWrite) } func test__connection_operation_remove_items() { - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") configureForReadingMultiple() let operation = items.removeOperation(connection) operation.completionBlock = { expectation.fulfill() } operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) checkTransactionDidRemoveItems() XCTAssertTrue(connection.didWrite) } diff --git a/Tests/YapDatabaseExtensionsTests.swift b/Tests/YapDatabaseExtensionsTests.swift index b244ca2..b14886a 100644 --- a/Tests/YapDatabaseExtensionsTests.swift +++ b/Tests/YapDatabaseExtensionsTests.swift @@ -66,7 +66,7 @@ class YapDatabaseReadTransactionTests: ReadWriteBaseTests { writeItemsToDatabase(db) let keys = db.makeNewConnection().read { $0.keysInCollection(Employee.collection) } XCTAssertNotNil(keys) - XCTAssertEqual(keys.sort(), items.map { $0.key }.sort()) + XCTAssertEqual(keys.sorted(), items.map { $0.key }.sorted()) } func test__read_at_index_returns_nil_with_empty_db() { @@ -101,7 +101,7 @@ class YapDatabaseReadWriteTransactionTests: ReadWriteBaseTests { func test__write_at_index_without_metadata() { let db = YapDB.testDatabase() - db.makeNewConnection().readWriteWithBlock { transaction in + db.makeNewConnection().readWrite { transaction in transaction.writeAtIndex(self.index, object: self.item) } let written = Employee.read(db).atIndex(index) @@ -111,7 +111,7 @@ class YapDatabaseReadWriteTransactionTests: ReadWriteBaseTests { func test__write_at_index_with_metadata() { let db = YapDB.testDatabase() - db.makeNewConnection().readWriteWithBlock { transaction in + db.makeNewConnection().readWrite { transaction in transaction.writeAtIndex(self.index, object: self.item, metadata: self.metadata) } let written: YapItem? = Employee.read(db).withMetadataAtIndex(index) @@ -123,10 +123,10 @@ class YapDatabaseReadWriteTransactionTests: ReadWriteBaseTests { func test__remove_at_indexes() { let db = YapDB.testDatabase() - db.makeNewConnection().write(items) + _ = db.makeNewConnection().write(items) XCTAssertNotNil(Employee.read(db).atIndex(index)) - db.makeNewConnection().readWriteWithBlock { transaction in + db.makeNewConnection().readWrite { transaction in transaction.removeAtIndexes(self.indexes) } @@ -136,23 +136,23 @@ class YapDatabaseReadWriteTransactionTests: ReadWriteBaseTests { class YapDatabaseConnectionTests: ReadWriteBaseTests { - var dispatchQueue: dispatch_queue_t! - var operationQueue: NSOperationQueue! + var dispatchQueue: DispatchQueue! + var operationQueue: OperationQueue! override func setUp() { super.setUp() - dispatchQueue = dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0) - operationQueue = NSOperationQueue() + dispatchQueue = DispatchQueue.global(qos: .default) + operationQueue = OperationQueue() } func test__async_read() { let db = YapDB.testDatabase() - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") - db.makeNewConnection().write(item) + _ = db.makeNewConnection().write(item) XCTAssertNotNil(Employee.read(db).atIndex(index)) - var received: Employee? = .None + var received: Employee? = .none db.newConnection().asyncRead({ transaction -> Employee? in return transaction.readAtIndex(self.index) as? Employee }, queue: dispatchQueue) { (result: Employee?) in @@ -160,16 +160,16 @@ class YapDatabaseConnectionTests: ReadWriteBaseTests { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) XCTAssertNotNil(received) } func test__async_write() { let db = YapDB.testDatabase() - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") - var written: Employee? = .None + var written: Employee? = .none db.makeNewConnection().asyncWrite({ transaction -> Employee? in transaction.writeAtIndex(self.index, object: self.item, metadata: self.metadata) return self.item @@ -178,7 +178,7 @@ class YapDatabaseConnectionTests: ReadWriteBaseTests { expectation.fulfill() } - waitForExpectationsWithTimeout(3.0, handler: nil) + waitForExpectations(timeout: 3.0, handler: nil) XCTAssertNotNil(written) XCTAssertNotNil(Employee.read(db).atIndex(index)) @@ -186,7 +186,7 @@ class YapDatabaseConnectionTests: ReadWriteBaseTests { func test__writeBlockOperation() { let db = YapDB.testDatabase() - let expectation = expectationWithDescription("Test: \(#function)") + let expectation = self.expectation(description: "Test: \(#function)") var didExecuteWithTransaction = false let operation = db.makeNewConnection().writeBlockOperation { transaction in @@ -196,8 +196,8 @@ class YapDatabaseConnectionTests: ReadWriteBaseTests { operationQueue.addOperation(operation) - waitForExpectationsWithTimeout(3.0, handler: nil) - XCTAssertTrue(operation.finished) + waitForExpectations(timeout: 3.0, handler: nil) + XCTAssertTrue(operation.isFinished) XCTAssertTrue(didExecuteWithTransaction) } } @@ -240,7 +240,7 @@ class ValueCodingTests: XCTestCase { item = Product( identifier: "vodka-123", name: "Belvidere", - barcode: .UPCA(1, 2, 3, 4) + barcode: .upca(1, 2, 3, 4) ) metadata = Product.Metadata(categoryIdentifier: 1) items = [ @@ -248,17 +248,17 @@ class ValueCodingTests: XCTestCase { Product( identifier: "gin-123", name: "Boxer Gin", - barcode: .UPCA(5, 10, 15, 20) + barcode: .upca(5, 10, 15, 20) ), Product( identifier: "rum-123", name: "Mount Gay Rum", - barcode: .UPCA(12, 24, 39, 48) + barcode: .upca(12, 24, 39, 48) ), Product( identifier: "gin-234", name: "Monkey 47", - barcode: .UPCA(31, 62, 93, 124) + barcode: .upca(31, 62, 93, 124) ) ] metadatas = [ @@ -280,11 +280,11 @@ class ValueCodingTests: XCTestCase { func test__index_is_codable() { let db = YapDB.testDatabase() - db.makeNewConnection().readWriteWithBlock { transaction in + db.makeNewConnection().readWrite { transaction in transaction.setObject(self.index.encoded, forKey: "test-index", inCollection: "test-index-collection") } - let unarchived = YapDB.Index.decode(db.makeNewConnection().read { $0.objectForKey("test-index", inCollection: "test-index-collection") }) + let unarchived = YapDB.Index.decode(db.makeNewConnection().read { $0.object(forKey: "test-index", inCollection: "test-index-collection") }) XCTAssertNotNil(unarchived) XCTAssertEqual(unarchived!, index) } diff --git a/YapDatabaseExtensions.xcodeproj/project.pbxproj b/YapDatabaseExtensions.xcodeproj/project.pbxproj index 00e948c..dd69886 100644 --- a/YapDatabaseExtensions.xcodeproj/project.pbxproj +++ b/YapDatabaseExtensions.xcodeproj/project.pbxproj @@ -270,11 +270,11 @@ TargetAttributes = { 656BAE6F1D9A4CCC008279EE = { CreatedOnToolsVersion = 7.3.1; - LastSwiftMigration = 0800; + LastSwiftMigration = 0820; }; 656BAE791D9A4CCC008279EE = { CreatedOnToolsVersion = 7.3.1; - LastSwiftMigration = 0800; + LastSwiftMigration = 0820; }; }; }; @@ -323,7 +323,6 @@ ValueCoding, YapDatabase, CocoaLumberjack, - Reachability, ); name = "Copy Carthage Frameworks"; outputPaths = ( @@ -497,7 +496,7 @@ ONLY_ACTIVE_ARCH = YES; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -543,7 +542,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks"; MTL_ENABLE_DEBUG_INFO = NO; SKIP_INSTALL = YES; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; }; @@ -593,7 +592,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = macosx; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Debug; }; @@ -633,7 +632,7 @@ MACOSX_DEPLOYMENT_TARGET = 10.11; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = macosx; - SWIFT_VERSION = 2.3; + SWIFT_VERSION = 3.0; }; name = Release; };