Skip to content
This repository was archived by the owner on Jan 3, 2021. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ let package = Package(
targets: [
.target(name: "Meow", dependencies: ["MongoKitten"]),
.testTarget(name: "MeowTests", dependencies: ["Meow"]),
]
],
swiftLanguageVersions: [.v4_2, .version("5")]
)
7 changes: 7 additions & 0 deletions Sources/Meow/Compatibility.swift
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions Sources/Meow/Migrations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class Migrator<M: Model> {

public extension Migrator where M: QueryableModel {

public func ensureValue<V: Encodable>(for keyPath: KeyPath<M, V>, default value: V) {
func ensureValue<V: Encodable>(for keyPath: KeyPath<M, V>, default value: V) {
add { collection in
let path = try M.makeQueryPath(for: keyPath)

Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions Sources/Meow/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public protocol Model: _Model {

// MARK: - Default implementations
public extension Model {
public func save(to context: Context) -> EventLoopFuture<Void> {
func save(to context: Context) -> EventLoopFuture<Void> {
return context.save(self)
}

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

Expand Down
12 changes: 6 additions & 6 deletions Sources/Meow/Reference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public struct Reference<M: Model>: 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
Expand Down Expand Up @@ -75,21 +75,21 @@ public protocol Resolvable {
}

public extension Resolvable where Result: QueryableModel {
public func resolve(in context: Context, where query: ModelQuery<Result>) -> EventLoopFuture<Result> {
func resolve(in context: Context, where query: ModelQuery<Result>) -> EventLoopFuture<Result> {
return self.resolve(in: context, where: query.query)
}

public func resolveIfPresent(in context: Context, where query: ModelQuery<Result>) -> EventLoopFuture<IfPresentResult> {
func resolveIfPresent(in context: Context, where query: ModelQuery<Result>) -> EventLoopFuture<IfPresentResult> {
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<Result.Element>) -> EventLoopFuture<Result> {
func resolve(in context: Context, where query: ModelQuery<Result.Element>) -> EventLoopFuture<Result> {
return self.resolve(in: context, where: query.query)
}

public func resolveIfPresent(in context: Context, where query: ModelQuery<Result.Element>) -> EventLoopFuture<IfPresentResult> {
func resolveIfPresent(in context: Context, where query: ModelQuery<Result.Element>) -> EventLoopFuture<IfPresentResult> {
return self.resolveIfPresent(in: context, where: query.query)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Meow/TypesafeQuerying.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public struct ModelQuery<M: QueryableModel> {
}

public extension AggregateCursor {
public func match<T>(_ query: ModelQuery<T>) -> AggregateCursor<Element> {
func match<T>(_ query: ModelQuery<T>) -> AggregateCursor<Element> {
return self.match(query.query)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Meow/Updatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: KeyPathQueryable>(_ instance: T, withAllowedKeyPaths keyPaths: [MeowWritableKeyPath]) throws -> [PartialKeyPath<T>] {
func update<T: KeyPathQueryable>(_ instance: T, withAllowedKeyPaths keyPaths: [MeowWritableKeyPath]) throws -> [PartialKeyPath<T>] {
let container = try self.container(keyedBy: UpdateCodingKey.self)

// must pass as inout to the KeyPath, hence the var
Expand Down Expand Up @@ -108,7 +108,7 @@ public extension JSONDecoder {
}

/// - returns: An array containing the key paths that were updated
public func update<T: QueryableModel>(_ instance: T, from data: Data, withAllowedKeyPaths keyPaths: [MeowWritableKeyPath]) throws -> [PartialKeyPath<T>] {
func update<T: QueryableModel>(_ instance: T, from data: Data, withAllowedKeyPaths keyPaths: [MeowWritableKeyPath]) throws -> [PartialKeyPath<T>] {
// It seems not ideal that MeowWritableKeyPath currently is not restricted to Root == T
assert(keyPaths.allSatisfy { $0 as? PartialKeyPath<T> != nil }, "Calling update for a certain model with allowed key paths of a different type does not make sense")

Expand Down
8 changes: 4 additions & 4 deletions Sources/Meow/WrappedIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -25,8 +24,9 @@ internal final class InstanceIdentifier<M: Model> : 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 {
Expand Down