From 5e6003dd00d357bf771bf61853edbb30f8120031 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Tue, 22 May 2018 16:50:43 -0500 Subject: [PATCH 01/11] Created Worker.succeeded(_:) and .failed(_:) methods --- Sources/Async/Worker.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Sources/Async/Worker.swift b/Sources/Async/Worker.swift index 1888d54c..e73f718e 100644 --- a/Sources/Async/Worker.swift +++ b/Sources/Async/Worker.swift @@ -41,6 +41,24 @@ extension Worker { public var eventLoop: EventLoop { return next() } + + /// Creates a new, succeeded `Future` from the worker's event loop. + /// + /// - Parameter value: The value that the future will wrap. + /// + /// - Returns: The succeeded future. + func succeeded(_ value: T) -> Future { + return self.eventLoop.newSucceededFuture(result: value) + } + + /// Creates a new, failed `Future` from the worker's event loop. + /// + /// - Parameter error: The error that the future will wrap. + /// + /// - Returns: The failed future. + func failed(_ error: Error) -> Future { + return self.eventLoop.newFailedFuture(error: error) + } } /// A basic `Worker` type that has a single `EventLoop`. From 61344521a1257c374f2d15949e95e1785c78ae05 Mon Sep 17 00:00:00 2001 From: bre7 Date: Tue, 22 May 2018 20:31:09 -0300 Subject: [PATCH 02/11] Added flag to indicate whether optionals should be returned or not --- Sources/Core/Reflectable.swift | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/Sources/Core/Reflectable.swift b/Sources/Core/Reflectable.swift index 1d484c5f..39ada2ab 100644 --- a/Sources/Core/Reflectable.swift +++ b/Sources/Core/Reflectable.swift @@ -63,12 +63,14 @@ public protocol Reflectable { /// try User.reflectProperties(depth: 0) // [id: UUID?, name: String, pet: Pet] /// try User.reflectProperties(depth: 1) // [pet.name: String, pet.age: Int] /// - /// - parameters: depth: The level of nesting to use. - /// If `0`, the top-most properties will be returned. - /// If `1`, the first layer of nested properties, and so-on. + /// - parameters: + /// - depth: The level of nesting to use. + /// If `0`, the top-most properties will be returned. + /// If `1`, the first layer of nested properties, and so-on. + /// - includeOptionals: Whether Optional properties should be included or not. /// - throws: Any error reflecting this type's properties. /// - returns: All `ReflectedProperty`s at the specified depth. - static func reflectProperties(depth: Int) throws -> [ReflectedProperty] + static func reflectProperties(depth: Int, includeOptionals: Bool) throws -> [ReflectedProperty] /// Returns a `ReflectedProperty` for the supplied key path. /// @@ -95,8 +97,14 @@ public protocol Reflectable { extension Reflectable { /// Reflects all of this type's `ReflectedProperty`s. - public static func reflectProperties() throws -> [ReflectedProperty] { - return try reflectProperties(depth: 0) + public static func reflectProperties(includeOptionals: Bool = true) throws -> [ReflectedProperty] { + if includeOptionals { + return try reflectProperties(depth: 0) + } else { + return try reflectProperties(depth: 0) + // remove optionals + .filter({ !($0.type is AnyOptionalType.Type) }) + } } } From 47da286178353dd23e481b45526e7ba145806a3e Mon Sep 17 00:00:00 2001 From: bre7 Date: Tue, 22 May 2018 20:39:23 -0300 Subject: [PATCH 03/11] Moved to extension --- Sources/Core/Reflectable.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/Core/Reflectable.swift b/Sources/Core/Reflectable.swift index 39ada2ab..500a8bf0 100644 --- a/Sources/Core/Reflectable.swift +++ b/Sources/Core/Reflectable.swift @@ -67,10 +67,9 @@ public protocol Reflectable { /// - depth: The level of nesting to use. /// If `0`, the top-most properties will be returned. /// If `1`, the first layer of nested properties, and so-on. - /// - includeOptionals: Whether Optional properties should be included or not. /// - throws: Any error reflecting this type's properties. /// - returns: All `ReflectedProperty`s at the specified depth. - static func reflectProperties(depth: Int, includeOptionals: Bool) throws -> [ReflectedProperty] + static func reflectProperties(depth: Int) throws -> [ReflectedProperty] /// Returns a `ReflectedProperty` for the supplied key path. /// @@ -97,6 +96,8 @@ public protocol Reflectable { extension Reflectable { /// Reflects all of this type's `ReflectedProperty`s. + /// - parameters: + /// - includeOptionals: Whether Optional properties should be included or not. public static func reflectProperties(includeOptionals: Bool = true) throws -> [ReflectedProperty] { if includeOptionals { return try reflectProperties(depth: 0) From 07bd704f8c05c8c64a09fd445bca559785d533d8 Mon Sep 17 00:00:00 2001 From: bre7 Date: Tue, 22 May 2018 20:39:30 -0300 Subject: [PATCH 04/11] Added test case --- Tests/CoreTests/ReflectableTests.swift | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Tests/CoreTests/ReflectableTests.swift b/Tests/CoreTests/ReflectableTests.swift index 70282623..3760ad4b 100644 --- a/Tests/CoreTests/ReflectableTests.swift +++ b/Tests/CoreTests/ReflectableTests.swift @@ -52,6 +52,27 @@ class ReflectableTests: XCTestCase { try XCTAssert(Foo.reflectProperty(forKey: \.odir)?.type is Direction?.Type) } + func testNonOptionalsOnly() throws { + struct Foo: Reflectable, Decodable { + var bool: Bool + var obool: Bool? + var int: Int + var oint: Int? + var sarr: [String] + var osarr: [String]? + } + + let properties = try Foo.reflectProperties(includeOptionals: false) + XCTAssertEqual(properties.description, "[bool: Bool, int: Int, sarr: Array]") + + try XCTAssertEqual(Foo.reflectProperty(forKey: \.bool)?.path, ["bool"]) + try XCTAssert(Foo.reflectProperty(forKey: \.bool)?.type is Bool.Type) + try XCTAssertEqual(Foo.reflectProperty(forKey: \.int)?.path, ["int"]) + try XCTAssert(Foo.reflectProperty(forKey: \.int)?.type is Int.Type) + try XCTAssertEqual(Foo.reflectProperty(forKey: \.sarr)?.path, ["sarr"]) + try XCTAssert(Foo.reflectProperty(forKey: \.sarr)?.type is [String].Type) + } + func testStructCustomProperties() throws { struct User: Reflectable { var firstName: String @@ -265,6 +286,7 @@ class ReflectableTests: XCTestCase { static let allTests = [ ("testStruct", testStruct), + ("testNonOptionalsOnly", testNonOptionalsOnly), ("testStructCustomProperties", testStructCustomProperties), ("testNestedStruct", testNestedStruct), ("testProperties", testProperties), From 7e31d0eab0d093020a2b1ad2ae60f405aa3d9869 Mon Sep 17 00:00:00 2001 From: Nathan Date: Tue, 22 May 2018 20:46:30 -0300 Subject: [PATCH 05/11] Style fix --- Sources/Core/Reflectable.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Core/Reflectable.swift b/Sources/Core/Reflectable.swift index 500a8bf0..6f94c9bd 100644 --- a/Sources/Core/Reflectable.swift +++ b/Sources/Core/Reflectable.swift @@ -103,7 +103,7 @@ extension Reflectable { return try reflectProperties(depth: 0) } else { return try reflectProperties(depth: 0) - // remove optionals + /// remove optionals .filter({ !($0.type is AnyOptionalType.Type) }) } } From a75514147429697277d7e33e855ce2e8b49ddbab Mon Sep 17 00:00:00 2001 From: Tanner Date: Thu, 24 May 2018 11:37:46 -0400 Subject: [PATCH 06/11] Update Reflectable.swift --- Sources/Core/Reflectable.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Core/Reflectable.swift b/Sources/Core/Reflectable.swift index 75285a58..27c0b305 100644 --- a/Sources/Core/Reflectable.swift +++ b/Sources/Core/Reflectable.swift @@ -103,8 +103,8 @@ public protocol AnyReflectable { /// /// - parameters: /// - depth: The level of nesting to use. - /// If `0`, the top-most properties will be returned. - /// If `1`, the first layer of nested properties, and so-on. + /// If `0`, the top-most properties will be returned. + /// If `1`, the first layer of nested properties, and so-on. /// - throws: Any error reflecting this type's properties. /// - returns: All `ReflectedProperty`s at the specified depth. static func reflectProperties(depth: Int) throws -> [ReflectedProperty] From 1f8cf27b686dc907550c16ecfb2538f7d81d328d Mon Sep 17 00:00:00 2001 From: Tanner Date: Thu, 24 May 2018 11:38:23 -0400 Subject: [PATCH 07/11] Update ReflectableTests.swift --- Tests/CoreTests/ReflectableTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/CoreTests/ReflectableTests.swift b/Tests/CoreTests/ReflectableTests.swift index 5b86aaaa..cd0fed0e 100644 --- a/Tests/CoreTests/ReflectableTests.swift +++ b/Tests/CoreTests/ReflectableTests.swift @@ -62,7 +62,7 @@ class ReflectableTests: XCTestCase { var osarr: [String]? } - let properties = try Foo.reflectProperties(includeOptionals: false) + let properties = try Foo.reflectProperties().optionalsRemoved() XCTAssertEqual(properties.description, "[bool: Bool, int: Int, sarr: Array]") try XCTAssertEqual(Foo.reflectProperty(forKey: \.bool)?.path, ["bool"]) From fa5054a4d139476173a3c3fbb5cfa7a35665a701 Mon Sep 17 00:00:00 2001 From: Tanner Date: Thu, 24 May 2018 11:38:55 -0400 Subject: [PATCH 08/11] remove tab --- Sources/Core/Reflectable.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Core/Reflectable.swift b/Sources/Core/Reflectable.swift index 27c0b305..c2f0da2d 100644 --- a/Sources/Core/Reflectable.swift +++ b/Sources/Core/Reflectable.swift @@ -102,7 +102,7 @@ public protocol AnyReflectable { /// try User.reflectProperties(depth: 1) // [pet.name: String, pet.age: Int] /// /// - parameters: - /// - depth: The level of nesting to use. + /// - depth: The level of nesting to use. /// If `0`, the top-most properties will be returned. /// If `1`, the first layer of nested properties, and so-on. /// - throws: Any error reflecting this type's properties. From adb75a329e16814276c0bee23b1fa8f8fd744c73 Mon Sep 17 00:00:00 2001 From: Tanner Date: Thu, 24 May 2018 11:46:48 -0400 Subject: [PATCH 09/11] remove extraneous reflectable method --- Sources/Core/Reflectable.swift | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Sources/Core/Reflectable.swift b/Sources/Core/Reflectable.swift index c2f0da2d..744d36ce 100644 --- a/Sources/Core/Reflectable.swift +++ b/Sources/Core/Reflectable.swift @@ -134,13 +134,6 @@ public protocol AnyReflectable { static func anyReflectProperty(valueType: Any.Type, keyPath: AnyKeyPath) throws -> ReflectedProperty? } -extension Reflectable { - /// Reflects all of this type's `ReflectedProperty`s. - public static func reflectProperties() throws -> [ReflectedProperty] { - return try reflectProperties(depth: 0) - } -} - /// Represents a property on a type that has been reflected using the `Reflectable` protocol. /// /// let property = try User.reflectProperty(forKey: \.pet.name) From d0ec2dd88aedcad39de3491f31ff26d9a1c85ec9 Mon Sep 17 00:00:00 2001 From: Caleb Kleveter Date: Thu, 24 May 2018 11:13:52 -0500 Subject: [PATCH 10/11] Renamed Worker.succeeded(_:) and .failed(_:) to .future(_:) and .future(error:) --- Sources/Async/Worker.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Async/Worker.swift b/Sources/Async/Worker.swift index e73f718e..9d557493 100644 --- a/Sources/Async/Worker.swift +++ b/Sources/Async/Worker.swift @@ -47,7 +47,7 @@ extension Worker { /// - Parameter value: The value that the future will wrap. /// /// - Returns: The succeeded future. - func succeeded(_ value: T) -> Future { + func future(_ value: T) -> Future { return self.eventLoop.newSucceededFuture(result: value) } @@ -56,7 +56,7 @@ extension Worker { /// - Parameter error: The error that the future will wrap. /// /// - Returns: The failed future. - func failed(_ error: Error) -> Future { + func future(error: Error) -> Future { return self.eventLoop.newFailedFuture(error: error) } } From 2a53578fba7a783d57337152a3e2491aa4b28303 Mon Sep 17 00:00:00 2001 From: Tanner Date: Thu, 24 May 2018 12:27:33 -0400 Subject: [PATCH 11/11] update comment formatting + add examples --- Sources/Async/Worker.swift | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Sources/Async/Worker.swift b/Sources/Async/Worker.swift index 9d557493..77d707b1 100644 --- a/Sources/Async/Worker.swift +++ b/Sources/Async/Worker.swift @@ -44,18 +44,22 @@ extension Worker { /// Creates a new, succeeded `Future` from the worker's event loop. /// - /// - Parameter value: The value that the future will wrap. + /// let a: Future = req.future("hello") /// - /// - Returns: The succeeded future. + /// - parameters: + /// - value: The value that the future will wrap. + /// - returns: The succeeded future. func future(_ value: T) -> Future { return self.eventLoop.newSucceededFuture(result: value) } /// Creates a new, failed `Future` from the worker's event loop. /// - /// - Parameter error: The error that the future will wrap. + /// let b: Future = req.future(error: Abort(...)) /// - /// - Returns: The failed future. + /// - parameters: + /// - error: The error that the future will wrap. + /// - returns: The failed future. func future(error: Error) -> Future { return self.eventLoop.newFailedFuture(error: error) }