mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-05-03 14:14:19 +02:00
support transformable values
This commit is contained in:
@@ -62,13 +62,27 @@ public extension DynamicSchema {
|
||||
for (attributeName, attribute) in attributesByName {
|
||||
|
||||
let containerType: String
|
||||
if attribute.isOptional {
|
||||
if attribute.attributeType == .transformableAttributeType {
|
||||
|
||||
containerType = "Value.Optional"
|
||||
if attribute.isOptional {
|
||||
|
||||
containerType = "Transformable.Optional"
|
||||
}
|
||||
else {
|
||||
|
||||
containerType = "Transformable.Required"
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
||||
containerType = "Value.Required"
|
||||
if attribute.isOptional {
|
||||
|
||||
containerType = "Value.Optional"
|
||||
}
|
||||
else {
|
||||
|
||||
containerType = "Value.Required"
|
||||
}
|
||||
}
|
||||
let valueType: Any.Type
|
||||
var defaultString = ""
|
||||
@@ -151,11 +165,29 @@ public extension DynamicSchema {
|
||||
}
|
||||
defaultString = ", default: Data(bytes: [\(bytes.joined(separator: ", "))])"
|
||||
}
|
||||
case .transformableAttributeType:
|
||||
if let attributeValueClassName = attribute.attributeValueClassName {
|
||||
|
||||
valueType = NSClassFromString(attributeValueClassName)!
|
||||
}
|
||||
else {
|
||||
|
||||
valueType = (NSCoding & NSCopying).self
|
||||
}
|
||||
if let defaultValue = attribute.defaultValue {
|
||||
|
||||
defaultString = ", default: /* \"\(defaultValue)\" */"
|
||||
}
|
||||
else if !attribute.isOptional {
|
||||
|
||||
defaultString = ", default: /* required */"
|
||||
}
|
||||
default:
|
||||
fatalError("Unsupported attribute type: \(attribute.attributeType)")
|
||||
fatalError("Unsupported attribute type: \(attribute.attributeType.rawValue)")
|
||||
}
|
||||
let indexedString = attribute.isIndexed ? ", isIndexed: true" : ""
|
||||
let transientString = attribute.isTransient ? ", isTransient: true" : ""
|
||||
output.append(" let \(attributeName) = \(containerType)<\(String(describing: valueType))>(\"\(attributeName)\"\(defaultString)\(transientString))\n")
|
||||
output.append(" let \(attributeName) = \(containerType)<\(String(describing: valueType))>(\"\(attributeName)\"\(indexedString)\(defaultString)\(transientString))\n")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -78,29 +78,29 @@ public extension NSManagedObject {
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func getValue(forKvcKey kvcKey: KeyPath) -> CoreDataNativeType? {
|
||||
public func getValue(forKvcKey kvcKey: KeyPath) -> Any? {
|
||||
|
||||
self.willAccessValue(forKey: kvcKey)
|
||||
defer {
|
||||
|
||||
self.didAccessValue(forKey: kvcKey)
|
||||
}
|
||||
return self.primitiveValue(forKey: kvcKey) as! CoreDataNativeType?
|
||||
return self.primitiveValue(forKey: kvcKey)
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func getValue<T>(forKvcKey kvcKey: KeyPath, didGetValue: (CoreDataNativeType?) throws -> T) rethrows -> T {
|
||||
public func getValue<T>(forKvcKey kvcKey: KeyPath, didGetValue: (Any?) throws -> T) rethrows -> T {
|
||||
|
||||
self.willAccessValue(forKey: kvcKey)
|
||||
defer {
|
||||
|
||||
self.didAccessValue(forKey: kvcKey)
|
||||
}
|
||||
return try didGetValue(self.primitiveValue(forKey: kvcKey) as! CoreDataNativeType?)
|
||||
return try didGetValue(self.primitiveValue(forKey: kvcKey))
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func getValue<T>(forKvcKey kvcKey: KeyPath, willGetValue: () throws -> Void, didGetValue: (CoreDataNativeType?) throws -> T) rethrows -> T {
|
||||
public func getValue<T>(forKvcKey kvcKey: KeyPath, willGetValue: () throws -> Void, didGetValue: (Any?) throws -> T) rethrows -> T {
|
||||
|
||||
self.willAccessValue(forKey: kvcKey)
|
||||
defer {
|
||||
@@ -108,11 +108,11 @@ public extension NSManagedObject {
|
||||
self.didAccessValue(forKey: kvcKey)
|
||||
}
|
||||
try willGetValue()
|
||||
return try didGetValue(self.primitiveValue(forKey: kvcKey) as! CoreDataNativeType?)
|
||||
return try didGetValue(self.primitiveValue(forKey: kvcKey))
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func setValue(_ value: CoreDataNativeType?, forKvcKey KVCKey: KeyPath) {
|
||||
public func setValue(_ value: Any?, forKvcKey KVCKey: KeyPath) {
|
||||
|
||||
self.willChangeValue(forKey: KVCKey)
|
||||
defer {
|
||||
@@ -123,7 +123,7 @@ public extension NSManagedObject {
|
||||
}
|
||||
|
||||
@nonobjc @inline(__always)
|
||||
public func setValue<T>(_ value: T, forKvcKey KVCKey: KeyPath, willSetValue: (T) throws -> CoreDataNativeType?) rethrows {
|
||||
public func setValue<T>(_ value: T, forKvcKey KVCKey: KeyPath, willSetValue: (T) throws -> Any?) rethrows {
|
||||
|
||||
self.willChangeValue(forKey: KVCKey)
|
||||
defer {
|
||||
|
||||
Reference in New Issue
Block a user