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

Commit

Permalink
Merge pull request #149 from bre7/master
Browse files Browse the repository at this point in the history
Allow optionals filtering in Reflectable
  • Loading branch information
tanner0101 authored May 24, 2018
2 parents f8ae555 + adb75a3 commit 4669a89
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
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 4669a89

Please sign in to comment.