Working on last update to storm for specifying in an override function, the primary key for a table/model

This commit is contained in:
Ryan Coyne 2017-11-24 12:48:45 -05:00
parent deda7d1478
commit 1d40673a97
3 changed files with 19 additions and 18 deletions

View File

@ -82,7 +82,7 @@ extension Bool {
}
//MARK: - Mirror Array:
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] = []
for mirror in self {
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:
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
return child.label.isNotNil && child.label == StORM.primaryKeyLabel!
return child.label == keyLabel
}) {
allChild.remove(label: keyLabel)
let idChild = allChild[index]

View File

@ -15,7 +15,8 @@
public protocol CCXMirroring {
func didInitializeSuperclass()
func allChildren(includingNilValues : Bool) -> [Mirror.Child]
func allChildren(includingNilValues : Bool, primaryKey: String?) -> [Mirror.Child]
func primaryKey() -> String?
}
open class CCXMirror: CCXMirroring {
@ -24,6 +25,9 @@ open class CCXMirror: CCXMirroring {
public func didInitializeSuperclass() {
self.superclassCount += 1
}
open func primaryKey() -> String? {
return nil
}
/// This function goes through all the superclass mirrors. This is dependent on the CCXMirroring protocol.
private func superclassMirrors() -> [Mirror] {
var mirrors : [Mirror] = []
@ -44,9 +48,9 @@ open class CCXMirror: CCXMirroring {
return mirrors
}
/// 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:
let children = self.superclassMirrors().allChildren(includingNilValues: includingNilValues)
let children = self.superclassMirrors().allChildren(includingNilValues: includingNilValues, primaryKey: primaryKey)
return children
}
}

View File

@ -34,19 +34,16 @@ open class StORM : CCXMirror {
public func cols(_ offset : Int = 0) -> [(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 StORM.primaryKeyLabel.isNil && offset == 1 {
children.remove(at: children.startIndex)
} else if offset == 1 && StORM.primaryKeyLabel.isNotNil {
for child in self.allChildren(primaryKey: self.primaryKey()) {
guard let key = child.label else {
continue
}
for child in children {
if child.label.isNotNil && !child.label!.hasPrefix("internal_") && !child.label!.hasPrefix("_") {
if !key.hasPrefix("internal_") && !key.hasPrefix("_") {
c.append((child.label!, type(of:child.value)))
}
count += 1
}
return c
@ -59,7 +56,7 @@ open class StORM : CCXMirror {
open func asData(_ offset : Int = 0) -> [(String, Any)] {
var c = [(String, Any)]()
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 value is [String:Any] {
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] {
var c = [String: Any]()
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 value is [String:Any] {
c[label] = modifyValue(try! (value as! [String:Any]).jsonEncodedString(), forKey: label)