mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-18 06:57:15 +01:00
WIP: Dynamic models (Goodbye xcdatamodel files!)
This commit is contained in:
@@ -24,8 +24,8 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CoreGraphics
|
||||
import CoreData
|
||||
import CoreGraphics
|
||||
|
||||
|
||||
// MARK: - QueryableAttributeType
|
||||
@@ -44,155 +44,6 @@ public protocol QueryableAttributeType: Hashable, SelectResultType {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSManagedObjectID
|
||||
|
||||
extension NSManagedObjectID: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSManagedObjectID
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .objectIDAttributeType
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Self? {
|
||||
|
||||
@inline(__always)
|
||||
func forceCast<T: NSManagedObjectID>(_ value: Any) -> T? {
|
||||
|
||||
return value as? T
|
||||
}
|
||||
return forceCast(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSNumber
|
||||
|
||||
extension NSNumber: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSNumber
|
||||
|
||||
public class var cs_rawAttributeType: NSAttributeType {
|
||||
|
||||
return .integer64AttributeType
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Self? {
|
||||
|
||||
@inline(__always)
|
||||
func forceCast<T: NSNumber>(_ value: Any) -> T? {
|
||||
|
||||
return value as? T
|
||||
}
|
||||
return forceCast(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSNumber
|
||||
|
||||
extension NSDecimalNumber /*: QueryableAttributeType */ {
|
||||
|
||||
public override class var cs_rawAttributeType: NSAttributeType {
|
||||
|
||||
return .decimalAttributeType
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSString
|
||||
|
||||
extension NSString: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSString
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .stringAttributeType
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Self? {
|
||||
|
||||
@inline(__always)
|
||||
func forceCast<T: NSString>(_ value: Any) -> T? {
|
||||
|
||||
return value as? T
|
||||
}
|
||||
return forceCast(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSDate
|
||||
|
||||
extension NSDate: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSDate
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .dateAttributeType
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Self? {
|
||||
|
||||
@inline(__always)
|
||||
func forceCast<T: NSDate>(_ value: Any) -> T? {
|
||||
|
||||
return value as? T
|
||||
}
|
||||
return forceCast(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSData
|
||||
|
||||
extension NSData: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSData
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .binaryDataAttributeType
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Self? {
|
||||
|
||||
@inline(__always)
|
||||
func forceCast<T: NSData>(_ value: Any) -> T? {
|
||||
|
||||
return value as? T
|
||||
}
|
||||
return forceCast(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Bool
|
||||
|
||||
extension Bool: QueryableAttributeType {
|
||||
@@ -224,6 +75,138 @@ extension Bool: QueryableAttributeType {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - CGFloat
|
||||
|
||||
extension CGFloat: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSNumber
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .doubleAttributeType
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> CGFloat? {
|
||||
|
||||
return CGFloat(value.doubleValue)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self as NSNumber
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Data
|
||||
|
||||
extension Data: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSData
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .binaryDataAttributeType
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Data? {
|
||||
|
||||
return value as Data
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self as NSData
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Date
|
||||
|
||||
extension Date: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSDate
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .dateAttributeType
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Date? {
|
||||
|
||||
return value as Date
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self as NSDate
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Double
|
||||
|
||||
extension Double: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSNumber
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .doubleAttributeType
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Double? {
|
||||
|
||||
return value.doubleValue
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self as NSNumber
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Float
|
||||
|
||||
extension Float: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSNumber
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .floatAttributeType
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Float? {
|
||||
|
||||
return value.floatValue
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self as NSNumber
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Int
|
||||
|
||||
extension Int: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSNumber
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .integer64AttributeType
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Int? {
|
||||
|
||||
return value.intValue
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self as NSNumber
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Int8
|
||||
|
||||
extension Int8: QueryableAttributeType {
|
||||
@@ -312,112 +295,217 @@ extension Int64: QueryableAttributeType {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Int
|
||||
// MARK: - NSData
|
||||
|
||||
extension Int: QueryableAttributeType {
|
||||
extension NSData: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSNumber
|
||||
public typealias QueryableNativeType = NSData
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .integer64AttributeType
|
||||
public static let cs_rawAttributeType: NSAttributeType = .binaryDataAttributeType
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Int? {
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Self? {
|
||||
|
||||
return value.intValue
|
||||
@inline(__always)
|
||||
func forceCast<T: NSData>(_ value: Any) -> T? {
|
||||
|
||||
return value as? T
|
||||
}
|
||||
return forceCast(value)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self as NSNumber
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Double
|
||||
// MARK: - NSDate
|
||||
|
||||
extension Double: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSNumber
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .doubleAttributeType
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Double? {
|
||||
|
||||
return value.doubleValue
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self as NSNumber
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Float
|
||||
|
||||
extension Float: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSNumber
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .floatAttributeType
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Float? {
|
||||
|
||||
return value.floatValue
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self as NSNumber
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - CGFloat
|
||||
|
||||
extension CGFloat: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSNumber
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .doubleAttributeType
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> CGFloat? {
|
||||
|
||||
return CGFloat(value.doubleValue)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self as NSNumber
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Date
|
||||
|
||||
extension Date: QueryableAttributeType {
|
||||
extension NSDate: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSDate
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .dateAttributeType
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Date? {
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Self? {
|
||||
|
||||
return value as Date
|
||||
@inline(__always)
|
||||
func forceCast<T: NSDate>(_ value: Any) -> T? {
|
||||
|
||||
return value as? T
|
||||
}
|
||||
return forceCast(value)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self as NSDate
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSDecimalNumber
|
||||
|
||||
extension NSDecimalNumber /*: QueryableAttributeType */ {
|
||||
|
||||
public override class var cs_rawAttributeType: NSAttributeType {
|
||||
|
||||
return .decimalAttributeType
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSManagedObjectID
|
||||
|
||||
extension NSManagedObjectID: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSManagedObjectID
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .objectIDAttributeType
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Self? {
|
||||
|
||||
@inline(__always)
|
||||
func forceCast<T: NSManagedObjectID>(_ value: Any) -> T? {
|
||||
|
||||
return value as? T
|
||||
}
|
||||
return forceCast(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSNull
|
||||
|
||||
extension NSNull: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSNull
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .undefinedAttributeType
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Self? {
|
||||
|
||||
return self.init()
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSNumber
|
||||
|
||||
extension NSNumber: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSNumber
|
||||
|
||||
public class var cs_rawAttributeType: NSAttributeType {
|
||||
|
||||
return .integer64AttributeType
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Self? {
|
||||
|
||||
@inline(__always)
|
||||
func forceCast<T: NSNumber>(_ value: Any) -> T? {
|
||||
|
||||
return value as? T
|
||||
}
|
||||
return forceCast(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSString
|
||||
|
||||
extension NSString: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSString
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .stringAttributeType
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Self? {
|
||||
|
||||
@inline(__always)
|
||||
func forceCast<T: NSString>(_ value: Any) -> T? {
|
||||
|
||||
return value as? T
|
||||
}
|
||||
return forceCast(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSURL
|
||||
|
||||
extension NSURL: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSString
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .stringAttributeType
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Self? {
|
||||
|
||||
return self.init(string: value as String)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return (self as URL).absoluteString as QueryableNativeType
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSUUID
|
||||
|
||||
extension NSUUID: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSString
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .stringAttributeType
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Self? {
|
||||
|
||||
return self.init(uuidString: value.lowercased)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self.uuidString.lowercased() as QueryableNativeType
|
||||
}
|
||||
}
|
||||
|
||||
@@ -444,45 +532,45 @@ extension String: QueryableAttributeType {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Data
|
||||
// MARK: - URL
|
||||
|
||||
extension Data: QueryableAttributeType {
|
||||
extension URL: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSData
|
||||
public typealias QueryableNativeType = NSString
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .binaryDataAttributeType
|
||||
public static let cs_rawAttributeType: NSAttributeType = .stringAttributeType
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Data? {
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> URL? {
|
||||
|
||||
return value as Data
|
||||
return self.init(string: value as String)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self as NSData
|
||||
return self.absoluteString as QueryableNativeType
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSNull
|
||||
// MARK: - UUID
|
||||
|
||||
extension NSNull: QueryableAttributeType {
|
||||
extension UUID: QueryableAttributeType {
|
||||
|
||||
public typealias QueryableNativeType = NSNull
|
||||
public typealias QueryableNativeType = NSString
|
||||
|
||||
public static let cs_rawAttributeType: NSAttributeType = .undefinedAttributeType
|
||||
public static let cs_rawAttributeType: NSAttributeType = .stringAttributeType
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> Self? {
|
||||
@inline(__always)
|
||||
public static func cs_fromQueryableNativeType(_ value: QueryableNativeType) -> UUID? {
|
||||
|
||||
return self.init()
|
||||
return self.init(uuidString: value.lowercased)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
@inline(__always)
|
||||
public func cs_toQueryableNativeType() -> QueryableNativeType {
|
||||
|
||||
return self
|
||||
return self.uuidString.lowercased() as QueryableNativeType
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,10 +25,36 @@
|
||||
|
||||
import Foundation
|
||||
import CoreData
|
||||
import CoreGraphics
|
||||
|
||||
|
||||
// MARK: - ImportableAttributeType
|
||||
|
||||
/**
|
||||
Types supported by CoreStore as `NSManagedObject` attribute types.
|
||||
Supported default types:
|
||||
- Bool
|
||||
- CGFloat
|
||||
- Data
|
||||
- Date
|
||||
- Double
|
||||
- Float
|
||||
- Int
|
||||
- Int8
|
||||
- Int16
|
||||
- Int32
|
||||
- Int64
|
||||
- NSData
|
||||
- NSDate
|
||||
- NSDecimalNumber
|
||||
- NSNumber
|
||||
- NSString
|
||||
- NSURL
|
||||
- NSUUID
|
||||
- String
|
||||
- URL
|
||||
- UUID
|
||||
*/
|
||||
public protocol ImportableAttributeType: QueryableAttributeType {
|
||||
|
||||
associatedtype ImportableNativeType: QueryableNativeType
|
||||
@@ -44,110 +70,6 @@ public protocol ImportableAttributeType: QueryableAttributeType {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSNumber
|
||||
|
||||
extension NSNumber: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSNumber
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_emptyValue() -> Self {
|
||||
|
||||
return self.init()
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Self? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSString
|
||||
|
||||
extension NSString: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSString
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_emptyValue() -> Self {
|
||||
|
||||
return self.init()
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Self? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSDate
|
||||
|
||||
extension NSDate: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSDate
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_emptyValue() -> Self {
|
||||
|
||||
return self.init(timeIntervalSinceReferenceDate: 0)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Self? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSData
|
||||
|
||||
extension NSData: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSData
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_emptyValue() -> Self {
|
||||
|
||||
return self.init()
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Self? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Bool
|
||||
|
||||
extension Bool: ImportableAttributeType {
|
||||
@@ -174,6 +96,188 @@ extension Bool: ImportableAttributeType {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - CGFloat
|
||||
|
||||
extension CGFloat: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSNumber
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_emptyValue() -> CGFloat {
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> CGFloat? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Data
|
||||
|
||||
extension Data: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSData
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_emptyValue() -> Data {
|
||||
|
||||
return Data()
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Data? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Date
|
||||
|
||||
extension Date: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSDate
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_emptyValue() -> Date {
|
||||
|
||||
return Date(timeIntervalSinceReferenceDate: 0)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Date? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Double
|
||||
|
||||
extension Double: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSNumber
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_emptyValue() -> Double {
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Double? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Float
|
||||
|
||||
extension Float: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSNumber
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_emptyValue() -> Float {
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Float? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Int
|
||||
|
||||
extension Int: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSNumber
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_emptyValue() -> Int {
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Int? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Int8
|
||||
|
||||
extension Int8: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSNumber
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_emptyValue() -> Int8 {
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Int8? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Int16
|
||||
|
||||
extension Int16: ImportableAttributeType {
|
||||
@@ -252,25 +356,25 @@ extension Int64: ImportableAttributeType {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Double
|
||||
// MARK: - NSData
|
||||
|
||||
extension Double: ImportableAttributeType {
|
||||
extension NSData: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSNumber
|
||||
public typealias ImportableNativeType = NSData
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_emptyValue() -> Double {
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_emptyValue() -> Self {
|
||||
|
||||
return 0
|
||||
return self.init()
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Double? {
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Self? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
@@ -278,51 +382,132 @@ extension Double: ImportableAttributeType {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Float
|
||||
// MARK: - NSDate
|
||||
|
||||
extension Float: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSNumber
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_emptyValue() -> Float {
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Float? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Date
|
||||
|
||||
extension Date: ImportableAttributeType {
|
||||
extension NSDate: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSDate
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_emptyValue() -> Date {
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_emptyValue() -> Self {
|
||||
|
||||
return Date(timeIntervalSinceReferenceDate: 0)
|
||||
return self.init(timeIntervalSinceReferenceDate: 0)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Date? {
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Self? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSNumber
|
||||
|
||||
extension NSNumber: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSNumber
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_emptyValue() -> Self {
|
||||
|
||||
return self.init()
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Self? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSString
|
||||
|
||||
extension NSString: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSString
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_emptyValue() -> Self {
|
||||
|
||||
return self.init()
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Self? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSURL
|
||||
|
||||
extension NSURL: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSString
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_emptyValue() -> Self {
|
||||
|
||||
return self.init(string: "")!
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Self? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - NSUUID
|
||||
|
||||
extension NSUUID: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSString
|
||||
|
||||
public class func cs_emptyValue() -> Self {
|
||||
|
||||
enum Static {
|
||||
|
||||
static var zero = Array<UInt8>(repeating: 0, count: 16)
|
||||
}
|
||||
return self.init(uuidBytes: &Static.zero)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public class func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Self? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
@@ -356,20 +541,56 @@ extension String: ImportableAttributeType {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Data
|
||||
// MARK: - URL
|
||||
|
||||
extension Data: ImportableAttributeType {
|
||||
extension URL: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSData
|
||||
public typealias ImportableNativeType = NSString
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_emptyValue() -> Data {
|
||||
public static func cs_emptyValue() -> URL {
|
||||
|
||||
return Data()
|
||||
enum Static {
|
||||
|
||||
static let empty = URL(string: "")!
|
||||
}
|
||||
return Static.empty
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Data? {
|
||||
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> URL? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public func cs_toImportableNativeType() -> ImportableNativeType {
|
||||
|
||||
return self.cs_toQueryableNativeType()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - UUID
|
||||
|
||||
extension UUID: ImportableAttributeType {
|
||||
|
||||
public typealias ImportableNativeType = NSString
|
||||
|
||||
public static func cs_emptyValue() -> UUID {
|
||||
|
||||
enum Static {
|
||||
|
||||
static let empty: UUID = {
|
||||
|
||||
var zero = Array<UInt8>(repeating: 0, count: 16)
|
||||
return NSUUID(uuidBytes: &zero) as UUID
|
||||
}()
|
||||
}
|
||||
return Static.empty
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> UUID? {
|
||||
|
||||
return self.cs_fromQueryableNativeType(value)
|
||||
}
|
||||
|
||||
266
Sources/Setup/Dynamic Models/Prototype.swift
Normal file
266
Sources/Setup/Dynamic Models/Prototype.swift
Normal file
@@ -0,0 +1,266 @@
|
||||
//
|
||||
// Prototype.swift
|
||||
// CoreStore
|
||||
//
|
||||
// Created by John Estropia on 2017/04/03.
|
||||
// Copyright © 2017 John Rommel Estropia. All rights reserved.
|
||||
//
|
||||
|
||||
import CoreGraphics
|
||||
import Foundation
|
||||
|
||||
|
||||
public protocol ManagedObjectProtocol: class {}
|
||||
|
||||
public protocol EntityProtocol {
|
||||
|
||||
var entityDescription: NSEntityDescription { get }
|
||||
}
|
||||
|
||||
protocol AttributeProtocol: class {
|
||||
|
||||
static var attributeType: NSAttributeType { get }
|
||||
var keyPath: String { get }
|
||||
var defaultValue: Any? { get }
|
||||
var accessRawObject: () -> NSManagedObject { get set }
|
||||
}
|
||||
|
||||
open class CoreStoreManagedObject: ManagedObjectProtocol {
|
||||
|
||||
let rawObject: NSManagedObject?
|
||||
let isMeta: Bool
|
||||
|
||||
public required init(_ object: NSManagedObject?) {
|
||||
|
||||
self.isMeta = object == nil
|
||||
self.rawObject = object
|
||||
|
||||
guard let object = object else {
|
||||
|
||||
return
|
||||
}
|
||||
self.initializeAttributes(Mirror(reflecting: self), { [unowned object] in object })
|
||||
}
|
||||
|
||||
private func initializeAttributes(_ mirror: Mirror, _ accessRawObject: @escaping () -> NSManagedObject) {
|
||||
|
||||
_ = mirror.superclassMirror.flatMap({ self.initializeAttributes($0, accessRawObject) })
|
||||
for child in mirror.children {
|
||||
|
||||
guard case let property as AttributeProtocol = child.value else {
|
||||
|
||||
continue
|
||||
}
|
||||
property.accessRawObject = accessRawObject
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public struct Entity<O: CoreStoreManagedObject>: EntityProtocol {
|
||||
|
||||
public let entityDescription: NSEntityDescription
|
||||
|
||||
public init(_ entityName: String) {
|
||||
|
||||
let entityDescription = NSEntityDescription()
|
||||
entityDescription.name = entityName
|
||||
entityDescription.managedObjectClassName = NSStringFromClass(NSManagedObject.self)
|
||||
entityDescription.properties = type(of: self).initializeAttributes(Mirror(reflecting: O.meta))
|
||||
|
||||
self.entityDescription = entityDescription
|
||||
}
|
||||
|
||||
private static func initializeAttributes(_ mirror: Mirror) -> [NSAttributeDescription] {
|
||||
|
||||
var attributeDescriptions: [NSAttributeDescription] = []
|
||||
for child in mirror.children {
|
||||
|
||||
guard case let property as AttributeProtocol = child.value else {
|
||||
|
||||
continue
|
||||
}
|
||||
let attributeDescription = NSAttributeDescription()
|
||||
attributeDescription.name = property.keyPath
|
||||
attributeDescription.attributeType = type(of: property).attributeType
|
||||
attributeDescription.isOptional = false
|
||||
attributeDescription.defaultValue = property.defaultValue
|
||||
attributeDescriptions.append(attributeDescription)
|
||||
}
|
||||
if let baseEntityAttributeDescriptions = mirror.superclassMirror.flatMap(self.initializeAttributes) {
|
||||
|
||||
attributeDescriptions.append(contentsOf: baseEntityAttributeDescriptions)
|
||||
}
|
||||
return attributeDescriptions
|
||||
}
|
||||
}
|
||||
|
||||
public enum AttributeContainer<O: ManagedObjectProtocol> {
|
||||
|
||||
public final class Required<V: ImportableAttributeType>: AttributeProtocol {
|
||||
|
||||
static var attributeType: NSAttributeType { return V.cs_rawAttributeType }
|
||||
|
||||
let keyPath: String
|
||||
let defaultValue: Any?
|
||||
var accessRawObject: () -> NSManagedObject = { fatalError("\(O.self) property values should not be accessed") }
|
||||
|
||||
var value: V {
|
||||
|
||||
get {
|
||||
|
||||
let object = self.accessRawObject()
|
||||
let key = self.keyPath
|
||||
let value = object.value(forKey: key)! as! V.ImportableNativeType
|
||||
return V.cs_fromImportableNativeType(value)!
|
||||
}
|
||||
set {
|
||||
|
||||
let object = self.accessRawObject()
|
||||
let key = self.keyPath
|
||||
object.setValue(newValue.cs_toImportableNativeType(), forKey: key)
|
||||
}
|
||||
}
|
||||
|
||||
public init(_ keyPath: String, `default`: V = V.cs_emptyValue()) {
|
||||
|
||||
self.keyPath = keyPath
|
||||
self.defaultValue = `default`
|
||||
}
|
||||
}
|
||||
|
||||
public final class Optional<V: ImportableAttributeType>: AttributeProtocol {
|
||||
|
||||
static var attributeType: NSAttributeType { return V.cs_rawAttributeType }
|
||||
|
||||
let keyPath: String
|
||||
let defaultValue: Any?
|
||||
var accessRawObject: () -> NSManagedObject = { fatalError("\(O.self) property values should not be accessed") }
|
||||
|
||||
var value: V? {
|
||||
|
||||
get {
|
||||
|
||||
let object = self.accessRawObject()
|
||||
let key = self.keyPath
|
||||
guard let value = object.value(forKey: key) as! V.ImportableNativeType? else {
|
||||
|
||||
return nil
|
||||
}
|
||||
return V.cs_fromImportableNativeType(value)
|
||||
}
|
||||
set {
|
||||
|
||||
let object = self.accessRawObject()
|
||||
let key = self.keyPath
|
||||
object.setValue(newValue?.cs_toImportableNativeType(), forKey: key)
|
||||
}
|
||||
}
|
||||
|
||||
public init(_ keyPath: String, `default`: V? = nil) {
|
||||
|
||||
self.keyPath = keyPath
|
||||
self.defaultValue = `default`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public extension ManagedObjectProtocol where Self: CoreStoreManagedObject {
|
||||
|
||||
public typealias Attribute = AttributeContainer<Self>
|
||||
|
||||
public static var meta: Self {
|
||||
|
||||
return self.init(nil)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func keyPath<O: CoreStoreManagedObject, V: ImportableAttributeType>(_ attribute: (Self) -> AttributeContainer<O>.Required<V>) -> String {
|
||||
|
||||
return attribute(self.meta).keyPath
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
public static func keyPath<O: CoreStoreManagedObject, V: ImportableAttributeType>(_ attribute: (Self) -> AttributeContainer<O>.Optional<V>) -> String {
|
||||
|
||||
return attribute(self.meta).keyPath
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//: ### Convenience Operators
|
||||
|
||||
infix operator .= : AssignmentPrecedence
|
||||
public func .= <O: ManagedObjectProtocol, V: ImportableAttributeType>(_ attribute: AttributeContainer<O>.Required<V>, _ value: V) {
|
||||
|
||||
attribute.value = value
|
||||
}
|
||||
public func .= <O: ManagedObjectProtocol, V: ImportableAttributeType>(_ attribute: AttributeContainer<O>.Optional<V>, _ value: V?) {
|
||||
|
||||
attribute.value = value
|
||||
}
|
||||
|
||||
postfix operator *
|
||||
public postfix func * <O: ManagedObjectProtocol, V: ImportableAttributeType>(_ attribute: AttributeContainer<O>.Required<V>) -> V {
|
||||
|
||||
return attribute.value
|
||||
}
|
||||
public postfix func * <O: ManagedObjectProtocol, V: ImportableAttributeType>(_ attribute: AttributeContainer<O>.Optional<V>) -> V? {
|
||||
|
||||
return attribute.value
|
||||
}
|
||||
|
||||
public extension AttributeContainer.Required where V: CVarArg {
|
||||
|
||||
public static func == (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
|
||||
|
||||
return NSPredicate(format: "%K == %@", argumentArray: [attribute.keyPath, value])
|
||||
}
|
||||
public static func < (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
|
||||
|
||||
return NSPredicate(format: "%K < %@", argumentArray: [attribute.keyPath, value])
|
||||
}
|
||||
public static func > (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
|
||||
|
||||
return NSPredicate(format: "%K > %@", argumentArray: [attribute.keyPath, value])
|
||||
}
|
||||
public static func <= (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
|
||||
|
||||
return NSPredicate(format: "%K <= %@", argumentArray: [attribute.keyPath, value])
|
||||
}
|
||||
public static func >= (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
|
||||
|
||||
return NSPredicate(format: "%K >= %@", argumentArray: [attribute.keyPath, value])
|
||||
}
|
||||
public static func != (_ attribute: AttributeContainer<O>.Required<V>, _ value: V) -> NSPredicate {
|
||||
|
||||
return NSPredicate(format: "%K != %@", argumentArray: [attribute.keyPath, value])
|
||||
}
|
||||
}
|
||||
public extension AttributeContainer.Optional where V: CVarArg {
|
||||
|
||||
public static func == (_ attribute: AttributeContainer<O>.Optional<V>, _ value: V?) -> NSPredicate {
|
||||
|
||||
if let value = value {
|
||||
|
||||
return NSPredicate(format: "%K == %@", argumentArray: [attribute.keyPath, value])
|
||||
}
|
||||
else {
|
||||
|
||||
return NSPredicate(format: "%K == nil", attribute.keyPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protocol ModelVersionProtocol: class {
|
||||
|
||||
static var version: String { get }
|
||||
static var entities: [EntityProtocol] { get }
|
||||
}
|
||||
|
||||
extension ModelVersionProtocol {
|
||||
|
||||
static func entity<O: CoreStoreManagedObject>(for type: O.Type) -> Entity<O> {
|
||||
|
||||
return self.entities.first(where: { $0 is Entity<O> })! as! Entity<O>
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user