Initial POC

This commit is contained in:
Zach Eriksen 2021-03-12 08:44:23 -06:00
parent 64aa9f667e
commit ddc69bd8c8
3 changed files with 122 additions and 9 deletions

View File

@ -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)

View File

@ -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")
}
}

View File

@ -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),
]