Last changes for backwards compatability

This commit is contained in:
Ryan Coyne 2017-11-24 11:55:33 -05:00
parent 2c249319af
commit 42dd9f0ed7
2 changed files with 22 additions and 42 deletions

View File

@ -20,7 +20,7 @@ public protocol CCXMirroring {
open class CCXMirror: CCXMirroring {
// The superclass count will include CCXMirror, StORM, & PostgresStORM by the time we get to the subclasses we need to process.
private var superclassCount = 1
private var superclassCount = 0
public func didInitializeSuperclass() {
self.superclassCount += 1
}
@ -30,13 +30,12 @@ open class CCXMirror: CCXMirroring {
let mir = Mirror(reflecting: self)
mirrors.append(mir)
var currentContext : Mirror?
for _ in 1...self.superclassCount {
for _ in 0...self.superclassCount {
if currentContext.isNil {
currentContext = mir.superclassMirror
} else {
currentContext = currentContext?.superclassMirror
}
if currentContext.isNotNil {
// we only want to bring in the variables from the superclasses that are beyond PostgresStORM:
mirrors.append(currentContext!)

View File

@ -58,23 +58,18 @@ open class StORM : CCXMirror {
/// If any object property begins with an underscore, or with "internal_" it is omitted from the response.
open func asData(_ offset : Int = 0) -> [(String, Any)] {
var c = [(String, Any)]()
var children = self.allChildren()
// 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 {
children.remove(label: StORM.primaryKeyLabel!)
}
for child in children {
if !child.label!.hasPrefix("internal_") && !child.label!.hasPrefix("_") {
if child.value is [String:Any] {
c.append((child.label!, modifyValue(try! (child.value as! [String:Any]).jsonEncodedString(), forKey: child.label!)))
} else if child.value is [String] {
c.append((child.label!, modifyValue((child.value as! [String]).joined(separator: ","), forKey: child.label!)))
var count = 0
for case let (label?, value) in self.allChildren() {
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)))
} else if value is [String] {
c.append((label, modifyValue((value as! [String]).joined(separator: ","), forKey: label)))
} else {
c.append((child.label!, modifyValue(child.value, forKey: child.label!)))
c.append((label, modifyValue(value, forKey: label)))
}
}
count += 1
}
return c
}
@ -83,23 +78,18 @@ open class StORM : CCXMirror {
/// If any object property begins with an underscore, or with "internal_" it is omitted from the response.
open func asDataDict(_ offset : Int = 0) -> [String: Any] {
var c = [String: Any]()
var children = self.allChildren()
// 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 {
children.remove(label: StORM.primaryKeyLabel!)
}
for child in children {
if !child.label!.hasPrefix("internal_") && !child.label!.hasPrefix("_") {
if child.value is [String:Any] {
c[child.label!] = modifyValue(try! (child.value as! [String:Any]).jsonEncodedString(), forKey: child.label!)
} else if child.value is [String] {
c[child.label!] = modifyValue((child.value as! [String]).joined(separator: ","), forKey: child.label!)
var count = 0
for case let (label?, value) in self.allChildren() {
if count >= offset && !label.hasPrefix("internal_") && !label.hasPrefix("_") {
if value is [String:Any] {
c[label] = modifyValue(try! (value as! [String:Any]).jsonEncodedString(), forKey: label)
} else if value is [String] {
c[label] = modifyValue((value as! [String]).joined(separator: ","), forKey: label)
} else {
c[child.label!] = modifyValue(child.value, forKey: child.label!)
c[label] = modifyValue(value, forKey: label)
}
}
count += 1
}
return c
}
@ -107,18 +97,9 @@ open class StORM : CCXMirror {
/// Returns a tuple of name & value of the object's key
/// The key is determined to be it's first property, which is assumed to be the object key.
public func firstAsKey() -> (String, Any) {
let primaryKey = StORM.primaryKeyLabel
if primaryKey.isNotNil {
for case let (label, value) in self.allChildren() {
if label == primaryKey {
return (label!, modifyValue(value, forKey: label!))
}
}
} else {
for case let (label, value) in self.allChildren() {
return (label!, modifyValue(value, forKey: label!))
}
}
return ("id", "unknown")
}