Merge pull request #1 from thislooksfun/patch-2

Add modifyValue(_:,forKey:)
This commit is contained in:
Jonathan Guthrie 2017-08-10 12:54:04 -04:00 committed by GitHub
commit dac972a3ba
1 changed files with 10 additions and 8 deletions

View File

@ -46,6 +46,8 @@ open class StORM {
return c return c
} }
open func modifyValue(_ v: Any, forKey k: String) -> Any { return v }
/// Returns a [(String,Any)] object representation of the current object. /// Returns a [(String,Any)] object representation of the current object.
/// If any object property begins with an underscore, or with "internal_" it is omitted from the response. /// If any object property begins with an underscore, or with "internal_" it is omitted from the response.
public func asData(_ offset: Int = 0) -> [(String, Any)] { public func asData(_ offset: Int = 0) -> [(String, Any)] {
@ -55,11 +57,11 @@ open class StORM {
for case let (label?, value) in mirror.children { for case let (label?, value) in mirror.children {
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, try! (value as! [String:Any]).jsonEncodedString())) c.append((label, modifyValue(try! (value as! [String:Any]).jsonEncodedString(), forKey: label)))
} else if value is [String] { } else if value is [String] {
c.append((label, (value as! [String]).joined(separator: ","))) c.append((label, modifyValue((value as! [String]).joined(separator: ","), forKey: label)))
} else { } else {
c.append((label, value)) c.append((label, modifyValue(value, forKey: label)))
} }
} }
count += 1 count += 1
@ -76,11 +78,11 @@ open class StORM {
for case let (label?, value) in mirror.children { for case let (label?, value) in mirror.children {
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] = try! (value as! [String:Any]).jsonEncodedString() c[label] = modifyValue(try! (value as! [String:Any]).jsonEncodedString(), forKey: label)
} else if value is [String] { } else if value is [String] {
c[label] = (value as! [String]).joined(separator: ",") c[label] = modifyValue((value as! [String]).joined(separator: ","), forKey: label)
} else { } else {
c[label] = value c[label] = modifyValue(value, forKey: label)
} }
} }
count += 1 count += 1
@ -93,7 +95,7 @@ open class StORM {
public func firstAsKey() -> (String, Any) { public func firstAsKey() -> (String, Any) {
let mirror = Mirror(reflecting: self) let mirror = Mirror(reflecting: self)
for case let (label?, value) in mirror.children { for case let (label?, value) in mirror.children {
return (label, value) return (label, modifyValue(value, forKey: label))
} }
return ("id", "unknown") return ("id", "unknown")
} }