mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-15 13:43:43 +01:00
WIP: typesafe queries
This commit is contained in:
@@ -72,10 +72,13 @@ class DynamicModelTests: XCTestCase {
|
||||
XCTAssertEqual(mascot.nickname*, "Riko")
|
||||
|
||||
|
||||
let p1 = (Bird.meta.species == "Swift")
|
||||
XCTAssertEqual(p1, Where("%K == %@", "species", "Swift").predicate)
|
||||
let p1 = Bird.where({ $0.species == "Swift" })
|
||||
XCTAssertEqual(p1.predicate, Where("%K == %@", "species", "Swift").predicate)
|
||||
|
||||
let p2 = (Mascot.meta.nickname == "Riko")
|
||||
XCTAssertEqual(p2, Where("%K == %@", "nickname", "Riko").predicate)
|
||||
let p2 = Mascot.where({ $0.nickname == "Riko" })
|
||||
XCTAssertEqual(p2.predicate, Where("%K == %@", "nickname", "Riko").predicate)
|
||||
|
||||
let p3 = Mascot.where({ $0.year == 2016 })
|
||||
XCTAssertEqual(p3.predicate, Where("%K == %@", "year", 2016).predicate)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,7 +168,7 @@ public extension ManagedObjectProtocol where Self: CoreStoreManagedObject {
|
||||
|
||||
public typealias Attribute = AttributeContainer<Self>
|
||||
|
||||
public static var meta: Self {
|
||||
static var meta: Self {
|
||||
|
||||
return self.init(nil)
|
||||
}
|
||||
@@ -184,6 +184,12 @@ public extension ManagedObjectProtocol where Self: CoreStoreManagedObject {
|
||||
|
||||
return attribute(self.meta).keyPath
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func `where`(_ condition: (Self) -> Where) -> Where {
|
||||
|
||||
return condition(self.meta)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -211,43 +217,36 @@ public postfix func * <O: ManagedObjectProtocol, V: ImportableAttributeType>(_ a
|
||||
|
||||
public extension AttributeContainer.Required where V: CVarArg {
|
||||
|
||||
public static func == (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
|
||||
public static func == (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> Where {
|
||||
|
||||
return NSPredicate(format: "%K == %@", argumentArray: [attribute.keyPath, value])
|
||||
return Where(attribute.keyPath, isEqualTo: value)
|
||||
}
|
||||
public static func < (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
|
||||
public static func < (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> Where {
|
||||
|
||||
return NSPredicate(format: "%K < %@", argumentArray: [attribute.keyPath, value])
|
||||
return Where("%K < %@", attribute.keyPath, value)
|
||||
}
|
||||
public static func > (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
|
||||
public static func > (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> Where {
|
||||
|
||||
return NSPredicate(format: "%K > %@", argumentArray: [attribute.keyPath, value])
|
||||
return Where("%K > %@", attribute.keyPath, value)
|
||||
}
|
||||
public static func <= (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
|
||||
public static func <= (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> Where {
|
||||
|
||||
return NSPredicate(format: "%K <= %@", argumentArray: [attribute.keyPath, value])
|
||||
return Where("%K <= %@", attribute.keyPath, value)
|
||||
}
|
||||
public static func >= (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
|
||||
public static func >= (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> Where {
|
||||
|
||||
return NSPredicate(format: "%K >= %@", argumentArray: [attribute.keyPath, value])
|
||||
return Where("%K >= %@", attribute.keyPath, value)
|
||||
}
|
||||
public static func != (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
|
||||
public static func != (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> Where {
|
||||
|
||||
return NSPredicate(format: "%K != %@", argumentArray: [attribute.keyPath, value])
|
||||
return Where("%K != %@", attribute.keyPath, value)
|
||||
}
|
||||
}
|
||||
public extension AttributeContainer.Optional where V: CVarArg {
|
||||
|
||||
public static func == (_ attribute: AttributeContainer<O>.Optional<V>, _ value: V?) -> NSPredicate {
|
||||
public static func == (_ attribute: AttributeContainer<O>.Optional<V>, _ value: V?) -> Where {
|
||||
|
||||
if let value = value {
|
||||
|
||||
return NSPredicate(format: "%K == %@", argumentArray: [attribute.keyPath, value])
|
||||
}
|
||||
else {
|
||||
|
||||
return NSPredicate(format: "%K == nil", attribute.keyPath)
|
||||
}
|
||||
return Where(attribute.keyPath, isEqualTo: value)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user