diff --git a/Package.swift b/Package.swift index 4639dc9..686a23b 100755 --- a/Package.swift +++ b/Package.swift @@ -12,5 +12,6 @@ let package = Package( targets: [ .target(name: "Meow", dependencies: ["MongoKitten"]), .testTarget(name: "MeowTests", dependencies: ["Meow"]), - ] + ], + swiftLanguageVersions: [.v4_2, .version("5")] ) diff --git a/Sources/Meow/Compatibility.swift b/Sources/Meow/Compatibility.swift new file mode 100644 index 0000000..fa408a7 --- /dev/null +++ b/Sources/Meow/Compatibility.swift @@ -0,0 +1,7 @@ +#if !swift(>=5.0) +internal extension Array { + func firstIndex(where predicate: (Element) throws -> Bool) rethrows -> Int? { + return try self.index(where: predicate) + } +} +#endif diff --git a/Sources/Meow/Migrations.swift b/Sources/Meow/Migrations.swift index 4c55ef9..f7ad1fa 100644 --- a/Sources/Meow/Migrations.swift +++ b/Sources/Meow/Migrations.swift @@ -60,7 +60,7 @@ public class Migrator { public extension Migrator where M: QueryableModel { - public func ensureValue(for keyPath: KeyPath, default value: V) { + func ensureValue(for keyPath: KeyPath, default value: V) { add { collection in let path = try M.makeQueryPath(for: keyPath) @@ -81,7 +81,7 @@ public extension Migrator where M: QueryableModel { /// This provides maximum flexibility. /// /// - parameter transform: A closure that will be executed on every model document in the database. The returned document from this closure replaces the existing document in the database. - public func map(_ transform: @escaping (Document) throws -> (Document)) { + func map(_ transform: @escaping (Document) throws -> (Document)) { add { collection in return collection.find().sequentialForEach { original in let replacement = try transform(original) diff --git a/Sources/Meow/Model.swift b/Sources/Meow/Model.swift index 8b5f81d..91d7cbd 100644 --- a/Sources/Meow/Model.swift +++ b/Sources/Meow/Model.swift @@ -29,7 +29,7 @@ public protocol Model: _Model { // MARK: - Default implementations public extension Model { - public func save(to context: Context) -> EventLoopFuture { + func save(to context: Context) -> EventLoopFuture { return context.save(self) } @@ -49,12 +49,12 @@ public extension Model { public extension Model where Self: Hashable { /// Provides a default implementation of Hashable for Models, that uses only the _id for Hashable conformance - public var hashValue: Int { - return _id.hashValue + func hash(into hasher: inout Hasher) { + hasher.combine(_id) } /// Compares the given models using the _id - public static func == (lhs: Self, rhs: Self) -> Bool { + static func == (lhs: Self, rhs: Self) -> Bool { return lhs._id == rhs._id } diff --git a/Sources/Meow/Reference.swift b/Sources/Meow/Reference.swift index fa0c787..7f5a036 100644 --- a/Sources/Meow/Reference.swift +++ b/Sources/Meow/Reference.swift @@ -14,8 +14,8 @@ public struct Reference: Hashable, Resolvable { } /// Makes a reference hashable - public var hashValue: Int { - return reference.hashValue + public func hash(into hasher: inout Hasher) { + hasher.combine(reference) } /// Creates a reference to an entity @@ -75,21 +75,21 @@ public protocol Resolvable { } public extension Resolvable where Result: QueryableModel { - public func resolve(in context: Context, where query: ModelQuery) -> EventLoopFuture { + func resolve(in context: Context, where query: ModelQuery) -> EventLoopFuture { return self.resolve(in: context, where: query.query) } - public func resolveIfPresent(in context: Context, where query: ModelQuery) -> EventLoopFuture { + func resolveIfPresent(in context: Context, where query: ModelQuery) -> EventLoopFuture { return self.resolveIfPresent(in: context, where: query.query) } } public extension Resolvable where Result: Sequence, Result.Element: QueryableModel { - public func resolve(in context: Context, where query: ModelQuery) -> EventLoopFuture { + func resolve(in context: Context, where query: ModelQuery) -> EventLoopFuture { return self.resolve(in: context, where: query.query) } - public func resolveIfPresent(in context: Context, where query: ModelQuery) -> EventLoopFuture { + func resolveIfPresent(in context: Context, where query: ModelQuery) -> EventLoopFuture { return self.resolveIfPresent(in: context, where: query.query) } } diff --git a/Sources/Meow/TypesafeQuerying.swift b/Sources/Meow/TypesafeQuerying.swift index 49903a1..70a1b03 100644 --- a/Sources/Meow/TypesafeQuerying.swift +++ b/Sources/Meow/TypesafeQuerying.swift @@ -42,7 +42,7 @@ public struct ModelQuery { } public extension AggregateCursor { - public func match(_ query: ModelQuery) -> AggregateCursor { + func match(_ query: ModelQuery) -> AggregateCursor { return self.match(query.query) } } diff --git a/Sources/Meow/Updatable.swift b/Sources/Meow/Updatable.swift index bc54489..5ff5205 100644 --- a/Sources/Meow/Updatable.swift +++ b/Sources/Meow/Updatable.swift @@ -80,7 +80,7 @@ extension WritableKeyPath: OptionalKeyPath where Value: ExpressibleByNilLiteral public extension Decoder { /// - returns: An array containing the key paths that were updated - public func update(_ instance: T, withAllowedKeyPaths keyPaths: [MeowWritableKeyPath]) throws -> [PartialKeyPath] { + func update(_ instance: T, withAllowedKeyPaths keyPaths: [MeowWritableKeyPath]) throws -> [PartialKeyPath] { let container = try self.container(keyedBy: UpdateCodingKey.self) // must pass as inout to the KeyPath, hence the var @@ -108,7 +108,7 @@ public extension JSONDecoder { } /// - returns: An array containing the key paths that were updated - public func update(_ instance: T, from data: Data, withAllowedKeyPaths keyPaths: [MeowWritableKeyPath]) throws -> [PartialKeyPath] { + func update(_ instance: T, from data: Data, withAllowedKeyPaths keyPaths: [MeowWritableKeyPath]) throws -> [PartialKeyPath] { // It seems not ideal that MeowWritableKeyPath currently is not restricted to Root == T assert(keyPaths.allSatisfy { $0 as? PartialKeyPath != nil }, "Calling update for a certain model with allowed key paths of a different type does not make sense") diff --git a/Sources/Meow/WrappedIdentifier.swift b/Sources/Meow/WrappedIdentifier.swift index b05d8d4..2a66154 100644 --- a/Sources/Meow/WrappedIdentifier.swift +++ b/Sources/Meow/WrappedIdentifier.swift @@ -2,9 +2,8 @@ internal class AnyInstanceIdentifier: Hashable { fileprivate init() {} - var hashValue: Int { + func hash(into hasher: inout Hasher) { assertionFailure("The HashValue implementation of AnyInstanceIdentifier should not be used") - return 0 } static func == (lhs: AnyInstanceIdentifier, rhs: AnyInstanceIdentifier) -> Bool { @@ -25,8 +24,9 @@ internal final class InstanceIdentifier : AnyInstanceIdentifier { super.init() } - override var hashValue: Int { - return ObjectIdentifier(M.self).hashValue ^ identifier.hashValue + override func hash(into hasher: inout Hasher) { + hasher.combine(ObjectIdentifier(M.self)) + hasher.combine(identifier.hashValue) } override func equals(_ other: AnyInstanceIdentifier) -> Bool {