From c3fc1b8f7659744e7637d2398bac8b37c029e1ef Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Wed, 8 Jun 2016 15:21:21 -0400 Subject: [PATCH 1/5] update to 06-06 --- .swift-version | 3 +- Package.swift | 6 ++- Sources/Fluent+C7.swift | 3 ++ Sources/Model.swift | 14 ------ Sources/Value.swift | 101 +--------------------------------------- 5 files changed, 11 insertions(+), 116 deletions(-) create mode 100644 Sources/Fluent+C7.swift diff --git a/.swift-version b/.swift-version index d4ca9281..d5989905 100644 --- a/.swift-version +++ b/.swift-version @@ -1 +1,2 @@ -3.0-preview-1-SNAPSHOT-2016-05-31-a +DEVELOPMENT-SNAPSHOT-2016-06-06-a + diff --git a/Package.swift b/Package.swift index b53360d0..f795106f 100644 --- a/Package.swift +++ b/Package.swift @@ -1,5 +1,9 @@ import PackageDescription let package = Package( - name: "Fluent" + name: "Fluent", + dependencies: [ + //Standards package. Contains protocols for cross-project compatability. + .Package(url: "https://github.com/open-swift/C7.git", majorVersion: 0, minor: 8) + ] ) diff --git a/Sources/Fluent+C7.swift b/Sources/Fluent+C7.swift new file mode 100644 index 00000000..a5f6722e --- /dev/null +++ b/Sources/Fluent+C7.swift @@ -0,0 +1,3 @@ +import C7 + +public typealias StructuredData = C7.StructuredData \ No newline at end of file diff --git a/Sources/Model.swift b/Sources/Model.swift index 66006559..ded8482d 100644 --- a/Sources/Model.swift +++ b/Sources/Model.swift @@ -121,17 +121,3 @@ extension Model { } } } - -//MARK: CustomStringConvertible - -extension Model { - public var description: String { - var readable: [String: String] = [:] - - serialize().forEach { key, val in - readable[key] = val?.string ?? "nil" - } - - return "[\(id)] \(readable)" - } -} diff --git a/Sources/Value.swift b/Sources/Value.swift index 54f0f560..28daef8b 100644 --- a/Sources/Value.swift +++ b/Sources/Value.swift @@ -2,105 +2,6 @@ A type of data that can be retrieved or stored in a database. */ -public protocol Value: CustomStringConvertible, Polymorphic { +public protocol Value: CustomStringConvertible { var structuredData: StructuredData { get } } - -public protocol Polymorphic { - var int: Int? { get } - var string: String? { get } - var double: Double? { get } -} - -public enum StructuredData { - case null - case bool(Bool) - case integer(Int) - case double(Double) - case string(String) - case array([StructuredData]) - case dictionary([String: StructuredData]) -} - -extension Value { - public var string: String? { - switch structuredData { - case .bool(let bool): - return "\(bool)" - case .integer(let int): - return "\(int)" - case .double(let double): - return "\(double)" - case .string(let string): - return "\(string)" - default: - return nil - } - } - - public var int: Int? { - switch structuredData { - case .integer(let int): - return int - case .string(let string): - return Int(string) - case .double(let double): - return Int(double) - case .bool(let bool): - return bool ? 1 : 0 - default: - return nil - } - } - - public var double: Double? { - switch structuredData { - case .double(let double): - return double - case .string(let string): - return Double(string) - case .integer(let int): - return Double(int) - case .bool(let bool): - return bool ? 1.0 : 0.0 - default: - return nil - } - } -} - -extension Value { - public var description: String { - return self.string ?? "" - } -} - -extension Int: Value { - public var structuredData: StructuredData { - return .integer(self) - } -} - -extension Double: Value { - public var structuredData: StructuredData { - return .double(self) - } -} - -extension String: Value { - public var structuredData: StructuredData { - return .string(self) - } -} - -extension Bool: Value { - public var structuredData: StructuredData { - return .bool(self) - } -} - -extension StructuredData: Value { - public var structuredData: StructuredData { - return self - } -} From b71ccdb4e39329087e09d63e814264c38ee99c78 Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Wed, 8 Jun 2016 16:24:09 -0400 Subject: [PATCH 2/5] update to use polymorphic 0.1 --- Package.swift | 5 ++++- Sources/Fluent+Polymorphic.swift | 1 + Sources/Value+Polymorphic.swift | 33 ++++++++++++++++++++++++++++++++ Sources/Value.swift | 2 +- 4 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 Sources/Fluent+Polymorphic.swift create mode 100644 Sources/Value+Polymorphic.swift diff --git a/Package.swift b/Package.swift index f795106f..5b7615f1 100644 --- a/Package.swift +++ b/Package.swift @@ -4,6 +4,9 @@ let package = Package( name: "Fluent", dependencies: [ //Standards package. Contains protocols for cross-project compatability. - .Package(url: "https://github.com/open-swift/C7.git", majorVersion: 0, minor: 8) + .Package(url: "https://github.com/open-swift/C7.git", majorVersion: 0, minor: 8), + + // Syntax for easily accessing values from generic data. + .Package(url: "https://github.com/qutheory/polymorphic.git", majorVersion: 0, minor: 1) ] ) diff --git a/Sources/Fluent+Polymorphic.swift b/Sources/Fluent+Polymorphic.swift new file mode 100644 index 00000000..08fe28ba --- /dev/null +++ b/Sources/Fluent+Polymorphic.swift @@ -0,0 +1 @@ +@_exported import Polymorphic diff --git a/Sources/Value+Polymorphic.swift b/Sources/Value+Polymorphic.swift new file mode 100644 index 00000000..a3496d1c --- /dev/null +++ b/Sources/Value+Polymorphic.swift @@ -0,0 +1,33 @@ +extension Value { + public var isNull: Bool { + return structuredData.isNull + } + + public var bool: Bool? { + return structuredData.bool + } + + public var float: Float? { + return structuredData.float + } + + public var double: Double? { + return structuredData.double + } + + public var int: Int? { + return structuredData.int + } + + public var string: String? { + return structuredData.string + } + + public var array: [Polymorphic]? { + return structuredData.array + } + + public var object: [String: Polymorphic]? { + return structuredData.object + } +} diff --git a/Sources/Value.swift b/Sources/Value.swift index 28daef8b..88e36247 100644 --- a/Sources/Value.swift +++ b/Sources/Value.swift @@ -2,6 +2,6 @@ A type of data that can be retrieved or stored in a database. */ -public protocol Value: CustomStringConvertible { +public protocol Value: CustomStringConvertible, Polymorphic { var structuredData: StructuredData { get } } From a9b8dcf894837b699ed3ded3e908c835b96482f8 Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Wed, 8 Jun 2016 16:31:52 -0400 Subject: [PATCH 3/5] private extractable --- Sources/SQL.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SQL.swift b/Sources/SQL.swift index bd1a07b2..ed8e9e85 100644 --- a/Sources/SQL.swift +++ b/Sources/SQL.swift @@ -169,7 +169,7 @@ extension Action { Allows optionals to be targeted in protocol extensions */ -public protocol Extractable { +private protocol Extractable { associatedtype Wrapped func extract() -> Wrapped? } From 514d1e9bdf5ae853327274680b439885f6cb1332 Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Wed, 8 Jun 2016 16:36:26 -0400 Subject: [PATCH 4/5] fix tests --- Sources/Model.swift | 14 ++++++++++++++ Sources/Value.swift | 28 ++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/Sources/Model.swift b/Sources/Model.swift index ded8482d..63aee4a3 100644 --- a/Sources/Model.swift +++ b/Sources/Model.swift @@ -121,3 +121,17 @@ extension Model { } } } + +//MARK: CustomStringConvertible + +extension Model { + public var description: String { + var readable: [String: String] = [:] + + serialize().forEach { key, val in + readable[key] = val?.string ?? "nil" + } + + return "[\(id)] \(readable)" + } +} \ No newline at end of file diff --git a/Sources/Value.swift b/Sources/Value.swift index 88e36247..b6f49d78 100644 --- a/Sources/Value.swift +++ b/Sources/Value.swift @@ -5,3 +5,31 @@ public protocol Value: CustomStringConvertible, Polymorphic { var structuredData: StructuredData { get } } + +extension Int: Value { + public var structuredData: StructuredData { + return .int(self) + } +} + +extension String: Value { + public var structuredData: StructuredData { + return .string(self) + } + + public var description: String { + return self + } +} + +extension Double: Value { + public var structuredData: StructuredData { + return .double(self) + } +} + +extension Float: Value { + public var structuredData: StructuredData { + return .double(Double(self)) + } +} From c5f3f9797ff3f216d8cfc9f82c245bf22f4985f1 Mon Sep 17 00:00:00 2001 From: Tanner Nelson Date: Wed, 8 Jun 2016 16:38:29 -0400 Subject: [PATCH 5/5] private func --- Sources/SQL.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SQL.swift b/Sources/SQL.swift index ed8e9e85..024ea338 100644 --- a/Sources/SQL.swift +++ b/Sources/SQL.swift @@ -178,7 +178,7 @@ private protocol Extractable { Conforms `Optional` */ extension Optional: Extractable { - public func extract() -> Wrapped? { + private func extract() -> Wrapped? { return self } }