diff --git a/README.md b/README.md index 96fa3da..cf6868b 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,39 @@ # WTV -A description of this package. +*Where's The Variable?* + + +```swift +struct Dictionary { + var value = [ + "someValue": [ + "what...?": 999 + ] + ] +} + +struct Value { + let somes = Dictionary() +} + +struct RootValue { + let child: Value = Value() +} + +struct OuterValue { + let root = RootValue() +} + +func testExample() { + guard let output = WTV(OuterValue()).variable(named: "what...?") else { + XCTFail() + return + } + + print(output) + + XCTAssert(output.contains("FOUND")) +} +``` + +> OuterValue - Inside: root - Inside: child - Inside: somes - Inside: value - Inside: someValue - Inside: someValue - Inside: what...? - FOUND: (label: Optional("what...?"), value: 999) diff --git a/Sources/WTV/WTV.swift b/Sources/WTV/WTV.swift index 0d26727..0c54580 100644 --- a/Sources/WTV/WTV.swift +++ b/Sources/WTV/WTV.swift @@ -1,3 +1,56 @@ -struct WTV { - var text = "Hello, World!" +public struct WTV { + public var value: Any + + public init(_ value: Any) { + self.value = value + } +} + +extension WTV { + public func variable(named name: String) -> String? { + let output = Mirror(reflecting: value).children + .compactMap { (label: String?, value: Any) -> String? in + variable(named: name, out: "\(type(of: self.value)) - ", child: (label, value)) + } + + guard !output.isEmpty else { + return nil + } + + return output.joined(separator: "\n") + } + + private func variable( + named name: String, + out: String?, + child: (label: String?, value: Any) + ) -> String? { + if let keyValue = child.value as? (key: AnyHashable, value: Any) { + return variable(named: name, out: (out ?? "") + "Inside: \(keyValue.key) - ", child: (keyValue.key.description, keyValue.value)) + } + + guard let childName = child.label else { + return nil + } + + guard name != childName else { + return (out ?? "-1") + "FOUND: \(child)" + } + + let children = Mirror(reflecting: child.value).children + guard !children.isEmpty else { + return nil + } + + let childrenOutput = children + .compactMap { (label: String?, value: Any) -> String? in + variable(named: name, out: (out ?? "") + "Inside: \(childName) - ", child: (label, value)) + } + + guard !childrenOutput.isEmpty else { + return nil + } + + return childrenOutput.joined(separator: "\n") + } } diff --git a/Tests/WTVTests/WTVTests.swift b/Tests/WTVTests/WTVTests.swift index e314f5d..1a774b9 100644 --- a/Tests/WTVTests/WTVTests.swift +++ b/Tests/WTVTests/WTVTests.swift @@ -2,13 +2,37 @@ import XCTest @testable import WTV final class WTVTests: XCTestCase { - func testExample() { - // This is an example of a functional test case. - // Use XCTAssert and related functions to verify your tests produce the correct - // results. - XCTAssertEqual(WTV().text, "Hello, World!") + struct Dictionary { + var value = [ + "someValue": [ + "what...?": 999 + ] + ] } - + + struct Value { + let somes = Dictionary() + } + + struct RootValue { + let child: Value = Value() + } + + struct OuterValue { + let root = RootValue() + } + + func testExample() { + guard let output = WTV(OuterValue()).variable(named: "what...?") else { + XCTFail() + return + } + + print(output) + + XCTAssert(output.contains("FOUND")) + } + static var allTests = [ ("testExample", testExample), ]