move type safety goodness

This commit is contained in:
John Estropia
2017-02-21 19:17:06 +09:00
parent 9ff1c9d545
commit 3224fcf71d
9 changed files with 732 additions and 608 deletions

View File

@@ -0,0 +1,317 @@
//
// CoreStoreImportableAttributeType.swift
// CoreStore
//
// Copyright © 2017 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
import CoreData
// MARK: - CoreStoreImportableAttributeType
public protocol CoreStoreImportableAttributeType: CoreStoreQueryableAttributeType {
associatedtype ImportableNativeType: QueryableNativeType
@inline(__always)
static func cs_emptyValue() -> Self
@inline(__always)
static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Self?
}
// MARK: - NSNumber
extension NSNumber: CoreStoreImportableAttributeType {
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? {
func forceCast<T: NSNumber>(_ value: Any) -> T? {
return value as? T
}
return forceCast(value)
}
}
// MARK: - NSString
extension NSString: CoreStoreImportableAttributeType {
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? {
func forceCast<T: NSString>(_ value: Any) -> T? {
return value as? T
}
return forceCast(value)
}
}
// MARK: - NSDate
extension NSDate: CoreStoreImportableAttributeType {
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? {
func forceCast<T: NSDate>(_ value: Any) -> T? {
return value as? T
}
return forceCast(value)
}
}
// MARK: - NSData
extension NSData: CoreStoreImportableAttributeType {
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? {
func forceCast<T: NSData>(_ value: Any) -> T? {
return value as? T
}
return forceCast(value)
}
}
// MARK: - Bool
extension Bool: CoreStoreImportableAttributeType {
public typealias ImportableNativeType = NSNumber
@inline(__always)
public static func cs_emptyValue() -> Bool {
return false
}
@inline(__always)
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Bool? {
return value.boolValue
}
}
// MARK: - Int16
extension Int16: CoreStoreImportableAttributeType {
public typealias ImportableNativeType = NSNumber
@inline(__always)
public static func cs_emptyValue() -> Int16 {
return 0
}
@inline(__always)
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Int16? {
return value.int16Value
}
}
// MARK: - Int32
extension Int32: CoreStoreImportableAttributeType {
public typealias ImportableNativeType = NSNumber
@inline(__always)
public static func cs_emptyValue() -> Int32 {
return 0
}
@inline(__always)
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Int32? {
return value.int32Value
}
}
// MARK: - Int64
extension Int64: CoreStoreImportableAttributeType {
public typealias ImportableNativeType = NSNumber
@inline(__always)
public static func cs_emptyValue() -> Int64 {
return 0
}
@inline(__always)
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> Int64? {
return value.int64Value
}
}
// MARK: - Double
extension Double: CoreStoreImportableAttributeType {
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 value.doubleValue
}
}
// MARK: - Float
extension Float: CoreStoreImportableAttributeType {
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 value.floatValue
}
}
// MARK: - Date
extension Date: CoreStoreImportableAttributeType {
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 value as Date
}
}
// MARK: - String
extension String: CoreStoreImportableAttributeType {
public typealias ImportableNativeType = NSString
@inline(__always)
public static func cs_emptyValue() -> String {
return ""
}
@inline(__always)
public static func cs_fromImportableNativeType(_ value: ImportableNativeType) -> String? {
return value as String
}
}
// MARK: - Data
extension Data: CoreStoreImportableAttributeType {
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 value as Data
}
}

View File

@@ -1,288 +0,0 @@
//
// CoreStoreUniqueIDAttributeType.swift
// CoreStore
//
// Copyright © 2017 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
import CoreData
// MARK: - CoreStoreUniqueIDAttributeType
public protocol CoreStoreUniqueIDAttributeType: CoreStoreQueryingAttributeType {
associatedtype NativeTypeForUniqueID: CoreDataNativeType
static func cs_fromUniqueIDNativeType(_ value: NativeTypeForUniqueID) -> Self?
func cs_toUniqueIDNativeType() -> NativeTypeForUniqueID
}
// MARK: - NSNumber
extension NSNumber: CoreStoreUniqueIDAttributeType {
public typealias NativeTypeForUniqueID = NSNumber
public class func cs_fromUniqueIDNativeType(_ value: NativeTypeForUniqueID) -> Self? {
func forceCast<T: NSNumber>(_ value: Any) -> T? {
return value as? T
}
return forceCast(value)
}
public func cs_toUniqueIDNativeType() -> NativeTypeForUniqueID {
return self
}
}
// MARK: - NSString
extension NSString: CoreStoreUniqueIDAttributeType {
public typealias NativeTypeForUniqueID = NSString
public class func cs_fromUniqueIDNativeType(_ value: NativeTypeForUniqueID) -> Self? {
func forceCast<T: NSString>(_ value: Any) -> T? {
return value as? T
}
return forceCast(value)
}
public func cs_toUniqueIDNativeType() -> NativeTypeForUniqueID {
return self
}
}
// MARK: - NSDate
extension NSDate: CoreStoreUniqueIDAttributeType {
public typealias NativeTypeForUniqueID = NSDate
public class func cs_fromUniqueIDNativeType(_ value: NativeTypeForUniqueID) -> Self? {
func forceCast<T: NSDate>(_ value: Any) -> T? {
return value as? T
}
return forceCast(value)
}
public func cs_toUniqueIDNativeType() -> NativeTypeForUniqueID {
return self
}
}
// MARK: - NSData
extension NSData: CoreStoreUniqueIDAttributeType {
public typealias NativeTypeForUniqueID = NSData
public class func cs_fromUniqueIDNativeType(_ value: NativeTypeForUniqueID) -> Self? {
func forceCast<T: NSData>(_ value: Any) -> T? {
return value as? T
}
return forceCast(value)
}
public func cs_toUniqueIDNativeType() -> NativeTypeForUniqueID {
return self
}
}
// MARK: - Bool
extension Bool: CoreStoreUniqueIDAttributeType {
public typealias NativeTypeForUniqueID = NSNumber
public static func cs_fromUniqueIDNativeType(_ value: NativeTypeForUniqueID) -> Bool? {
return value.boolValue
}
public func cs_toUniqueIDNativeType() -> NativeTypeForUniqueID {
return self as NSNumber
}
}
// MARK: - Int16
extension Int16: CoreStoreUniqueIDAttributeType {
public typealias NativeTypeForUniqueID = NSNumber
public static func cs_fromUniqueIDNativeType(_ value: NativeTypeForUniqueID) -> Int16? {
return value.int16Value
}
public func cs_toUniqueIDNativeType() -> NativeTypeForUniqueID {
return self as NSNumber
}
}
// MARK: - Int32
extension Int32: CoreStoreUniqueIDAttributeType {
public typealias NativeTypeForUniqueID = NSNumber
public static func cs_fromUniqueIDNativeType(_ value: NativeTypeForUniqueID) -> Int32? {
return value.int32Value
}
public func cs_toUniqueIDNativeType() -> NativeTypeForUniqueID {
return self as NSNumber
}
}
// MARK: - Int64
extension Int64: CoreStoreUniqueIDAttributeType {
public typealias NativeTypeForUniqueID = NSNumber
public static func cs_fromUniqueIDNativeType(_ value: NativeTypeForUniqueID) -> Int64? {
return value.int64Value
}
public func cs_toUniqueIDNativeType() -> NativeTypeForUniqueID {
return self as NSNumber
}
}
// MARK: - Double
extension Double: CoreStoreUniqueIDAttributeType {
public typealias NativeTypeForUniqueID = NSNumber
public static func cs_fromUniqueIDNativeType(_ value: NativeTypeForUniqueID) -> Double? {
return value.doubleValue
}
public func cs_toUniqueIDNativeType() -> NativeTypeForUniqueID {
return self as NSNumber
}
}
// MARK: - Float
extension Float: CoreStoreUniqueIDAttributeType {
public typealias NativeTypeForUniqueID = NSNumber
public static func cs_fromUniqueIDNativeType(_ value: NativeTypeForUniqueID) -> Float? {
return value.floatValue
}
public func cs_toUniqueIDNativeType() -> NativeTypeForUniqueID {
return self as NSNumber
}
}
// MARK: - Date
extension Date: CoreStoreUniqueIDAttributeType {
public typealias NativeTypeForUniqueID = NSDate
public static func cs_fromUniqueIDNativeType(_ value: NativeTypeForUniqueID) -> Date? {
return value as Date
}
public func cs_toUniqueIDNativeType() -> NativeTypeForUniqueID {
return self as NSDate
}
}
// MARK: - String
extension String: CoreStoreUniqueIDAttributeType {
public typealias NativeTypeForUniqueID = NSString
public static func cs_fromUniqueIDNativeType(_ value: NativeTypeForUniqueID) -> String? {
return value as String
}
public func cs_toUniqueIDNativeType() -> NativeTypeForUniqueID {
return self as NSString
}
}
// MARK: - Data
extension Data: CoreStoreUniqueIDAttributeType {
public typealias NativeTypeForUniqueID = NSData
public static func cs_fromUniqueIDNativeType(_ value: NativeTypeForUniqueID) -> Data? {
return value as Data
}
public func cs_toUniqueIDNativeType() -> NativeTypeForUniqueID {
return self as NSData
}
}

View File

@@ -59,7 +59,7 @@ public protocol ImportableUniqueObject: ImportableObject {
/**
The data type for the entity's unique ID attribute
*/
associatedtype UniqueIDType: CoreStoreUniqueIDAttributeType
associatedtype UniqueIDType: CoreStoreImportableAttributeType
/**
The keyPath to the entity's unique ID attribute
@@ -192,14 +192,14 @@ public extension ImportableUniqueObject where Self: NSManagedObject {
get {
return UniqueIDType.cs_fromUniqueIDNativeType(
self.value(forKey: type(of: self).uniqueIDKeyPath) as! UniqueIDType.NativeTypeForUniqueID
return UniqueIDType.cs_fromImportableNativeType(
self.value(forKey: type(of: self).uniqueIDKeyPath) as! UniqueIDType.ImportableNativeType
)!
}
set {
self.setValue(
newValue.cs_toUniqueIDNativeType(),
newValue.cs_toQueryableNativeType(),
forKey: type(of: self).uniqueIDKeyPath
)
}