mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-01-16 05:56:50 +01:00
renamed Field.Computed to Field.Virtual to distinguish from Field.Derived
This commit is contained in:
@@ -114,7 +114,8 @@ extension NSManagedObject: DynamicObject {
|
||||
|
||||
public class func cs_fromRaw(object: NSManagedObject) -> Self {
|
||||
|
||||
return unsafeDowncast(object, to: self)
|
||||
// unsafeDowncast fails debug assertion starting Swift 5.2
|
||||
return _unsafeUncheckedDowncast(object, to: self)
|
||||
}
|
||||
|
||||
public static func cs_matches(object: NSManagedObject) -> Bool {
|
||||
|
||||
@@ -70,7 +70,7 @@ extension DynamicSchema {
|
||||
let containerType: String
|
||||
if attribute.isTransient || attribute.attributeType == .undefinedAttributeType {
|
||||
|
||||
containerType = "Field.Computed"
|
||||
containerType = "Field.Virtual"
|
||||
}
|
||||
else if attribute.attributeType == .transformableAttributeType {
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ extension FieldContainer {
|
||||
@Field.Stored("species")
|
||||
var species = ""
|
||||
|
||||
@Field.Computed("pluralName", customGetter: Animal.pluralName(_:))
|
||||
@Field.Virtual("pluralName", customGetter: Animal.pluralName(_:))
|
||||
var pluralName: String = ""
|
||||
|
||||
@Field.PlistCoded("color")
|
||||
@@ -62,7 +62,7 @@ extension FieldContainer {
|
||||
@Field.Stored("name")
|
||||
var name: String = ""
|
||||
|
||||
@Field.Computed("displayName", customGetter: Person.getName(_:))
|
||||
@Field.Virtual("displayName", customGetter: Person.getName(_:))
|
||||
var displayName: String = ""
|
||||
|
||||
private static func getName(_ partialObject: PartialObject<Person>) -> String {
|
||||
@@ -366,7 +366,7 @@ extension FieldContainer.Coded where V: FieldOptionalType {
|
||||
@Field.Stored("name")
|
||||
var name: String = ""
|
||||
|
||||
@Field.Computed("displayName", customGetter: Person.getName(_:))
|
||||
@Field.Virtual("displayName", customGetter: Person.getName(_:))
|
||||
var displayName: String = ""
|
||||
|
||||
private static func getName(_ partialObject: PartialObject<Person>) -> String {
|
||||
@@ -454,7 +454,7 @@ extension FieldContainer.Coded where V: DefaultNSSecureCodable {
|
||||
@Field.Stored("name")
|
||||
var name: String = ""
|
||||
|
||||
@Field.Computed("displayName", customGetter: Person.getName(_:))
|
||||
@Field.Virtual("displayName", customGetter: Person.getName(_:))
|
||||
var displayName: String = ""
|
||||
|
||||
private static func getName(_ partialObject: PartialObject<Person>) -> String {
|
||||
|
||||
@@ -40,7 +40,7 @@ extension FieldContainer {
|
||||
@Field.Stored("species")
|
||||
var species = ""
|
||||
|
||||
@Field.Computed("pluralName", customGetter: Animal.pluralName(_:))
|
||||
@Field.Virtual("pluralName", customGetter: Animal.pluralName(_:))
|
||||
var pluralName: String = ""
|
||||
|
||||
@Field.PlistCoded("color")
|
||||
@@ -62,7 +62,7 @@ extension FieldContainer {
|
||||
@Field.Stored("name")
|
||||
var name: String = ""
|
||||
|
||||
@Field.Computed("displayName", customGetter: Person.getName(_:))
|
||||
@Field.Virtual("displayName", customGetter: Person.getName(_:))
|
||||
var displayName: String = ""
|
||||
|
||||
private static func getName(_ partialObject: PartialObject<Person>) -> String {
|
||||
@@ -320,7 +320,7 @@ extension FieldContainer.Stored where V: FieldOptionalType {
|
||||
@Field.Stored("name")
|
||||
var name: String = ""
|
||||
|
||||
@Field.Computed("displayName", customGetter: Person.getName(_:))
|
||||
@Field.Virtual("displayName", customGetter: Person.getName(_:))
|
||||
var displayName: String = ""
|
||||
|
||||
private static func getName(_ partialObject: PartialObject<Person>) -> String {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// Field.Computed.swift
|
||||
// Field.Virtual.swift
|
||||
// CoreStore
|
||||
//
|
||||
// Copyright © 2020 John Rommel Estropia
|
||||
@@ -31,7 +31,7 @@ import Foundation
|
||||
|
||||
extension FieldContainer {
|
||||
|
||||
// MARK: - Computed
|
||||
// MARK: - Virtual
|
||||
|
||||
/**
|
||||
The containing type for computed property values. Any type that conforms to `FieldStorableType` are supported.
|
||||
@@ -40,17 +40,17 @@ extension FieldContainer {
|
||||
@Field.Stored("species")
|
||||
var species = ""
|
||||
|
||||
@Field.Computed("pluralName", customGetter: Animal.pluralName(_:))
|
||||
@Field.Virtual("pluralName", customGetter: Animal.pluralName(_:))
|
||||
var pluralName: String = ""
|
||||
|
||||
@Field.PlistCoded("color")
|
||||
var color: UIColor?
|
||||
}
|
||||
```
|
||||
- Important: `Field` properties are required to be used as `@propertyWrapper`s. Any other declaration not using the `@Field.Computed(...) var` syntax will be ignored.
|
||||
- Important: `Field` properties are required to be used as `@propertyWrapper`s. Any other declaration not using the `@Field.Virtual(...) var` syntax will be ignored.
|
||||
*/
|
||||
@propertyWrapper
|
||||
public struct Computed<V>: AttributeKeyPathStringConvertible, FieldAttributeProtocol {
|
||||
public struct Virtual<V>: AttributeKeyPathStringConvertible, FieldAttributeProtocol {
|
||||
|
||||
/**
|
||||
Initializes the metadata for the property.
|
||||
@@ -62,7 +62,7 @@ extension FieldContainer {
|
||||
@Field.Stored("name")
|
||||
var name: String = ""
|
||||
|
||||
@Field.Computed("displayName", customGetter: Person.getName(_:))
|
||||
@Field.Virtual("displayName", customGetter: Person.getName(_:))
|
||||
var displayName: String = ""
|
||||
|
||||
private static func getName(_ partialObject: PartialObject<Person>) -> String {
|
||||
@@ -85,7 +85,7 @@ extension FieldContainer {
|
||||
*/
|
||||
public init(
|
||||
_ keyPath: KeyPathString,
|
||||
customGetter: ((_ partialObject: PartialObject<O>) -> V)? = nil,
|
||||
customGetter: @escaping (_ partialObject: PartialObject<O>) -> V,
|
||||
customSetter: ((_ partialObject: PartialObject<O>, _ newValue: V) -> Void)? = nil,
|
||||
affectedByKeyPaths: @autoclosure @escaping () -> Set<KeyPathString> = []) {
|
||||
|
||||
@@ -299,3 +299,22 @@ extension FieldContainer {
|
||||
private let customSetter: ((_ partialObject: PartialObject<O>, _ newValue: V) -> Void)?
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension FieldContainer.Virtual where V: FieldOptionalType {
|
||||
|
||||
public init(
|
||||
_ keyPath: KeyPathString,
|
||||
customGetter: ((_ partialObject: PartialObject<O>) -> V)? = nil,
|
||||
customSetter: ((_ partialObject: PartialObject<O>, _ newValue: V) -> Void)? = nil,
|
||||
affectedByKeyPaths: @autoclosure @escaping () -> Set<KeyPathString> = []) {
|
||||
|
||||
self.init(
|
||||
keyPath: keyPath,
|
||||
isOptional: false,
|
||||
customGetter: customGetter,
|
||||
customSetter: customSetter,
|
||||
affectedByKeyPaths: affectedByKeyPaths
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -385,7 +385,7 @@ extension ObjectPublisher where O: CoreStoreObject {
|
||||
/**
|
||||
Returns the value for the property identified by a given key.
|
||||
*/
|
||||
public subscript<OBase, V>(dynamicMember member: KeyPath<O, FieldContainer<OBase>.Computed<V>>) -> V? {
|
||||
public subscript<OBase, V>(dynamicMember member: KeyPath<O, FieldContainer<OBase>.Virtual<V>>) -> V? {
|
||||
|
||||
guard
|
||||
let object = self.object,
|
||||
@@ -394,7 +394,7 @@ extension ObjectPublisher where O: CoreStoreObject {
|
||||
|
||||
return nil
|
||||
}
|
||||
return FieldContainer<OBase>.Computed<V>.read(field: object[keyPath: member], for: rawObject) as! V?
|
||||
return FieldContainer<OBase>.Virtual<V>.read(field: object[keyPath: member], for: rawObject) as! V?
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -195,7 +195,7 @@ extension ObjectSnapshot where O: CoreStoreObject {
|
||||
/**
|
||||
Returns the value for the property identified by a given key.
|
||||
*/
|
||||
public subscript<OBase, V>(dynamicMember member: KeyPath<O, FieldContainer<OBase>.Computed<V>>) -> V {
|
||||
public subscript<OBase, V>(dynamicMember member: KeyPath<O, FieldContainer<OBase>.Virtual<V>>) -> V {
|
||||
|
||||
get {
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ public struct PartialObject<O: CoreStoreObject> {
|
||||
/**
|
||||
Returns the value for the property identified by a given key.
|
||||
*/
|
||||
public func value<V>(for property: (O) -> FieldContainer<O>.Computed<V>) -> V {
|
||||
public func value<V>(for property: (O) -> FieldContainer<O>.Virtual<V>) -> V {
|
||||
|
||||
switch self.rawObject.value(forKey: property(O.meta).keyPath) {
|
||||
|
||||
@@ -89,7 +89,7 @@ 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 receiver’s private storage.
|
||||
*/
|
||||
public func primitiveValue<V>(for property: (O) -> FieldContainer<O>.Computed<V>) -> V? {
|
||||
public func primitiveValue<V>(for property: (O) -> FieldContainer<O>.Virtual<V>) -> V? {
|
||||
|
||||
switch self.rawObject.primitiveValue(forKey: property(O.meta).keyPath) {
|
||||
|
||||
@@ -145,7 +145,7 @@ public struct PartialObject<O: CoreStoreObject> {
|
||||
|
||||
Sets in the receiver’s private internal storage the value of the property specified by key to value.
|
||||
*/
|
||||
public func setPrimitiveValue<V>(_ value: V, for property: (O) -> FieldContainer<O>.Computed<V>) {
|
||||
public func setPrimitiveValue<V>(_ value: V, for property: (O) -> FieldContainer<O>.Virtual<V>) {
|
||||
|
||||
self.rawObject.setPrimitiveValue(
|
||||
value,
|
||||
|
||||
Reference in New Issue
Block a user