From 93e171e053434ae537e61a5bfd81066fefdde7b2 Mon Sep 17 00:00:00 2001 From: 15038777234 <15038777234@163.com> Date: Tue, 15 Aug 2017 10:14:56 +0800 Subject: [PATCH 1/5] Support parent class field field seat database field refinement method decoupling --- Sources/StORM/StORM.swift | 96 ++++++++++++++--------------- Sources/StORM/StORMMirrorData.swift | 68 ++++++++++++++++++++ 2 files changed, 114 insertions(+), 50 deletions(-) create mode 100644 Sources/StORM/StORMMirrorData.swift diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index ab5314e..ba62943 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -11,6 +11,10 @@ /// When true, certain methods will generate a debug message under certain conditions. public var StORMdebug = false +/// If you set the StORMIgnoreClassString field, you support the parent class property lookup until you find the set field class, or you only support sub class property lookups +public var StORMIgnoreClassString:String = "" +/// Set the suffix to the primary key and defaults to id +public var StORMPrimarykeySuffix:String = "id" /// Base StORM superclass from which all Database-Connector StORM classes inherit. /// Provides base functionality and rules. @@ -24,6 +28,9 @@ open class StORM { /// Contain last error message as string. open var errorMsg = "" + + /// Database primary key + private var primary:(String,Any)? /// Base empty init function. public init() {} @@ -31,20 +38,15 @@ open class StORM { /// 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 - } + let mirrorData = StORMMirrorData.mirror(mirror: mirror) + for child in mirrorData.childs { + c.append((child.label, type(of: child.value))) + } return c } + + open func modifyValue(_ v: Any, forKey k: String) -> Any { return v } @@ -52,20 +54,17 @@ open class StORM { /// If any object property begins with an underscore, or with "internal_" it is omitted from the response. public func asData(_ offset: Int = 0) -> [(String, Any)] { 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))) - } else { - c.append((label, modifyValue(value, forKey: label))) - } - } - count += 1 - } + let mirrorData = StORMMirrorData.mirror(mirror: mirror) + for child in mirrorData.childs { + 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))) + } else { + c.append((child.label, modifyValue(child.value, forKey: child.label))) + } + } return c } @@ -73,20 +72,9 @@ open class StORM { /// If any object property begins with an underscore, or with "internal_" it is omitted from the response. public 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) - } else { - c[label] = modifyValue(value, forKey: label) - } - } - count += 1 - } + for (label,value) in asData(offset) { + c[label] = value + } return c } @@ -94,10 +82,10 @@ open class StORM { /// 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)) - } - return ("id", "unknown") + guard let child = StORMMirrorData.mirror(mirror: mirror).primary else { + return ("id", "unknown") + } + return (child.label, modifyValue(child.value, forKey: child.label)) } /// Returns a boolean that is true if the first property in the class contains a value. @@ -109,13 +97,21 @@ open class StORM { } else { return false } - } else { - if (val as! String).isEmpty { - return true - } else { - return false - } - } + } else if val is Int32 { + if val as! Int32 == 0 { + return true + } else { + return false + } + } else if val is String { + if (val as! String).isEmpty { + return true + } else { + return false + } + } else { + return false + } } /// The create method is designed to be overridden @@ -123,6 +119,6 @@ open class StORM { open func create() throws { throw StORMError.notImplemented } - + } diff --git a/Sources/StORM/StORMMirrorData.swift b/Sources/StORM/StORMMirrorData.swift new file mode 100644 index 0000000..b9e2e47 --- /dev/null +++ b/Sources/StORM/StORMMirrorData.swift @@ -0,0 +1,68 @@ +// +// StORMMirrorData.swift +// StORM +// +// Created by 张行 on 2017/8/14. +// +// + +import Foundation + +/// A field that supports the search for subclasses and the parent class matches the attribute seat database +public class StORMMirrorData { + public var childs:[StORMMirrorDataChild] = [] /// In addition to the primary key, the database field + public var primary:StORMMirrorDataChild? /// The primary key field may exist or does not exist + + /// Resolve Mirror to find the criteria for the database field and the primary key + public static func mirror(mirror:Mirror) -> StORMMirrorData { + let mirrorData = StORMMirrorData() + for child in mirrorData.mirrorData(mirror: mirror) { + mirrorData.childs.append(StORMMirrorDataChild(label: child.0, value: child.1)) + } + return mirrorData + } + + public func mirrorData(mirror:Mirror) -> [(String,Any)] { + var c:[(String,Any)] = [] + if "\(mirror.subjectType)" == StORMIgnoreClassString { + return c + } + var count:Int = 0 + for case let (label?,value) in mirror.children { + if isFilterKey(key: label) { + continue + } + if count == 0 && isPrimaryKey(key: label) && (self.primary == nil){ + self.primary = StORMMirrorDataChild(label: label, value: value) + } else { + c.append((label,value)) + } + count += 1 + } + if let superMirror = mirror.superclassMirror, StORMIgnoreClassString.characters.count > 0 { + c.append(contentsOf: mirrorData(mirror: superMirror)) + } + return c + } + + ///Is the filter field, YES represents the filter field, and NO stands for the database table field + public func isFilterKey(key:String) -> Bool { + if key.hasPrefix("internal_") { + return true + } + if key.hasPrefix("_") { + return true + } + return false + } + + /// Is it the primary key for the database? YES stands for the primary key, and NO stands for not a primary key + public func isPrimaryKey(key:String) -> Bool { + return key.hasSuffix(StORMPrimarykeySuffix) + } +} + +public struct StORMMirrorDataChild { + var label:String + var value:Any +} From 1578afe368f2b9fc8b825338e7dc38092e99cff0 Mon Sep 17 00:00:00 2001 From: 15038777234 <15038777234@163.com> Date: Tue, 15 Aug 2017 12:54:56 +0800 Subject: [PATCH 2/5] open StORMMirrorDataChild --- Package.swift | 2 +- Sources/StORM/StORMMirrorData.swift | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Package.swift b/Package.swift index a4fc0a1..90dad82 100644 --- a/Package.swift +++ b/Package.swift @@ -9,7 +9,7 @@ import PackageDescription let package = Package( - name: "StORM", + name: "StORM-R", targets: [], dependencies: [ .Package(url: "https://github.com/PerfectlySoft/PerfectLib.git", majorVersion: 2) diff --git a/Sources/StORM/StORMMirrorData.swift b/Sources/StORM/StORMMirrorData.swift index b9e2e47..deafc56 100644 --- a/Sources/StORM/StORMMirrorData.swift +++ b/Sources/StORM/StORMMirrorData.swift @@ -39,8 +39,12 @@ public class StORMMirrorData { } count += 1 } - if let superMirror = mirror.superclassMirror, StORMIgnoreClassString.characters.count > 0 { - c.append(contentsOf: mirrorData(mirror: superMirror)) + print(StORMIgnoreClassString) + if let superMirror = mirror.superclassMirror { + if StORMIgnoreClassString.characters.count > 0 { + c.append(contentsOf: mirrorData(mirror: superMirror)) + } + } return c } @@ -63,6 +67,6 @@ public class StORMMirrorData { } public struct StORMMirrorDataChild { - var label:String - var value:Any + public var label:String + public var value:Any } From 8cd9c30e7fb38b7850697594171ce874d0f7eb29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=9B=E8=B5=8F?= Date: Tue, 15 Aug 2017 14:19:56 +0800 Subject: [PATCH 3/5] Update StORMMirrorData.swift --- Sources/StORM/StORMMirrorData.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/StORM/StORMMirrorData.swift b/Sources/StORM/StORMMirrorData.swift index deafc56..01a4a32 100644 --- a/Sources/StORM/StORMMirrorData.swift +++ b/Sources/StORM/StORMMirrorData.swift @@ -39,7 +39,6 @@ public class StORMMirrorData { } count += 1 } - print(StORMIgnoreClassString) if let superMirror = mirror.superclassMirror { if StORMIgnoreClassString.characters.count > 0 { c.append(contentsOf: mirrorData(mirror: superMirror)) From 67db7ca50cdaade6124ae05fdb98dc35fb248e0c Mon Sep 17 00:00:00 2001 From: josercc Date: Tue, 15 Aug 2017 22:56:09 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E6=97=A0=E6=B3=95=E6=9F=A5=E5=87=BA=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E4=B8=BB=E9=94=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/StORM/StORM.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sources/StORM/StORM.swift b/Sources/StORM/StORM.swift index ba62943..aec9ba5 100644 --- a/Sources/StORM/StORM.swift +++ b/Sources/StORM/StORM.swift @@ -42,6 +42,9 @@ open class StORM { let mirrorData = StORMMirrorData.mirror(mirror: mirror) for child in mirrorData.childs { c.append((child.label, type(of: child.value))) + } + if let primary = mirrorData.primary { + c.append((primary.label,primary.value)) } return c } From a28842f290f83d41888fa158b566e76e13b92165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=9B=E8=B5=8F?= Date: Wed, 16 Aug 2017 08:31:14 +0800 Subject: [PATCH 5/5] Create CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1 @@ +