Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into process-async-execute
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 authored May 24, 2018
2 parents d567c16 + 4669a89 commit 714a261
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
22 changes: 22 additions & 0 deletions Sources/Async/Worker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ extension Worker {
public var eventLoop: EventLoop {
return next()
}

/// Creates a new, succeeded `Future` from the worker's event loop.
///
/// let a: Future<String> = req.future("hello")
///
/// - parameters:
/// - value: The value that the future will wrap.
/// - returns: The succeeded future.
func future<T>(_ value: T) -> Future<T> {
return self.eventLoop.newSucceededFuture(result: value)
}

/// Creates a new, failed `Future` from the worker's event loop.
///
/// let b: Future<String> = req.future(error: Abort(...))
///
/// - parameters:
/// - error: The error that the future will wrap.
/// - returns: The failed future.
func future<T>(error: Error) -> Future<T> {
return self.eventLoop.newFailedFuture(error: error)
}
}

/// A basic `Worker` type that has a single `EventLoop`.
Expand Down
14 changes: 11 additions & 3 deletions Sources/Core/Reflectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ public protocol AnyReflectable {
/// 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.
/// - throws: Any error reflecting this type's properties.
/// - returns: All `ReflectedProperty`s at the specified depth.
static func reflectProperties(depth: Int) throws -> [ReflectedProperty]
Expand Down Expand Up @@ -158,6 +159,13 @@ public struct ReflectedProperty {
}
}

extension Collection where Element == ReflectedProperty {
/// Removes all optional properties from an array of `ReflectedProperty`.
public func optionalsRemoved() -> [ReflectedProperty] {
return filter { !($0.type is AnyOptionalType.Type) }
}
}

extension ReflectedProperty: CustomStringConvertible {
/// See `CustomStringConvertible.description`
public var description: String {
Expand Down
22 changes: 22 additions & 0 deletions Tests/CoreTests/ReflectableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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().optionalsRemoved()
XCTAssertEqual(properties.description, "[bool: Bool, int: Int, sarr: Array<String>]")

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
Expand Down Expand Up @@ -265,6 +286,7 @@ class ReflectableTests: XCTestCase {

static let allTests = [
("testStruct", testStruct),
("testNonOptionalsOnly", testNonOptionalsOnly),
("testStructCustomProperties", testStructCustomProperties),
("testNestedStruct", testNestedStruct),
("testProperties", testProperties),
Expand Down

0 comments on commit 714a261

Please sign in to comment.