From 7fbe9728731f36bbe48e82493b448be385f906be Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Wed, 22 Nov 2017 20:59:21 -0500 Subject: [PATCH 01/52] Working on subclassing the postgres-storm object --- Sources/StORM/CCXExtensions.swift | 118 ++++++++++++++++++++++++++++++ Sources/StORM/CCXMirroring.swift | 64 ++++++++++++++++ Sources/StORM/StORM.swift | 69 ++++++++--------- 3 files changed, 217 insertions(+), 34 deletions(-) create mode 100644 Sources/StORM/CCXExtensions.swift create mode 100644 Sources/StORM/CCXMirroring.swift diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift new file mode 100644 index 0000000..1be0019 --- /dev/null +++ b/Sources/StORM/CCXExtensions.swift @@ -0,0 +1,118 @@ +// +// CCXExtensions.swift +// StORM +// +// Created by Ryan Coyne on 11/22/17. +// + +import Foundation + +//MARK: - Optionals +extension Optional { + /// This returns whether or not the optional is equal to nil. + var isNil : Bool { + return self == nil + } + /// This returns whether or not the optional is not equal to nil. + var isNotNil : Bool { + return self != nil + } + /// This returns the optional unwrapped into a boolean value. + var boolValue : Bool? { + if self.isNil { + return nil + } + switch self { + case is String, is String?: + return Bool(self as! String) + case is Int, is Int?: + return Bool(self as! Int) + default: + return nil + } + } + /// This returns the optionally wrapped object as a string value. + var stringValue : String? { + return self as? String + } + /// This returns the optionally wrapped object as a dictionary value. + var dicValue : [String:Any]! { + return self as? [String:Any] ?? [:] + } + /// This returns the optionally wrapped object as an array of dictionaries...value. + var arrayDicValue : [[String:Any]]! { + return self as? [[String:Any]] ?? [[:]] + } + /// This returns the optionally wrapped object as an integer value. + var intValue : Int? { + return self as? Int + } + /// This returns the optionally wrapped object as a double value. + var doubleValue : Double? { + return self as? Double + } + /// This returns the optionally wrapped object as a float value. + var floatValue : Float? { + return self as? Float + } + /// This returns the optionally wrapped object as a URL value. + var urlValue : URL? { + if self.isNil { + return nil + } + switch self { + case is String, is Optional: + return URL(string: self.stringValue!) + case is URL, is URL?: + return self as? URL + default: + return nil + } + } +} +//MARK: - Boolean +extension Bool { + init(_ integerValue : Int) { + if integerValue == 0 { + self.init(false) + } else { + self.init(true) + } + } +} +//MARK: - Mirror Array: +extension Array where Iterator.Element == Mirror { + var allChildren : [String:Any] { + var allChild : [String:Any] = [:] + for mirror in self { + mirror.children.forEach({ (child) in + if let label = child.label { + allChild[label] = child.value + } + }) + } + return allChild + } +// var allChildrenData : [(String,Any)] { +// var allChild : [(String,Any)] = [] +// for mirror in self { +// mirror.children.forEach({ (child) in +// if child.label.isNotNil { +// allChild.append((child.label!, child.value)) +// } +// }) +// } +// return allChild +// } +} + +extension Dictionary where Key == String, Value == Any { + public func asData() -> [(String, Any)] { + var data : [(String,Any)] = [] + // Remove out the superclass count which is private: + for row in self { + data.append(row) + } + return data + } +} diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/CCXMirroring.swift new file mode 100644 index 0000000..cfe9516 --- /dev/null +++ b/Sources/StORM/CCXMirroring.swift @@ -0,0 +1,64 @@ +// +// CCXMirroring.swift +// +// Created by Ryan Coyne on 11/22/17. +// Copyright © 2017 ClearCodeX, Inc. All rights reserved. +// + +// +// CCXMirror.swift +// +// +// Created by Ryan Coyne on 11/17/17. +// Copyright © 2017 ClearCodeX, Inc. All rights reserved. +// + +public protocol CCXMirroring { + func didInitializeSuperclass() + func allChildren() -> [String:Any] +} + +open class CCXMirror: CCXMirroring { + private var superclassCount = 0 + public func didInitializeSuperclass() { + self.superclassCount += 1 + } + /// This function goes through all the superclass mirrors. This is dependent on the CCXMirroring protocol. + private func superclassMirrors() -> [Mirror] { + var mirrors : [Mirror] = [] + let mir = Mirror(reflecting: self) + // Make sure we aren't adding in the CCXMirror or StORM mirrors: + switch mir.subjectType { + case is CCXMirror.Type, is StORM.Type: + break + default: + mirrors.append(mir) + } + var currentContext : Mirror? + for _ in 0...self.superclassCount { + if currentContext.isNil { + currentContext = mir.superclassMirror + } else { + currentContext = currentContext?.superclassMirror + } + // Make sure we aren't adding in the CCXMirror or StORM mirrors: + switch currentContext?.subjectType { + case is CCXMirror.Type?, is StORM.Type?: + break + default: + if currentContext.isNotNil { + mirrors.append(currentContext!) + } + } + } + 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() -> [String:Any] { + // Remove out the superclass count which is private: + var children = self.superclassMirrors().allChildren + children.removeValue(forKey: "superclassCount") + return children + } +} + diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 6903f37..53b9b99 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -11,11 +11,10 @@ /// When true, certain methods will generate a debug message under certain conditions. public var StORMdebug = false - /// Base StORM superclass from which all Database-Connector StORM classes inherit. /// Provides base functionality and rules. -open class StORM { - +open class StORM : CCXMirror { + /// Results container of type StORMResultSet. open var results = StORMResultSet() @@ -26,23 +25,26 @@ open class StORM { open var errorMsg = "" /// Base empty init function. - public init() {} + public override init() {} + + /// primary key label (not assuming the first child is the id). + public static var primaryKeyLabel : String = "id" /// Provides structure introspection to client methods. public func cols(_ offset: Int = 0) -> [(String, Any)] { + var c = [(String, Any)]() var count = 0 - let mirror = Mirror(reflecting: self) - for child in mirror.children { - guard let key = child.label else { - continue - } - if count >= offset && !key.hasPrefix("internal_") && !key.hasPrefix("_") { - c.append((key, type(of:child.value))) - //c[key] = type(of:child.value) - } - count += 1 - } + + for child in self.allChildren() { + + if count >= offset && !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { + c.append((child.key, type(of:child.value))) + + } + count += 1 + } + return c } @@ -53,15 +55,14 @@ open class StORM { open func asData(_ offset: Int = 0) -> [(String, Any)] { var c = [(String, Any)]() var count = 0 - let mirror = Mirror(reflecting: self) - for case let (label?, value) in mirror.children { - 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))) + for child in self.allChildren() { + if count >= offset && !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { + if child.value is [String:Any] { + c.append((child.key, modifyValue(try! (child.value as! [String:Any]).jsonEncodedString(), forKey: child.key))) + } else if child.value is [String] { + c.append((child.key, modifyValue((child.value as! [String]).joined(separator: ","), forKey: child.key))) } else { - c.append((label, modifyValue(value, forKey: label))) + c.append((child.key, modifyValue(child.value, forKey: child.key))) } } count += 1 @@ -74,15 +75,14 @@ open class StORM { open func asDataDict(_ offset: Int = 0) -> [String: Any] { var c = [String: Any]() var count = 0 - let mirror = Mirror(reflecting: self) - for case let (label?, value) in mirror.children { - 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) + for child in self.allChildren() { + if count >= offset && !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { + if child.value is [String:Any] { + c[child.key] = modifyValue(try! (child.value as! [String:Any]).jsonEncodedString(), forKey: child.key) + } else if child.value is [String] { + c[child.key] = modifyValue((child.value as! [String]).joined(separator: ","), forKey: child.key) } else { - c[label] = modifyValue(value, forKey: label) + c[child.key] = modifyValue(child.value, forKey: child.key) } } count += 1 @@ -93,9 +93,10 @@ open class StORM { /// 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 mirror = Mirror(reflecting: self) - for case let (label?, value) in mirror.children { - return (label, modifyValue(value, forKey: label)) + for case let (label, value) in self.allChildren() { + if label == StORM.primaryKeyLabel { + return (label, modifyValue(value, forKey: label)) + } } return ("id", "unknown") } From 6b468a6f221d2e56276d5c97dc2b7b21ceee1dd6 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Thu, 23 Nov 2017 10:48:11 -0500 Subject: [PATCH 02/52] Making the mirroring support optionals & if the value is nil, it does not add it in the dictionary or asData array on the save --- Sources/StORM/CCXExtensions.swift | 15 ++------------- Sources/StORM/CCXMirroring.swift | 3 ++- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift index 1be0019..0f7f2f1 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/CCXExtensions.swift @@ -86,30 +86,19 @@ extension Array where Iterator.Element == Mirror { var allChild : [String:Any] = [:] for mirror in self { mirror.children.forEach({ (child) in - if let label = child.label { + // Make sure our child has a label & the string describing the value is not nil. (Making optionals supported) + if let label = child.label, String(describing: child.value) != "nil" { allChild[label] = child.value } }) } return allChild } -// var allChildrenData : [(String,Any)] { -// var allChild : [(String,Any)] = [] -// for mirror in self { -// mirror.children.forEach({ (child) in -// if child.label.isNotNil { -// allChild.append((child.label!, child.value)) -// } -// }) -// } -// return allChild -// } } extension Dictionary where Key == String, Value == Any { public func asData() -> [(String, Any)] { var data : [(String,Any)] = [] - // Remove out the superclass count which is private: for row in self { data.append(row) } diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/CCXMirroring.swift index cfe9516..b8e604d 100644 --- a/Sources/StORM/CCXMirroring.swift +++ b/Sources/StORM/CCXMirroring.swift @@ -19,7 +19,8 @@ public protocol CCXMirroring { } open class CCXMirror: CCXMirroring { - private var superclassCount = 0 + // The superclass count will include CCXMirror, StORM, & Postgres-StORM by the time we get to the subclasses we need to process. + private var superclassCount = 3 public func didInitializeSuperclass() { self.superclassCount += 1 } From 11d32ea7b60c1694a4d970cbd6bfb0cd345efb66 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Thu, 23 Nov 2017 12:19:38 -0500 Subject: [PATCH 03/52] Updating last thing for setting up tables to Postgres-StORM --- Sources/StORM/CCXExtensions.swift | 8 +++++--- Sources/StORM/CCXMirroring.swift | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift index 0f7f2f1..347e28e 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/CCXExtensions.swift @@ -82,13 +82,15 @@ extension Bool { } //MARK: - Mirror Array: extension Array where Iterator.Element == Mirror { - var allChildren : [String:Any] { + func allChildren(includingNilValues: Bool=false) -> [String:Any] { var allChild : [String:Any] = [:] for mirror in self { mirror.children.forEach({ (child) in // Make sure our child has a label & the string describing the value is not nil. (Making optionals supported) - if let label = child.label, String(describing: child.value) != "nil" { - allChild[label] = child.value + if !includingNilValues { + if let label = child.label, String(describing: child.value) != "nil" { + allChild[label] = child.value + } } }) } diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/CCXMirroring.swift index b8e604d..e9d4884 100644 --- a/Sources/StORM/CCXMirroring.swift +++ b/Sources/StORM/CCXMirroring.swift @@ -15,7 +15,7 @@ public protocol CCXMirroring { func didInitializeSuperclass() - func allChildren() -> [String:Any] + func allChildren(includingNilValues : Bool) -> [String:Any] } open class CCXMirror: CCXMirroring { @@ -55,9 +55,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() -> [String:Any] { + public func allChildren(includingNilValues : Bool = false) -> [String:Any] { // Remove out the superclass count which is private: - var children = self.superclassMirrors().allChildren + var children = self.superclassMirrors().allChildren(includingNilValues: includingNilValues) children.removeValue(forKey: "superclassCount") return children } From 8616a818c185a6d66989221d7a74d4f930bff141 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Thu, 23 Nov 2017 12:56:11 -0500 Subject: [PATCH 04/52] Fixing an issue with the mirroring --- Sources/StORM/CCXMirroring.swift | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/CCXMirroring.swift index e9d4884..7c23d08 100644 --- a/Sources/StORM/CCXMirroring.swift +++ b/Sources/StORM/CCXMirroring.swift @@ -43,13 +43,8 @@ open class CCXMirror: CCXMirroring { currentContext = currentContext?.superclassMirror } // Make sure we aren't adding in the CCXMirror or StORM mirrors: - switch currentContext?.subjectType { - case is CCXMirror.Type?, is StORM.Type?: - break - default: - if currentContext.isNotNil { - mirrors.append(currentContext!) - } + if currentContext.isNotNil { + mirrors.append(currentContext!) } } return mirrors From 65550d3a3a1cf4ae2f1917c727c1633a80e981f4 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Thu, 23 Nov 2017 13:04:12 -0500 Subject: [PATCH 05/52] Adding in some log prints --- Sources/StORM/CCXMirroring.swift | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/CCXMirroring.swift index 7c23d08..1b11487 100644 --- a/Sources/StORM/CCXMirroring.swift +++ b/Sources/StORM/CCXMirroring.swift @@ -22,19 +22,16 @@ open class CCXMirror: CCXMirroring { // The superclass count will include CCXMirror, StORM, & Postgres-StORM by the time we get to the subclasses we need to process. private var superclassCount = 3 public func didInitializeSuperclass() { + print(self.superclassCount) self.superclassCount += 1 } /// This function goes through all the superclass mirrors. This is dependent on the CCXMirroring protocol. private func superclassMirrors() -> [Mirror] { var mirrors : [Mirror] = [] let mir = Mirror(reflecting: self) - // Make sure we aren't adding in the CCXMirror or StORM mirrors: - switch mir.subjectType { - case is CCXMirror.Type, is StORM.Type: - break - default: - mirrors.append(mir) - } + + mirrors.append(mir) + print(mir.children.count) var currentContext : Mirror? for _ in 0...self.superclassCount { if currentContext.isNil { @@ -42,11 +39,12 @@ open class CCXMirror: CCXMirroring { } else { currentContext = currentContext?.superclassMirror } - // Make sure we aren't adding in the CCXMirror or StORM mirrors: if currentContext.isNotNil { mirrors.append(currentContext!) } + print(currentContext?.children.count) } + print(mirrors.count) return mirrors } /// This returns all the children, even all the superclass mirrored children. Use allChildren().asData() to return an array of key/values. From 6cb4d45209de6708c9f4b4437d6c0611f6e72616 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Thu, 23 Nov 2017 13:20:36 -0500 Subject: [PATCH 06/52] Fixing a dumb issue --- Sources/StORM/CCXExtensions.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift index 347e28e..71352ad 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/CCXExtensions.swift @@ -91,6 +91,10 @@ extension Array where Iterator.Element == Mirror { if let label = child.label, String(describing: child.value) != "nil" { allChild[label] = child.value } + } else { + if let label = child.label { + allChild[label] = child.value + } } }) } From 4c73b204695d9afc515e2e90850ab1285ff12185 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Thu, 23 Nov 2017 16:59:17 -0500 Subject: [PATCH 07/52] Fixing last issues with mirroring & getting the correct superclasses --- Sources/StORM/CCXMirroring.swift | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/CCXMirroring.swift index 1b11487..29d9765 100644 --- a/Sources/StORM/CCXMirroring.swift +++ b/Sources/StORM/CCXMirroring.swift @@ -29,22 +29,24 @@ open class CCXMirror: CCXMirroring { private func superclassMirrors() -> [Mirror] { var mirrors : [Mirror] = [] let mir = Mirror(reflecting: self) - mirrors.append(mir) - print(mir.children.count) var currentContext : Mirror? - for _ in 0...self.superclassCount { + for _ in 1...self.superclassCount { if currentContext.isNil { currentContext = mir.superclassMirror } else { currentContext = currentContext?.superclassMirror } if currentContext.isNotNil { - mirrors.append(currentContext!) + // we only want to bring in the variables from the superclasses that are beyond PostgresStORM: + switch String(describing: currentContext!.subjectType) { + case "CCXMirror", "StORM", "PostgresStORM": + break + default: + mirrors.append(currentContext!) + } } - print(currentContext?.children.count) } - print(mirrors.count) return mirrors } /// This returns all the children, even all the superclass mirrored children. Use allChildren().asData() to return an array of key/values. From ab826264da544d9dc060ca077baf71e89a3e65da Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Thu, 23 Nov 2017 17:02:47 -0500 Subject: [PATCH 08/52] Last few changes --- Sources/StORM/CCXMirroring.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/CCXMirroring.swift index 29d9765..fa6ab25 100644 --- a/Sources/StORM/CCXMirroring.swift +++ b/Sources/StORM/CCXMirroring.swift @@ -19,10 +19,9 @@ public protocol CCXMirroring { } open class CCXMirror: CCXMirroring { - // The superclass count will include CCXMirror, StORM, & Postgres-StORM by the time we get to the subclasses we need to process. + // The superclass count will include CCXMirror, StORM, & PostgresStORM by the time we get to the subclasses we need to process. private var superclassCount = 3 public func didInitializeSuperclass() { - print(self.superclassCount) self.superclassCount += 1 } /// This function goes through all the superclass mirrors. This is dependent on the CCXMirroring protocol. @@ -53,7 +52,6 @@ open class CCXMirror: CCXMirroring { public func allChildren(includingNilValues : Bool = false) -> [String:Any] { // Remove out the superclass count which is private: var children = self.superclassMirrors().allChildren(includingNilValues: includingNilValues) - children.removeValue(forKey: "superclassCount") return children } } From 1028767f5431f8db1f5c9c6017e13a150763db62 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 24 Nov 2017 09:00:49 -0500 Subject: [PATCH 09/52] Cleaning up the storm module for what I need in the postgresStorm module --- Sources/StORM/CCXMirroring.swift | 5 +- Sources/StORM/StORM.swift | 167 ++++++++++++++++--------------- 2 files changed, 87 insertions(+), 85 deletions(-) diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/CCXMirroring.swift index fa6ab25..dc36c78 100644 --- a/Sources/StORM/CCXMirroring.swift +++ b/Sources/StORM/CCXMirroring.swift @@ -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 = 3 + private var superclassCount = 0 public func didInitializeSuperclass() { self.superclassCount += 1 } @@ -51,8 +51,7 @@ open class CCXMirror: CCXMirroring { /// 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) -> [String:Any] { // Remove out the superclass count which is private: - var children = self.superclassMirrors().allChildren(includingNilValues: includingNilValues) + let children = self.superclassMirrors().allChildren(includingNilValues: includingNilValues) return children } } - diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 53b9b99..8c20822 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -15,27 +15,27 @@ public var StORMdebug = false /// Provides base functionality and rules. open class StORM : CCXMirror { - /// Results container of type StORMResultSet. - open var results = StORMResultSet() - - /// connection error status of type StORMError. - open var error = StORMError() - - /// Contain last error message as string. - open var errorMsg = "" - - /// Base empty init function. + /// Results container of type StORMResultSet. + open var results = StORMResultSet() + + /// connection error status of type StORMError. + open var error = StORMError() + + /// Contain last error message as string. + open var errorMsg = "" + + /// Base empty init function. public override init() {} /// primary key label (not assuming the first child is the id). public static var primaryKeyLabel : String = "id" - - /// Provides structure introspection to client methods. - public func cols(_ offset: Int = 0) -> [(String, Any)] { - - var c = [(String, Any)]() - var count = 0 + /// Provides structure introspection to client methods. + public func cols(_ offset: Int = 0) -> [(String, Any)] { + + var c = [(String, Any)]() + var count = 0 + for child in self.allChildren() { if count >= offset && !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { @@ -44,66 +44,70 @@ open class StORM : CCXMirror { } count += 1 } - - return c - } - - open func modifyValue(_ v: Any, forKey k: String) -> Any { return v } - - /// 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. - open func asData(_ offset: Int = 0) -> [(String, Any)] { - var c = [(String, Any)]() - var count = 0 - for child in self.allChildren() { - if count >= offset && !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { - if child.value is [String:Any] { - c.append((child.key, modifyValue(try! (child.value as! [String:Any]).jsonEncodedString(), forKey: child.key))) - } else if child.value is [String] { - c.append((child.key, modifyValue((child.value as! [String]).joined(separator: ","), forKey: child.key))) - } else { - c.append((child.key, modifyValue(child.value, forKey: child.key))) - } - } - count += 1 - } - return c - } - - /// 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. - open func asDataDict(_ offset: Int = 0) -> [String: Any] { - var c = [String: Any]() - var count = 0 - for child in self.allChildren() { - if count >= offset && !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { - if child.value is [String:Any] { - c[child.key] = modifyValue(try! (child.value as! [String:Any]).jsonEncodedString(), forKey: child.key) - } else if child.value is [String] { - c[child.key] = modifyValue((child.value as! [String]).joined(separator: ","), forKey: child.key) - } else { - c[child.key] = modifyValue(child.value, forKey: child.key) - } - } - count += 1 - } - return c - } - - /// 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) { - for case let (label, value) in self.allChildren() { + + return c + } + + open func modifyValue(_ v: Any, forKey k: String) -> Any { return v } + + /// 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. + open func asData(_ includePrimaryKey : Bool = false) -> [(String, Any)] { + var c = [(String, Any)]() + var children = self.allChildren() + if !includePrimaryKey { + children.removeValue(forKey: StORM.primaryKeyLabel) + } + for child in children { + if !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { + if child.value is [String:Any] { + c.append((child.key, modifyValue(try! (child.value as! [String:Any]).jsonEncodedString(), forKey: child.key))) + } else if child.value is [String] { + c.append((child.key, modifyValue((child.value as! [String]).joined(separator: ","), forKey: child.key))) + } else { + c.append((child.key, modifyValue(child.value, forKey: child.key))) + } + } + } + return c + } + + /// 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. + open func asDataDict(_ includePrimaryKey : Bool = false) -> [String: Any] { + var c = [String: Any]() + var children = self.allChildren() + if !includePrimaryKey { + children.removeValue(forKey: StORM.primaryKeyLabel) + } + for child in children { + if !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { + if child.value is [String:Any] { + c[child.key] = modifyValue(try! (child.value as! [String:Any]).jsonEncodedString(), forKey: child.key) + } else if child.value is [String] { + c[child.key] = modifyValue((child.value as! [String]).joined(separator: ","), forKey: child.key) + } else { + c[child.key] = modifyValue(child.value, forKey: child.key) + } + } + } + return c + } + + /// 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) { + for case let (label, value) in self.allChildren() { if label == StORM.primaryKeyLabel { return (label, modifyValue(value, forKey: label)) } - } - return ("id", "unknown") - } - - /// Returns a boolean that is true if the first property in the class contains a value. - public func keyIsEmpty() -> Bool { - let (_, val) = firstAsKey() + } + return ("id", "unknown") + } + + /// Returns a boolean that is true if the first property in the class contains a value. + public func keyIsEmpty() -> Bool { + let (_, val) = firstAsKey() // Grab the type of value: let type = type(of: val) @@ -123,13 +127,12 @@ open class StORM : CCXMirror { return false } - } - - /// The create method is designed to be overridden - /// If not set in the chile class it will return an error of the enum value .notImplemented - open func create() throws { - throw StORMError.notImplemented - } - + } + + /// The create method is designed to be overridden + /// If not set in the chile class it will return an error of the enum value .notImplemented + open func create() throws { + throw StORMError.notImplemented + } + } - From 4fda089074be000605e7bf0d0c041d69f58fc5ed Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 24 Nov 2017 09:03:58 -0500 Subject: [PATCH 10/52] Updating the cols function --- Sources/StORM/StORM.swift | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 8c20822..7d5f9eb 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -31,18 +31,20 @@ open class StORM : CCXMirror { public static var primaryKeyLabel : String = "id" /// Provides structure introspection to client methods. - public func cols(_ offset: Int = 0) -> [(String, Any)] { + public func cols(_ includePrimaryKey : Bool = false) -> [(String, Any)] { var c = [(String, Any)]() - var count = 0 - - for child in self.allChildren() { + var children = self.allChildren() + if !includePrimaryKey { + children.removeValue(forKey: StORM.primaryKeyLabel) + } + for child in children { - if count >= offset && !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { + if !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { c.append((child.key, type(of:child.value))) } - count += 1 + } return c From ad1f579cea80653c2c2b41b3d91cb5e80f1f27f2 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 24 Nov 2017 11:13:11 -0500 Subject: [PATCH 11/52] Making it so StORM has the backwards capability for this update --- Sources/StORM/CCXMirroring.swift | 10 +++----- Sources/StORM/StORM.swift | 40 ++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/CCXMirroring.swift index dc36c78..e305f7f 100644 --- a/Sources/StORM/CCXMirroring.swift +++ b/Sources/StORM/CCXMirroring.swift @@ -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 = 0 + private var superclassCount = 1 public func didInitializeSuperclass() { self.superclassCount += 1 } @@ -36,14 +36,10 @@ open class CCXMirror: CCXMirroring { } else { currentContext = currentContext?.superclassMirror } + if currentContext.isNotNil { // we only want to bring in the variables from the superclasses that are beyond PostgresStORM: - switch String(describing: currentContext!.subjectType) { - case "CCXMirror", "StORM", "PostgresStORM": - break - default: - mirrors.append(currentContext!) - } + mirrors.append(currentContext!) } } return mirrors diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 7d5f9eb..1ad9107 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -28,15 +28,18 @@ open class StORM : CCXMirror { public override init() {} /// primary key label (not assuming the first child is the id). - public static var primaryKeyLabel : String = "id" + public static var primaryKeyLabel : String? = nil /// Provides structure introspection to client methods. - public func cols(_ includePrimaryKey : Bool = false) -> [(String, Any)] { + public func cols(_ offset : Int = 0) -> [(String, Any)] { var c = [(String, Any)]() var children = self.allChildren() - if !includePrimaryKey { - children.removeValue(forKey: StORM.primaryKeyLabel) + // 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.removeValue(forKey: StORM.primaryKeyLabel!) } for child in children { @@ -54,11 +57,14 @@ open class StORM : CCXMirror { /// 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. - open func asData(_ includePrimaryKey : Bool = false) -> [(String, Any)] { + open func asData(_ offset : Int = 0) -> [(String, Any)] { var c = [(String, Any)]() var children = self.allChildren() - if !includePrimaryKey { - children.removeValue(forKey: StORM.primaryKeyLabel) + // 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.removeValue(forKey: StORM.primaryKeyLabel!) } for child in children { if !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { @@ -76,11 +82,14 @@ open class StORM : CCXMirror { /// 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. - open func asDataDict(_ includePrimaryKey : Bool = false) -> [String: Any] { + open func asDataDict(_ offset : Int = 0) -> [String: Any] { var c = [String: Any]() var children = self.allChildren() - if !includePrimaryKey { - children.removeValue(forKey: StORM.primaryKeyLabel) + // 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.removeValue(forKey: StORM.primaryKeyLabel!) } for child in children { if !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { @@ -99,8 +108,15 @@ 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) { - for case let (label, value) in self.allChildren() { - if label == StORM.primaryKeyLabel { + 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)) } } From 2c249319af1f3d4fc434aa69ee4ea0652dcb0ce2 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 24 Nov 2017 11:40:37 -0500 Subject: [PATCH 12/52] Working on backwards compatability for when we mirror --- Sources/StORM/CCXExtensions.swift | 22 ++++++++++++++++------ Sources/StORM/CCXMirroring.swift | 4 ++-- Sources/StORM/StORM.swift | 31 +++++++++++++++---------------- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift index 71352ad..17be7c9 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/CCXExtensions.swift @@ -82,18 +82,18 @@ extension Bool { } //MARK: - Mirror Array: extension Array where Iterator.Element == Mirror { - func allChildren(includingNilValues: Bool=false) -> [String:Any] { - var allChild : [String:Any] = [:] + func allChildren(includingNilValues: Bool=false) -> [Mirror.Child] { + var allChild : [Mirror.Child] = [] for mirror in self { mirror.children.forEach({ (child) in // Make sure our child has a label & the string describing the value is not nil. (Making optionals supported) if !includingNilValues { - if let label = child.label, String(describing: child.value) != "nil" { - allChild[label] = child.value + if child.label.isNotNil, String(describing: child.value) != "nil" { + allChild.append(child) } } else { - if let label = child.label { - allChild[label] = child.value + if child.label.isNotNil { + allChild.append(child) } } }) @@ -102,6 +102,16 @@ extension Array where Iterator.Element == Mirror { } } +extension Array where Iterator.Element == Mirror.Child { + mutating func remove(label : String) { + if let index = self.index(where: { (child) -> Bool in + return child.label.isNotNil && child.label == StORM.primaryKeyLabel + }) { + self.remove(at: index) + } + } +} + extension Dictionary where Key == String, Value == Any { public func asData() -> [(String, Any)] { var data : [(String,Any)] = [] diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/CCXMirroring.swift index e305f7f..807e54b 100644 --- a/Sources/StORM/CCXMirroring.swift +++ b/Sources/StORM/CCXMirroring.swift @@ -15,7 +15,7 @@ public protocol CCXMirroring { func didInitializeSuperclass() - func allChildren(includingNilValues : Bool) -> [String:Any] + func allChildren(includingNilValues : Bool) -> [Mirror.Child] } open class CCXMirror: CCXMirroring { @@ -45,7 +45,7 @@ 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) -> [String:Any] { + public func allChildren(includingNilValues : Bool = false) -> [Mirror.Child] { // Remove out the superclass count which is private: let children = self.superclassMirrors().allChildren(includingNilValues: includingNilValues) return children diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 1ad9107..48357fc 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -39,13 +39,12 @@ open class StORM : CCXMirror { if StORM.primaryKeyLabel.isNil && offset == 1 { children.remove(at: children.startIndex) } else if offset == 1 && StORM.primaryKeyLabel.isNotNil { - children.removeValue(forKey: StORM.primaryKeyLabel!) + } for child in children { - if !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { - c.append((child.key, type(of:child.value))) - + if child.label.isNotNil && !child.label!.hasPrefix("internal_") && !child.label!.hasPrefix("_") { + c.append((child.label!, type(of:child.value))) } } @@ -64,16 +63,16 @@ open class StORM : CCXMirror { if StORM.primaryKeyLabel.isNil && offset == 1 { children.remove(at: children.startIndex) } else if offset == 1 && StORM.primaryKeyLabel.isNotNil { - children.removeValue(forKey: StORM.primaryKeyLabel!) + children.remove(label: StORM.primaryKeyLabel!) } for child in children { - if !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { + if !child.label!.hasPrefix("internal_") && !child.label!.hasPrefix("_") { if child.value is [String:Any] { - c.append((child.key, modifyValue(try! (child.value as! [String:Any]).jsonEncodedString(), forKey: child.key))) + c.append((child.label!, modifyValue(try! (child.value as! [String:Any]).jsonEncodedString(), forKey: child.label!))) } else if child.value is [String] { - c.append((child.key, modifyValue((child.value as! [String]).joined(separator: ","), forKey: child.key))) + c.append((child.label!, modifyValue((child.value as! [String]).joined(separator: ","), forKey: child.label!))) } else { - c.append((child.key, modifyValue(child.value, forKey: child.key))) + c.append((child.label!, modifyValue(child.value, forKey: child.label!))) } } } @@ -89,16 +88,16 @@ open class StORM : CCXMirror { if StORM.primaryKeyLabel.isNil && offset == 1 { children.remove(at: children.startIndex) } else if offset == 1 && StORM.primaryKeyLabel.isNotNil { - children.removeValue(forKey: StORM.primaryKeyLabel!) + children.remove(label: StORM.primaryKeyLabel!) } for child in children { - if !child.key.hasPrefix("internal_") && !child.key.hasPrefix("_") { + if !child.label!.hasPrefix("internal_") && !child.label!.hasPrefix("_") { if child.value is [String:Any] { - c[child.key] = modifyValue(try! (child.value as! [String:Any]).jsonEncodedString(), forKey: child.key) + c[child.label!] = modifyValue(try! (child.value as! [String:Any]).jsonEncodedString(), forKey: child.label!) } else if child.value is [String] { - c[child.key] = modifyValue((child.value as! [String]).joined(separator: ","), forKey: child.key) + c[child.label!] = modifyValue((child.value as! [String]).joined(separator: ","), forKey: child.label!) } else { - c[child.key] = modifyValue(child.value, forKey: child.key) + c[child.label!] = modifyValue(child.value, forKey: child.label!) } } } @@ -112,12 +111,12 @@ open class StORM : CCXMirror { if primaryKey.isNotNil { for case let (label, value) in self.allChildren() { if label == primaryKey { - return (label, modifyValue(value, forKey: label)) + return (label!, modifyValue(value, forKey: label!)) } } } else { for case let (label, value) in self.allChildren() { - return (label, modifyValue(value, forKey: label)) + return (label!, modifyValue(value, forKey: label!)) } } return ("id", "unknown") From 42dd9f0ed7f6e703a9463fd539d40519b49a3e2f Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 24 Nov 2017 11:55:33 -0500 Subject: [PATCH 13/52] Last changes for backwards compatability --- Sources/StORM/CCXMirroring.swift | 5 ++- Sources/StORM/StORM.swift | 59 +++++++++++--------------------- 2 files changed, 22 insertions(+), 42 deletions(-) diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/CCXMirroring.swift index 807e54b..e7233a9 100644 --- a/Sources/StORM/CCXMirroring.swift +++ b/Sources/StORM/CCXMirroring.swift @@ -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!) diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 48357fc..26eb203 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -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,17 +97,8 @@ 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!)) - } + for case let (label, value) in self.allChildren() { + return (label!, modifyValue(value, forKey: label!)) } return ("id", "unknown") } From deda7d1478f48e2550a5b90a714c7cc0c3956ae1 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 24 Nov 2017 12:02:24 -0500 Subject: [PATCH 14/52] Last changes for backwards compatability --- Sources/StORM/CCXExtensions.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift index 17be7c9..678d5b9 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/CCXExtensions.swift @@ -98,6 +98,16 @@ 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 index = allChild.index(where: { (child) -> Bool in + return child.label.isNotNil && child.label == StORM.primaryKeyLabel! + }) { + allChild.remove(label: keyLabel) + let idChild = allChild[index] + allChild.insert(idChild, at: 0) + } + } return allChild } } From 1d40673a97de02395a8df0834805304edb33e396 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 24 Nov 2017 12:48:45 -0500 Subject: [PATCH 15/52] Working on last update to storm for specifying in an override function, the primary key for a table/model --- Sources/StORM/CCXExtensions.swift | 6 +++--- Sources/StORM/CCXMirroring.swift | 10 +++++++--- Sources/StORM/StORM.swift | 21 +++++++++------------ 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift index 678d5b9..129b1c9 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/CCXExtensions.swift @@ -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] diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/CCXMirroring.swift index e7233a9..0591731 100644 --- a/Sources/StORM/CCXMirroring.swift +++ b/Sources/StORM/CCXMirroring.swift @@ -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 } } diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 26eb203..bf43567 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -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 children { - - if child.label.isNotNil && !child.label!.hasPrefix("internal_") && !child.label!.hasPrefix("_") { + for child in self.allChildren(primaryKey: self.primaryKey()) { + guard let key = child.label else { + continue + } + 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) From 49e74f0dca269377e6c76f835af567910a42c84d Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 24 Nov 2017 12:54:26 -0500 Subject: [PATCH 16/52] Renaming the open function for overriding the primaryKey to primaryKeyLabel --- Sources/StORM/CCXMirroring.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/CCXMirroring.swift index 0591731..7df2c8c 100644 --- a/Sources/StORM/CCXMirroring.swift +++ b/Sources/StORM/CCXMirroring.swift @@ -16,7 +16,7 @@ public protocol CCXMirroring { func didInitializeSuperclass() func allChildren(includingNilValues : Bool, primaryKey: String?) -> [Mirror.Child] - func primaryKey() -> String? + func primaryKeyLabel() -> String? } open class CCXMirror: CCXMirroring { @@ -25,7 +25,7 @@ open class CCXMirror: CCXMirroring { public func didInitializeSuperclass() { self.superclassCount += 1 } - open func primaryKey() -> String? { + open func primaryKeyLabel() -> String? { return nil } /// This function goes through all the superclass mirrors. This is dependent on the CCXMirroring protocol. From 90f85f313a0f0888b4ebb02e4028179ccbc21f8f Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 24 Nov 2017 12:58:41 -0500 Subject: [PATCH 17/52] Oopsie - fixing the upate to the primaryKeyLabel --- Sources/StORM/StORM.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index bf43567..75dca1e 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -36,7 +36,7 @@ open class StORM : CCXMirror { var c = [(String, Any)]() var count = 0 // If the StORM primary key is nil, we should assume the first will be the primary key. - for child in self.allChildren(primaryKey: self.primaryKey()) { + for child in self.allChildren(primaryKey: self.primaryKeyLabel()) { guard let key = child.label else { continue } @@ -56,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(primaryKey: self.primaryKey()) { + for case let (label?, value) in self.allChildren(primaryKey: self.primaryKeyLabel()) { 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))) @@ -76,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(primaryKey: self.primaryKey()) { + for case let (label?, value) in self.allChildren(primaryKey: self.primaryKeyLabel()) { if count >= offset && !label.hasPrefix("internal_") && !label.hasPrefix("_") { if value is [String:Any] { c[label] = modifyValue(try! (value as! [String:Any]).jsonEncodedString(), forKey: label) From 05eb48ea029c72b657242a585b7c2db7eca51100 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 24 Nov 2017 14:16:14 -0500 Subject: [PATCH 18/52] Fixing the issue with multiple primary keys in the setup statement --- Sources/StORM/CCXExtensions.swift | 24 ++++-------------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift index 129b1c9..2966536 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/CCXExtensions.swift @@ -103,31 +103,15 @@ extension Array where Iterator.Element == Mirror { if let index = allChild.index(where: { (child) -> Bool in return child.label == keyLabel }) { - allChild.remove(label: keyLabel) - let idChild = allChild[index] - allChild.insert(idChild, at: 0) + allChild.move(at: index, to: 0) } } return allChild } } -extension Array where Iterator.Element == Mirror.Child { - mutating func remove(label : String) { - if let index = self.index(where: { (child) -> Bool in - return child.label.isNotNil && child.label == StORM.primaryKeyLabel - }) { - self.remove(at: index) - } - } -} - -extension Dictionary where Key == String, Value == Any { - public func asData() -> [(String, Any)] { - var data : [(String,Any)] = [] - for row in self { - data.append(row) - } - return data +extension Array { + mutating func move(at oldIndex: Int, to newIndex: Int) { + self.insert(self.remove(at: oldIndex), at: newIndex) } } From 62ec4d70f624de38a80309e8fe33f4ff0af43318 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 24 Nov 2017 15:04:01 -0500 Subject: [PATCH 19/52] Fixing last issue for saving/updating --- Sources/StORM/StORM.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 75dca1e..9f8bbb1 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -94,7 +94,7 @@ 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) { - for case let (label, value) in self.allChildren() { + for case let (label, value) in self.allChildren(includingNilValues: true, primaryKey: self.primaryKeyLabel()) { return (label!, modifyValue(value, forKey: label!)) } return ("id", "unknown") From fa8eda53e9a53c7bedf19ff33143ec12f6630550 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 24 Nov 2017 20:56:08 -0500 Subject: [PATCH 20/52] Updating StORM last time after making few minor changes after testing postgresStorm module --- Sources/StORM/CCXExtensions.swift | 2 ++ Sources/StORM/StORM.swift | 38 +++++++++++++++++++------------ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift index 2966536..c1610a0 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/CCXExtensions.swift @@ -90,6 +90,8 @@ extension Array where Iterator.Element == Mirror { if !includingNilValues { if child.label.isNotNil, String(describing: child.value) != "nil" { allChild.append(child) + } else if child.label.isNotNil, child.label == "created" || child.label == "modified" { + allChild.append(child) } } else { if child.label.isNotNil { diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 9f8bbb1..fe6499b 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -49,7 +49,27 @@ open class StORM : CCXMirror { return c } - open func modifyValue(_ v: Any, forKey k: String) -> Any { return v } + open func modifyValue(_ v: Any, forKey k: String) -> Any { + + guard String(describing: v) != "nil" else { return v } + switch type(of: v) { + case is Int?.Type: + return v as! Int + case is String?.Type: + return v as! String + case is Double?.Type: + return v as! Double + case is Float?.Type: + return v as! Float + case is [String]?.Type: + return v as! [String] + case is [String:Any]?.Type: + return try! (v as! [String:Any]).jsonEncodedString() + default: + return v + } + + } /// 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. @@ -58,13 +78,7 @@ open class StORM : CCXMirror { var count = 0 for case let (label?, value) in self.allChildren(primaryKey: self.primaryKeyLabel()) { 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((label, modifyValue(value, forKey: label))) - } + c.append((label, modifyValue(value, forKey: label))) } count += 1 } @@ -78,13 +92,7 @@ open class StORM : CCXMirror { var count = 0 for case let (label?, value) in self.allChildren(primaryKey: self.primaryKeyLabel()) { 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[label] = modifyValue(value, forKey: label) - } + c[label] = modifyValue(value, forKey: label) } count += 1 } From ed3f71b8431f875634ee0369b5755bb68eba0969 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Sun, 26 Nov 2017 17:47:36 -0500 Subject: [PATCH 21/52] Making it so created & modified dates for integers is automatically set & filtered through the asData function, if the model contains 'created' or 'modified' columns --- Sources/StORM/CCXExtensions.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift index c1610a0..4656714 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/CCXExtensions.swift @@ -90,8 +90,11 @@ extension Array where Iterator.Element == Mirror { if !includingNilValues { if child.label.isNotNil, String(describing: child.value) != "nil" { allChild.append(child) + // Automatic created & modified fields: } else if child.label.isNotNil, child.label == "created" || child.label == "modified" { - allChild.append(child) + var mutableChild = child + mutableChild.value = Int(Date().timeIntervalSince1970) + allChild.append(mutableChild) } } else { if child.label.isNotNil { From 93d6e57dbd9d1a3c8305aa13be0cdfa7d5830364 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Sun, 26 Nov 2017 18:05:33 -0500 Subject: [PATCH 22/52] Making it so StORM fills the integer value for the date for created & modified for the values that would be defaulted to a value --- Sources/StORM/CCXExtensions.swift | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift index 4656714..fb1f925 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/CCXExtensions.swift @@ -98,12 +98,18 @@ extension Array where Iterator.Element == Mirror { } } else { if child.label.isNotNil { - allChild.append(child) + if child.label! == "created" || child.label! == "modified" { + var mutableChild = child + mutableChild.value = Int(Date().timeIntervalSince1970) + allChild.append(mutableChild) + } else { + allChild.append(child) + } } } }) } - // 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 returned for asData/asDataDic functions: if let keyLabel = primaryKey, allChild.first?.label != keyLabel { if let index = allChild.index(where: { (child) -> Bool in return child.label == keyLabel @@ -116,6 +122,7 @@ extension Array where Iterator.Element == Mirror { } extension Array { + /// This removes & inserts the object at the old index, to the new specified index. mutating func move(at oldIndex: Int, to newIndex: Int) { self.insert(self.remove(at: oldIndex), at: newIndex) } From 7d8994b138bcb151d9a0e6144a0315dbc7fb7479 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Sun, 26 Nov 2017 19:05:24 -0500 Subject: [PATCH 23/52] Last change for defaulting values for created/modified integer date --- Sources/StORM/CCXExtensions.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift index fb1f925..77161d3 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/CCXExtensions.swift @@ -89,7 +89,14 @@ extension Array where Iterator.Element == Mirror { // Make sure our child has a label & the string describing the value is not nil. (Making optionals supported) if !includingNilValues { if child.label.isNotNil, String(describing: child.value) != "nil" { - allChild.append(child) + // If we default a created/modified integer to zero we need to overwrite it here: + if child.label! == "created" || child.label! == "modified" { + var mutableChild = child + mutableChild.value = Int(Date().timeIntervalSince1970) + allChild.append(mutableChild) + } else { + allChild.append(child) + } // Automatic created & modified fields: } else if child.label.isNotNil, child.label == "created" || child.label == "modified" { var mutableChild = child From 32b3fa93327c5ad1eef21f02ea2e09fb5824b4da Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Sun, 26 Nov 2017 23:56:29 -0500 Subject: [PATCH 24/52] Last change to cols function to return columns even when they are nil --- Sources/StORM/StORM.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index fe6499b..2156420 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -36,7 +36,7 @@ open class StORM : CCXMirror { var c = [(String, Any)]() var count = 0 // If the StORM primary key is nil, we should assume the first will be the primary key. - for child in self.allChildren(primaryKey: self.primaryKeyLabel()) { + for child in self.allChildren(includingNilValues: true, primaryKey: self.primaryKeyLabel()) { guard let key = child.label else { continue } From 7c3313af7054c5a36d6769b3788fd98f87039385 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Thu, 30 Nov 2017 12:27:25 -0500 Subject: [PATCH 25/52] Reworking using foundation to get the current date interval since 1970 to using SwiftMoment & adding swiftmoment as a dependency to StORM --- Package.swift | 1 + Sources/StORM/CCXExtensions.swift | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Package.swift b/Package.swift index e89de9d..0593006 100644 --- a/Package.swift +++ b/Package.swift @@ -6,5 +6,6 @@ let package = Package( targets: [], dependencies: [ .Package(url: "https://github.com/PerfectlySoft/PerfectLib.git", majorVersion: 3), + .Package(url: "https://github.com/iamjono/SwiftMoment.git", majorVersion: 0) ] ) diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift index 77161d3..7cddf48 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/CCXExtensions.swift @@ -5,6 +5,7 @@ // Created by Ryan Coyne on 11/22/17. // +import SwiftMoment import Foundation //MARK: - Optionals @@ -92,7 +93,7 @@ extension Array where Iterator.Element == Mirror { // If we default a created/modified integer to zero we need to overwrite it here: if child.label! == "created" || child.label! == "modified" { var mutableChild = child - mutableChild.value = Int(Date().timeIntervalSince1970) + mutableChild.value = Int(utc().epoch()) allChild.append(mutableChild) } else { allChild.append(child) @@ -100,14 +101,14 @@ extension Array where Iterator.Element == Mirror { // Automatic created & modified fields: } else if child.label.isNotNil, child.label == "created" || child.label == "modified" { var mutableChild = child - mutableChild.value = Int(Date().timeIntervalSince1970) + mutableChild.value = Int(utc().epoch()) allChild.append(mutableChild) } } else { if child.label.isNotNil { if child.label! == "created" || child.label! == "modified" { var mutableChild = child - mutableChild.value = Int(Date().timeIntervalSince1970) + mutableChild.value = Int(utc().epoch()) allChild.append(mutableChild) } else { allChild.append(child) From d81a7779542b06a4abe584495132b0a887641eec Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Thu, 30 Nov 2017 12:29:53 -0500 Subject: [PATCH 26/52] Updating extension --- Sources/StORM/CCXExtensions.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift index 7cddf48..66c3611 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/CCXExtensions.swift @@ -42,7 +42,7 @@ extension Optional { } /// This returns the optionally wrapped object as an array of dictionaries...value. var arrayDicValue : [[String:Any]]! { - return self as? [[String:Any]] ?? [[:]] + return self as? [[String:Any]] ?? [] } /// This returns the optionally wrapped object as an integer value. var intValue : Int? { From 715bdbd6c204304f8eb8b8878e507eb3845207b8 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Thu, 30 Nov 2017 15:18:47 -0500 Subject: [PATCH 27/52] Adding comments to some of the CCX extensions & mirroring protocol object --- Sources/StORM/CCXExtensions.swift | 2 +- Sources/StORM/CCXMirroring.swift | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/CCXExtensions.swift index 66c3611..ebf314e 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/CCXExtensions.swift @@ -117,7 +117,7 @@ extension Array where Iterator.Element == Mirror { } }) } - // Lets make sure if the primaryKey is set, it is the first object returned for asData/asDataDic functions: + // Lets make sure if the primaryKey is set, it is the first object returned for asData/asDataDic & firstAsKey functions: if let keyLabel = primaryKey, allChild.first?.label != keyLabel { if let index = allChild.index(where: { (child) -> Bool in return child.label == keyLabel diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/CCXMirroring.swift index 7df2c8c..86c2e76 100644 --- a/Sources/StORM/CCXMirroring.swift +++ b/Sources/StORM/CCXMirroring.swift @@ -25,6 +25,10 @@ open class CCXMirror: CCXMirroring { public func didInitializeSuperclass() { self.superclassCount += 1 } + + /// This is intended to make it easier to specify your primary key, rather than having the id specifically in this model. + /// + /// - Returns: This returns the label for the primary key for this model. open func primaryKeyLabel() -> String? { return nil } From ef598db162200cd744c0ce35412a576de0e99bc5 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Thu, 30 Nov 2017 16:11:09 -0500 Subject: [PATCH 28/52] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 8f51ec9..87fc598 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,9 @@ StORM is a modular ORM for Swift, layered on top of Perfect. It aims to be easy to use, but flexible, and maintain consistency between datasource implementations for the user: you, the developer. It tries to allow you to write great code without worrying about the details of how to interact with the database. Please see the full documentation at: [https://www.perfect.org/docs/StORM.html](https://www.perfect.org/docs/StORM.html) + +# Latest Updates: + +- Increased flexibility with determining the primary key, without having to have it your first declared variable in your model. +- Includes subclassing support. +- Includes optional variable support. From b3a1bf8eb5e73343b40309704289f5fbd090aa07 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 1 Dec 2017 11:20:25 -0500 Subject: [PATCH 29/52] Renaming the StORM extensions/mirroring protocols --- Sources/StORM/StORM.swift | 2 +- .../StORM/{CCXExtensions.swift => StORMExtensions.swift} | 2 +- .../StORM/{CCXMirroring.swift => StORMMirroring.swift} | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) rename Sources/StORM/{CCXExtensions.swift => StORMExtensions.swift} (99%) rename Sources/StORM/{CCXMirroring.swift => StORMMirroring.swift} (94%) diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 2156420..95c910f 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -13,7 +13,7 @@ public var StORMdebug = false /// Base StORM superclass from which all Database-Connector StORM classes inherit. /// Provides base functionality and rules. -open class StORM : CCXMirror { +open class StORM : StORMMirror { /// Results container of type StORMResultSet. open var results = StORMResultSet() diff --git a/Sources/StORM/CCXExtensions.swift b/Sources/StORM/StORMExtensions.swift similarity index 99% rename from Sources/StORM/CCXExtensions.swift rename to Sources/StORM/StORMExtensions.swift index ebf314e..df52255 100644 --- a/Sources/StORM/CCXExtensions.swift +++ b/Sources/StORM/StORMExtensions.swift @@ -1,5 +1,5 @@ // -// CCXExtensions.swift +// StORMExtensions.swift // StORM // // Created by Ryan Coyne on 11/22/17. diff --git a/Sources/StORM/CCXMirroring.swift b/Sources/StORM/StORMMirroring.swift similarity index 94% rename from Sources/StORM/CCXMirroring.swift rename to Sources/StORM/StORMMirroring.swift index 86c2e76..a58ab2f 100644 --- a/Sources/StORM/CCXMirroring.swift +++ b/Sources/StORM/StORMMirroring.swift @@ -1,25 +1,25 @@ // -// CCXMirroring.swift +// StORMMirroring.swift // // Created by Ryan Coyne on 11/22/17. // Copyright © 2017 ClearCodeX, Inc. All rights reserved. // // -// CCXMirror.swift +// StORMMirror.swift // // // Created by Ryan Coyne on 11/17/17. // Copyright © 2017 ClearCodeX, Inc. All rights reserved. // -public protocol CCXMirroring { +public protocol StORMMirroring { func didInitializeSuperclass() func allChildren(includingNilValues : Bool, primaryKey: String?) -> [Mirror.Child] func primaryKeyLabel() -> String? } -open class CCXMirror: CCXMirroring { +open class StORMMirror: StORMMirroring { // The superclass count will include CCXMirror, StORM, & PostgresStORM by the time we get to the subclasses we need to process. private var superclassCount = 0 public func didInitializeSuperclass() { From d3b5286ee1fc255e300508409ac8bae8120d5755 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 1 Dec 2017 11:21:07 -0500 Subject: [PATCH 30/52] Last comment change --- Sources/StORM/StORMMirroring.swift | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Sources/StORM/StORMMirroring.swift b/Sources/StORM/StORMMirroring.swift index a58ab2f..9dd4b81 100644 --- a/Sources/StORM/StORMMirroring.swift +++ b/Sources/StORM/StORMMirroring.swift @@ -1,16 +1,8 @@ // // StORMMirroring.swift -// -// Created by Ryan Coyne on 11/22/17. -// Copyright © 2017 ClearCodeX, Inc. All rights reserved. -// - -// // StORMMirror.swift // -// -// Created by Ryan Coyne on 11/17/17. -// Copyright © 2017 ClearCodeX, Inc. All rights reserved. +// Created by Ryan Coyne on 11/22/17. // public protocol StORMMirroring { From 7f42d5552f81c28e095b96846424217854d50350 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 1 Dec 2017 11:38:53 -0500 Subject: [PATCH 31/52] Updating naming conventions of the extensions/mirroring protocol --- Sources/StORM/StORM.swift | 1 + Sources/StORM/StORMExtensions.swift | 2 ++ Sources/StORM/StORMMirroring.swift | 1 + 3 files changed, 4 insertions(+) diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 95c910f..2ed19e1 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -49,6 +49,7 @@ open class StORM : StORMMirror { return c } + /// This modifyValue function now supports optional variables and forces the value as the type it is, so that the String(describing) doesn't include the optional string wrapping the actual value. open func modifyValue(_ v: Any, forKey k: String) -> Any { guard String(describing: v) != "nil" else { return v } diff --git a/Sources/StORM/StORMExtensions.swift b/Sources/StORM/StORMExtensions.swift index df52255..19fd3f4 100644 --- a/Sources/StORM/StORMExtensions.swift +++ b/Sources/StORM/StORMExtensions.swift @@ -83,6 +83,8 @@ extension Bool { } //MARK: - Mirror Array: extension Array where Iterator.Element == Mirror { + /// This function will automatically return the created/modified values IF your model has a variable with that name. Created/modified are intended to be integer values of UTC time since 1970. + /// Createdby/Modifiedby fields are automatically supported at the database level in PostgresStORM. func allChildren(includingNilValues: Bool = false, primaryKey: String? = nil) -> [Mirror.Child] { var allChild : [Mirror.Child] = [] for mirror in self { diff --git a/Sources/StORM/StORMMirroring.swift b/Sources/StORM/StORMMirroring.swift index 9dd4b81..ca04788 100644 --- a/Sources/StORM/StORMMirroring.swift +++ b/Sources/StORM/StORMMirroring.swift @@ -44,6 +44,7 @@ open class StORMMirror: StORMMirroring { return mirrors } /// This returns all the children, even all the superclass mirrored children. Use allChildren().asData() to return an array of key/values. + /// The includingNilValues optional parameter is used to return the mirror children even if the value is nil. 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, primaryKey: primaryKey) From 13b5b78da835bbe0f8c2ba4ac6f1e88740fb8990 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 1 Dec 2017 15:06:59 -0500 Subject: [PATCH 32/52] Update README.md --- README.md | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/README.md b/README.md index 87fc598..1682343 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,105 @@ Please see the full documentation at: [https://www.perfect.org/docs/StORM.html]( - Increased flexibility with determining the primary key, without having to have it your first declared variable in your model. - Includes subclassing support. - Includes optional variable support. + +## Usage: +### Example -> Conforming to StORMirroring: +Say we have to models using PostgresStORM: + +``` +class AuditFields: PostgresStORM { + + /// This is when the table row has been created. + var created : Int? = nil + /// This is the id of the user that has created the row. + var createdby : String? = nil + /// This is when the table row has been modified. + var modified : Int? = nil + /// This is the id of the user that has modified the row. + var modifiedby : String? = nil + + // This is needed when created a subclass containing other fields to re-use for other models. + override init() { + super.init() + self.didInitializeSuperclass() + } +} + +// The outer most class does not need to override init & call didInitializeSuperclass. This helps with identifying the id in the model. +class TestUser2: AuditFields { + // Notice we now do not need to put id at the top. However, this is backwards compatable, meaning if you do not want to subclass, or if someone updates & has the same models as configured before, they do not need to add any extra code to set the primaryKeyLabel. + var id : Int? = nil + var firstname : String? = nil { + didSet { + if oldValue != nil && firstname == nil { + self.nullColumns.insert("firstname") + } else if firstname != nil { + self.nullColumns.remove("firstname") + } + } + } + var lastname : String? = nil { + didSet { + if oldValue != nil && lastname == nil { + self.nullColumns.insert("lastname") + } else if firstname != nil { + self.nullColumns.remove("lastname") + } + } + } + var phonenumber : String? = nil { + didSet { + if oldValue != nil && phonenumber == nil { + self.nullColumns.insert("phonenumber") + } else if firstname != nil { + self.nullColumns.remove("phonenumber") + } + } + } + + override open func table() -> String { + return "testuser2" + } + + override func to(_ this: StORMRow) { + + // Audit fields: + id = this.data["id"] as? Int + created = this.data["created"] as? Int + createdby = this.data["createdby"] as? String + modified = this.data["modified"] as? Int + modifiedby = this.data["modifiedby"] as? String + + firstname = this.data["firstname"] as? String + lastname = this.data["lastname"] as? String + phonenumber = this.data["phonenumber"] as? String + + } + + func rows() -> [TestUser2] { + var rows = [TestUser2]() + for i in 0.. Getting all the children: +``` +// Getting all the children (including nil values or NOT) +// Returns an array of children, making sure the overrided function primaryKeyLabel takes the child for that key and places it in the first index of the array: +self.allChildren(includingNilValues: true, primaryKey: self.primaryKeyLabel()) +``` + +This provides the capability for PostgresStORM and other database supported StORM modules to be given the ability to subclass PostgresStORM objects deeper than just the single mirror of the class. + +StORMMirroring also gives you the flexibility to place your primary key in a superclass model. All you would need to do is override the primaryKeyLabel function that StORMMirroring offers. This ensures that when we create the array of children from all the mirrors, it places the primary key as the first child in the array. + + From 8aa891151b72ded6b4ad5e0464e7b0776156de69 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 1 Dec 2017 15:15:21 -0500 Subject: [PATCH 33/52] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 1682343..e5a2548 100644 --- a/README.md +++ b/README.md @@ -116,4 +116,9 @@ This provides the capability for PostgresStORM and other database supported StOR StORMMirroring also gives you the flexibility to place your primary key in a superclass model. All you would need to do is override the primaryKeyLabel function that StORMMirroring offers. This ensures that when we create the array of children from all the mirrors, it places the primary key as the first child in the array. +### Supporting Optionals in other database-StORM Modules: +- Make sure to keep track of going from an optional Non-nil value to nil, to update NULL/DEFAULT or NULL. See TestUser2 model. + +### Other -> Automatic Auditing For other database-StORM Modules: +- StORMMirroring also supports automatic created & modified values to come back if they exist in the model. They are intended to be used as integers to store as epoch timestamps. Make sure to skip over modified or created depending on if you are creating a new record or modifying a record. From e2ff7e02f51492ff871819aa441521a97870f3b3 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 1 Dec 2017 15:16:59 -0500 Subject: [PATCH 34/52] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e5a2548..54eff62 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ class AuditFields: PostgresStORM { // The outer most class does not need to override init & call didInitializeSuperclass. This helps with identifying the id in the model. class TestUser2: AuditFields { - // Notice we now do not need to put id at the top. However, this is backwards compatable, meaning if you do not want to subclass, or if someone updates & has the same models as configured before, they do not need to add any extra code to set the primaryKeyLabel. + // In this example we have id at the top but that is not mandatory now if you implement the primaryKeyLabel overrided function. var id : Int? = nil var firstname : String? = nil { didSet { From bca0e2d6a17d04fd4069c7e9a4359b0a654d9741 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Fri, 1 Dec 2017 15:20:08 -0500 Subject: [PATCH 35/52] Getting rid of the primaryKeyLabel variable that was static to storm - made more sense as an override function in the model --- Sources/StORM/StORM.swift | 3 --- 1 file changed, 3 deletions(-) diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 2ed19e1..b5ef242 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -27,9 +27,6 @@ open class StORM : StORMMirror { /// Base empty init function. public override init() {} - /// primary key label (not assuming the first child is the id). - public static var primaryKeyLabel : String? = nil - /// Provides structure introspection to client methods. public func cols(_ offset : Int = 0) -> [(String, Any)] { From e89694e24b97a105b30bf9ca3a79ee5a429cdb56 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 16 Dec 2018 13:58:07 -0500 Subject: [PATCH 36/52] Updating a statement to return the string value for conforming to CustomStringConvertible for new database types --- Sources/StORM/StORM.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index b5ef242..54ed506 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -64,7 +64,8 @@ open class StORM : StORMMirror { case is [String:Any]?.Type: return try! (v as! [String:Any]).jsonEncodedString() default: - return v + // Here we will support new database types by returning what we need conforming to CustomStringConvertable. + return String(describing: v) } } From b1624e3852909167dbd07271cbc07eaf06744d76 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 16 Dec 2018 14:14:04 -0500 Subject: [PATCH 37/52] Working on fixing this issue --- Sources/StORM/StORM.swift | 2 +- Sources/StORM/StORMExtensions.swift | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 54ed506..562483a 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -65,7 +65,7 @@ open class StORM : StORMMirror { return try! (v as! [String:Any]).jsonEncodedString() default: // Here we will support new database types by returning what we need conforming to CustomStringConvertable. - return String(describing: v) + return v } } diff --git a/Sources/StORM/StORMExtensions.swift b/Sources/StORM/StORMExtensions.swift index 19fd3f4..459c4c8 100644 --- a/Sources/StORM/StORMExtensions.swift +++ b/Sources/StORM/StORMExtensions.swift @@ -89,6 +89,7 @@ extension Array where Iterator.Element == Mirror { var allChild : [Mirror.Child] = [] for mirror in self { mirror.children.forEach({ (child) in + print(child) // Make sure our child has a label & the string describing the value is not nil. (Making optionals supported) if !includingNilValues { if child.label.isNotNil, String(describing: child.value) != "nil" { From 5aa05cf578db91225bc7818f897aa80825ecdedf Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 16 Dec 2018 16:23:03 -0500 Subject: [PATCH 38/52] CHanging up something in storm --- Sources/StORM/StORM.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 562483a..67a880b 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -38,7 +38,7 @@ open class StORM : StORMMirror { continue } if !key.hasPrefix("internal_") && !key.hasPrefix("_") { - c.append((child.label!, type(of:child.value))) + c.append((child.label!, child.value)) } count += 1 } From ec64bbc0d47b6b37c987a72086644c106a2916ec Mon Sep 17 00:00:00 2001 From: Ryan Date: Thu, 20 Dec 2018 23:52:58 -0500 Subject: [PATCH 39/52] Taking out the print statement when running through all the children --- Sources/StORM/StORMExtensions.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/StORM/StORMExtensions.swift b/Sources/StORM/StORMExtensions.swift index 459c4c8..19fd3f4 100644 --- a/Sources/StORM/StORMExtensions.swift +++ b/Sources/StORM/StORMExtensions.swift @@ -89,7 +89,6 @@ extension Array where Iterator.Element == Mirror { var allChild : [Mirror.Child] = [] for mirror in self { mirror.children.forEach({ (child) in - print(child) // Make sure our child has a label & the string describing the value is not nil. (Making optionals supported) if !includingNilValues { if child.label.isNotNil, String(describing: child.value) != "nil" { From afabb1c39de20d40d45a968b55b1e201e7b0f6c9 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 22 Dec 2018 21:38:33 -0500 Subject: [PATCH 40/52] Updating the package.swift to swift 4 SPM --- Package.swift | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Package.swift b/Package.swift index 95360f9..e387d40 100644 --- a/Package.swift +++ b/Package.swift @@ -1,12 +1,17 @@ -// Generated automatically by Perfect Assistant Application -// Date: 2018-03-02 16:12:45 +0000 +// swift-tools-version:4.0 import PackageDescription let package = Package( name: "StORM", targets: [], dependencies: [ - .Package(url: "https://github.com/PerfectlySoft/PerfectLib.git", majorVersion: 3), - .Package(url: "https://github.com/iamjono/SwiftMoment.git", majorVersion: 1), - .Package(url: "https://github.com/iamjono/SwiftString.git", majorVersion: 2), - ] + .package(url: "https://github.com/PerfectlySoft/PerfectLib.git", from: "3.0.0"), + .package(url: "https://github.com/iamjono/SwiftMoment.git", from: "1.0.0"), + .package(url: "https://github.com/iamjono/SwiftString.git", from: "2.0.0"), + ], + targets: [ + .target( + name: "StORM", + dependencies: ["PerfectLib","SwiftMoment", "SwiftString"] + ), + ] ) From 6172f4b4cb230d89cf10f60025dd5da8f7fcb5c5 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 22 Dec 2018 21:40:31 -0500 Subject: [PATCH 41/52] Updating the StORM Package.swift file to SPM version 4 --- Package.swift | 1 - Sources/StORM/StORM.swift | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Package.swift b/Package.swift index e387d40..97560cc 100644 --- a/Package.swift +++ b/Package.swift @@ -2,7 +2,6 @@ import PackageDescription let package = Package( name: "StORM", - targets: [], dependencies: [ .package(url: "https://github.com/PerfectlySoft/PerfectLib.git", from: "3.0.0"), .package(url: "https://github.com/iamjono/SwiftMoment.git", from: "1.0.0"), diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index 67a880b..be253b7 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -112,20 +112,20 @@ open class StORM : StORMMirror { let (_, val) = firstAsKey() // Grab the type of value: - let type = type(of: val) + let thetype = type(of: val) // Check if we are nil, we would then of course have an empty primary key. guard String(describing: val) != "nil" else { return true } // For now we will be expecting String & Integer key types: - switch type { + switch thetype { case is Int.Type, is Int?.Type: return (val as! Int == 0) case is String.Type, is String?.Type: return (val as! String).isEmpty default: - print("[StORM] WARNING: [\(#function)] Unexpected \(type) for PRIMARY KEY.") + print("[StORM] WARNING: [\(#function)] Unexpected \(thetype) for PRIMARY KEY.") return false } From 3e46381e9167b4310806690b4c708da1ab5d06b0 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 22 Dec 2018 22:37:16 -0500 Subject: [PATCH 42/52] Updating the package file for storm to specify the file path --- Package.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 97560cc..0d79aa8 100644 --- a/Package.swift +++ b/Package.swift @@ -10,7 +10,8 @@ let package = Package( targets: [ .target( name: "StORM", - dependencies: ["PerfectLib","SwiftMoment", "SwiftString"] + dependencies: ["PerfectLib","SwiftMoment", "SwiftString"], + path: "Sources/StORM" ), ] ) From f748a7219913fd15c2824f4cd72a0523c8a6e92a Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 22 Dec 2018 22:42:24 -0500 Subject: [PATCH 43/52] Updating package file --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index 0d79aa8..e6c7921 100644 --- a/Package.swift +++ b/Package.swift @@ -11,7 +11,7 @@ let package = Package( .target( name: "StORM", dependencies: ["PerfectLib","SwiftMoment", "SwiftString"], - path: "Sources/StORM" + path: "StORM" ), ] ) From 6fd469db193600ec6d6c904ad8378ecebe57eaa5 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 22 Dec 2018 22:43:15 -0500 Subject: [PATCH 44/52] Updating package file --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index e6c7921..0d79aa8 100644 --- a/Package.swift +++ b/Package.swift @@ -11,7 +11,7 @@ let package = Package( .target( name: "StORM", dependencies: ["PerfectLib","SwiftMoment", "SwiftString"], - path: "StORM" + path: "Sources/StORM" ), ] ) From f92222eca5f88e0efdc2d468699c50d9d18423c2 Mon Sep 17 00:00:00 2001 From: Ryan Coyne Date: Sat, 22 Dec 2018 23:01:58 -0500 Subject: [PATCH 45/52] Update Package.swift --- Package.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index 0d79aa8..69914a2 100644 --- a/Package.swift +++ b/Package.swift @@ -10,8 +10,8 @@ let package = Package( targets: [ .target( name: "StORM", - dependencies: ["PerfectLib","SwiftMoment", "SwiftString"], - path: "Sources/StORM" + dependencies: ["PerfectLib","SwiftMoment", "SwiftString"] +// path: "Sources/StORM" ), ] ) From 3b67e22f6ceae0c23047d6b1e21f9f88d089c6af Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 23 Dec 2018 13:42:53 -0500 Subject: [PATCH 46/52] Updatingg the package file --- Package.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Package.swift b/Package.swift index 69914a2..a74787c 100644 --- a/Package.swift +++ b/Package.swift @@ -2,6 +2,12 @@ import PackageDescription let package = Package( name: "StORM", + products: [ + // Products define the executables and libraries produced by a package, and make them visible to other packages. + .library( + name: "StORM", + targets: ["StORM"]), + ], dependencies: [ .package(url: "https://github.com/PerfectlySoft/PerfectLib.git", from: "3.0.0"), .package(url: "https://github.com/iamjono/SwiftMoment.git", from: "1.0.0"), From 9ca347a5a4651cd421c0a20039b9d5cbd1b3a607 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 23 Dec 2018 13:44:17 -0500 Subject: [PATCH 47/52] Adding in the path for StORM --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index a74787c..bfc284a 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,7 @@ let package = Package( .target( name: "StORM", dependencies: ["PerfectLib","SwiftMoment", "SwiftString"] -// path: "Sources/StORM" + path: "Sources/StORM" ), ] ) From 9bfdc121b7c1f3fa1bd18d03a88ba10150849ac9 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 23 Dec 2018 13:44:30 -0500 Subject: [PATCH 48/52] Adding in the path for StORM and adding in the comma --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index bfc284a..4eade4d 100644 --- a/Package.swift +++ b/Package.swift @@ -16,7 +16,7 @@ let package = Package( targets: [ .target( name: "StORM", - dependencies: ["PerfectLib","SwiftMoment", "SwiftString"] + dependencies: ["PerfectLib","SwiftMoment", "SwiftString"], path: "Sources/StORM" ), ] From 42d6efe857bc9b42edd6f5b53f3ee02b2c90009a Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 23 Dec 2018 14:27:06 -0500 Subject: [PATCH 49/52] Updating the StORM package to use my packages that use the updated package.swift file --- Package.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index 4eade4d..4f64073 100644 --- a/Package.swift +++ b/Package.swift @@ -10,8 +10,8 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/PerfectlySoft/PerfectLib.git", from: "3.0.0"), - .package(url: "https://github.com/iamjono/SwiftMoment.git", from: "1.0.0"), - .package(url: "https://github.com/iamjono/SwiftString.git", from: "2.0.0"), + .package(url: "https://github.com/ryancoyne/SwiftMoment.git", from: "1.0.0"), + .package(url: "https://github.com/ryancoyne/SwiftString.git", from: "2.0.0"), ], targets: [ .target( From ab848b0e46e57a6c352fc2d44186ee5faf8f5142 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 23 Dec 2018 14:29:07 -0500 Subject: [PATCH 50/52] Updating the package file to make it line up with everything --- Package.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Package.swift b/Package.swift index 4f64073..552ae16 100644 --- a/Package.swift +++ b/Package.swift @@ -1,18 +1,18 @@ // swift-tools-version:4.0 import PackageDescription let package = Package( - name: "StORM", + name: "StORM", products: [ // Products define the executables and libraries produced by a package, and make them visible to other packages. .library( name: "StORM", targets: ["StORM"]), ], - dependencies: [ + dependencies: [ .package(url: "https://github.com/PerfectlySoft/PerfectLib.git", from: "3.0.0"), .package(url: "https://github.com/ryancoyne/SwiftMoment.git", from: "1.0.0"), - .package(url: "https://github.com/ryancoyne/SwiftString.git", from: "2.0.0"), - ], + .package(url: "https://github.com/ryancoyne/SwiftString.git", from: "2.0.0"), + ], targets: [ .target( name: "StORM", From 5c41d236e9695b98ffa6d1cff02db57bd6e681b5 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 30 Dec 2018 13:49:55 -0500 Subject: [PATCH 51/52] Updating the package to swift 4.2 and removed out SwiftString --- Package.swift | 6 +++--- Sources/StORM/Extract.swift | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Package.swift b/Package.swift index 552ae16..c9a45c0 100644 --- a/Package.swift +++ b/Package.swift @@ -11,13 +11,13 @@ let package = Package( dependencies: [ .package(url: "https://github.com/PerfectlySoft/PerfectLib.git", from: "3.0.0"), .package(url: "https://github.com/ryancoyne/SwiftMoment.git", from: "1.0.0"), - .package(url: "https://github.com/ryancoyne/SwiftString.git", from: "2.0.0"), +// .package(url: "https://github.com/ryancoyne/SwiftString.git", from: "2.0.0"), ], targets: [ .target( name: "StORM", - dependencies: ["PerfectLib","SwiftMoment", "SwiftString"], - path: "Sources/StORM" + dependencies: ["PerfectLib", "SwiftMoment"], + path: "Sources/StORM" ), ] ) diff --git a/Sources/StORM/Extract.swift b/Sources/StORM/Extract.swift index 65ef8c2..5544e27 100644 --- a/Sources/StORM/Extract.swift +++ b/Sources/StORM/Extract.swift @@ -7,7 +7,6 @@ import Foundation import SwiftMoment -import SwiftString extension StORM { @@ -78,14 +77,14 @@ extension StORM { // Array Of Strings // ======================================================================================= public static func arrayOfStrings(_ data: [String: Any], _ name: String, _ def: [String]? = [String]()) -> [String]? { - return (data[name] as? String ?? "").split(",").map{ $0.trimmed() } // note default ignored right now + return (data[name] as? String ?? "").split(separator: ",").map{ $0.trimmingCharacters(in: .whitespacesAndNewlines) } // note default ignored right now } // ======================================================================================= // Array Of Integers // ======================================================================================= public static func arrayOfIntegers(_ data: [String: Any], _ name: String, _ def: [Int]? = [Int]()) -> [Int]? { - return (data[name] as? String ?? "").split(",").map{ Int($0.trimmed()) ?? 0 } // note default ignored right now + return (data[name] as? String ?? "").split(separator: ",").map{ Int($0.trimmingCharacters(in: .whitespacesAndNewlines)) ?? 0 } // note default ignored right now } // ======================================================================================= From d85e12546c4b85c90359fec877867f03640d5504 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sat, 5 Jan 2019 16:17:59 -0500 Subject: [PATCH 52/52] Updating the StORM package so we dont have SPM conflicts --- Package.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Package.swift b/Package.swift index c9a45c0..a35549f 100644 --- a/Package.swift +++ b/Package.swift @@ -9,7 +9,7 @@ let package = Package( targets: ["StORM"]), ], dependencies: [ - .package(url: "https://github.com/PerfectlySoft/PerfectLib.git", from: "3.0.0"), + .package(url: "https://github.com/ryancoyne/PerfectLib.git", from: "4.0.0"), .package(url: "https://github.com/ryancoyne/SwiftMoment.git", from: "1.0.0"), // .package(url: "https://github.com/ryancoyne/SwiftString.git", from: "2.0.0"), ],