Field.Coded implementations for transformable attributes

This commit is contained in:
John Estropia
2020-01-18 16:22:06 +09:00
parent 43f61359da
commit bcc2d9def3
22 changed files with 1505 additions and 101 deletions

View File

@@ -65,8 +65,10 @@ public struct PartialObject<O: CoreStoreObject> {
case let value as V:
return value
default:
return nil as Any? as! V // filter NSNull
case nil,
is NSNull,
_? /* any other unrelated type */ :
return nil as Any? as! V // There should be no uninitialized state at this point
}
}
@@ -87,15 +89,41 @@ public struct PartialObject<O: CoreStoreObject> {
This method does not invoke the access notification methods (`willAccessValue(forKey:)` and `didAccessValue(forKey:)`). This method is used primarily by subclasses that implement custom accessor methods that need direct access to the receivers private storage.
*/
public func primitiveValue<V>(for property: (O) -> FieldContainer<O>.Computed<V>) -> V {
public func primitiveValue<V>(for property: (O) -> FieldContainer<O>.Computed<V>) -> V? {
switch self.rawObject.primitiveValue(forKey: property(O.meta).keyPath) {
case let value as V:
return value
default:
return nil as Any? as! V // filter NSNull
case nil,
is NSNull,
_? /* any other unrelated type */ :
return nil
}
}
/**
Returns the value for the specified property from the managed objects private internal storage.
This method does not invoke the access notification methods (`willAccessValue(forKey:)` and `didAccessValue(forKey:)`). This method is used primarily by subclasses that implement custom accessor methods that need direct access to the receivers private storage.
*/
public func primitiveValue<V>(for property: (O) -> FieldContainer<O>.Coded<V>) -> V? {
let keyPath = property(O.meta).keyPath
switch self.rawObject.primitiveValue(forKey: keyPath) {
case let valueBox as Internals.AnyFieldCoder.TransformableDefaultValueCodingBox:
rawObject.setPrimitiveValue(valueBox.value, forKey: keyPath)
return valueBox.value as? V
case let value as V:
return value
case nil,
is NSNull,
_? /* any other unrelated type */ :
return nil
}
}