Working on last update to storm for specifying in an override function, the primary key for a table/model
This commit is contained in:
parent
deda7d1478
commit
1d40673a97
|
@ -82,7 +82,7 @@ extension Bool {
|
||||||
}
|
}
|
||||||
//MARK: - Mirror Array:
|
//MARK: - Mirror Array:
|
||||||
extension Array where Iterator.Element == Mirror {
|
extension Array where Iterator.Element == Mirror {
|
||||||
func allChildren(includingNilValues: Bool=false) -> [Mirror.Child] {
|
func allChildren(includingNilValues: Bool = false, primaryKey: String? = nil) -> [Mirror.Child] {
|
||||||
var allChild : [Mirror.Child] = []
|
var allChild : [Mirror.Child] = []
|
||||||
for mirror in self {
|
for mirror in self {
|
||||||
mirror.children.forEach({ (child) in
|
mirror.children.forEach({ (child) in
|
||||||
|
@ -99,9 +99,9 @@ extension Array where Iterator.Element == Mirror {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
// Lets make sure if the primaryKey is set, it is the first object:
|
// Lets make sure if the primaryKey is set, it is the first object:
|
||||||
if let keyLabel = StORM.primaryKeyLabel, allChild.first?.label != keyLabel {
|
if let keyLabel = primaryKey, allChild.first?.label != keyLabel {
|
||||||
if let index = allChild.index(where: { (child) -> Bool in
|
if let index = allChild.index(where: { (child) -> Bool in
|
||||||
return child.label.isNotNil && child.label == StORM.primaryKeyLabel!
|
return child.label == keyLabel
|
||||||
}) {
|
}) {
|
||||||
allChild.remove(label: keyLabel)
|
allChild.remove(label: keyLabel)
|
||||||
let idChild = allChild[index]
|
let idChild = allChild[index]
|
||||||
|
|
|
@ -15,7 +15,8 @@
|
||||||
|
|
||||||
public protocol CCXMirroring {
|
public protocol CCXMirroring {
|
||||||
func didInitializeSuperclass()
|
func didInitializeSuperclass()
|
||||||
func allChildren(includingNilValues : Bool) -> [Mirror.Child]
|
func allChildren(includingNilValues : Bool, primaryKey: String?) -> [Mirror.Child]
|
||||||
|
func primaryKey() -> String?
|
||||||
}
|
}
|
||||||
|
|
||||||
open class CCXMirror: CCXMirroring {
|
open class CCXMirror: CCXMirroring {
|
||||||
|
@ -24,6 +25,9 @@ open class CCXMirror: CCXMirroring {
|
||||||
public func didInitializeSuperclass() {
|
public func didInitializeSuperclass() {
|
||||||
self.superclassCount += 1
|
self.superclassCount += 1
|
||||||
}
|
}
|
||||||
|
open func primaryKey() -> String? {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
/// This function goes through all the superclass mirrors. This is dependent on the CCXMirroring protocol.
|
/// This function goes through all the superclass mirrors. This is dependent on the CCXMirroring protocol.
|
||||||
private func superclassMirrors() -> [Mirror] {
|
private func superclassMirrors() -> [Mirror] {
|
||||||
var mirrors : [Mirror] = []
|
var mirrors : [Mirror] = []
|
||||||
|
@ -44,9 +48,9 @@ open class CCXMirror: CCXMirroring {
|
||||||
return mirrors
|
return mirrors
|
||||||
}
|
}
|
||||||
/// This returns all the children, even all the superclass mirrored children. Use allChildren().asData() to return an array of key/values.
|
/// This returns all the children, even all the superclass mirrored children. Use allChildren().asData() to return an array of key/values.
|
||||||
public func allChildren(includingNilValues : Bool = false) -> [Mirror.Child] {
|
public func allChildren(includingNilValues : Bool = false, primaryKey: String? = nil) -> [Mirror.Child] {
|
||||||
// Remove out the superclass count which is private:
|
// Remove out the superclass count which is private:
|
||||||
let children = self.superclassMirrors().allChildren(includingNilValues: includingNilValues)
|
let children = self.superclassMirrors().allChildren(includingNilValues: includingNilValues, primaryKey: primaryKey)
|
||||||
return children
|
return children
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,19 +34,16 @@ open class StORM : CCXMirror {
|
||||||
public func cols(_ offset : Int = 0) -> [(String, Any)] {
|
public func cols(_ offset : Int = 0) -> [(String, Any)] {
|
||||||
|
|
||||||
var c = [(String, Any)]()
|
var c = [(String, Any)]()
|
||||||
var children = self.allChildren()
|
var count = 0
|
||||||
// If the StORM primary key is nil, we should assume the first will be the primary key.
|
// If the StORM primary key is nil, we should assume the first will be the primary key.
|
||||||
if StORM.primaryKeyLabel.isNil && offset == 1 {
|
for child in self.allChildren(primaryKey: self.primaryKey()) {
|
||||||
children.remove(at: children.startIndex)
|
guard let key = child.label else {
|
||||||
} else if offset == 1 && StORM.primaryKeyLabel.isNotNil {
|
continue
|
||||||
|
|
||||||
}
|
}
|
||||||
for child in children {
|
if !key.hasPrefix("internal_") && !key.hasPrefix("_") {
|
||||||
|
|
||||||
if child.label.isNotNil && !child.label!.hasPrefix("internal_") && !child.label!.hasPrefix("_") {
|
|
||||||
c.append((child.label!, type(of:child.value)))
|
c.append((child.label!, type(of:child.value)))
|
||||||
}
|
}
|
||||||
|
count += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return c
|
return c
|
||||||
|
@ -59,7 +56,7 @@ open class StORM : CCXMirror {
|
||||||
open func asData(_ offset : Int = 0) -> [(String, Any)] {
|
open func asData(_ offset : Int = 0) -> [(String, Any)] {
|
||||||
var c = [(String, Any)]()
|
var c = [(String, Any)]()
|
||||||
var count = 0
|
var count = 0
|
||||||
for case let (label?, value) in self.allChildren() {
|
for case let (label?, value) in self.allChildren(primaryKey: self.primaryKey()) {
|
||||||
if count >= offset && !label.hasPrefix("internal_") && !label.hasPrefix("_") {
|
if count >= offset && !label.hasPrefix("internal_") && !label.hasPrefix("_") {
|
||||||
if value is [String:Any] {
|
if value is [String:Any] {
|
||||||
c.append((label, modifyValue(try! (value as! [String:Any]).jsonEncodedString(), forKey: label)))
|
c.append((label, modifyValue(try! (value as! [String:Any]).jsonEncodedString(), forKey: label)))
|
||||||
|
@ -79,7 +76,7 @@ open class StORM : CCXMirror {
|
||||||
open func asDataDict(_ offset : Int = 0) -> [String: Any] {
|
open func asDataDict(_ offset : Int = 0) -> [String: Any] {
|
||||||
var c = [String: Any]()
|
var c = [String: Any]()
|
||||||
var count = 0
|
var count = 0
|
||||||
for case let (label?, value) in self.allChildren() {
|
for case let (label?, value) in self.allChildren(primaryKey: self.primaryKey()) {
|
||||||
if count >= offset && !label.hasPrefix("internal_") && !label.hasPrefix("_") {
|
if count >= offset && !label.hasPrefix("internal_") && !label.hasPrefix("_") {
|
||||||
if value is [String:Any] {
|
if value is [String:Any] {
|
||||||
c[label] = modifyValue(try! (value as! [String:Any]).jsonEncodedString(), forKey: label)
|
c[label] = modifyValue(try! (value as! [String:Any]).jsonEncodedString(), forKey: label)
|
||||||
|
|
Loading…
Reference in New Issue