Skip to content

Commit

Permalink
Merge pull request #3 from adam-fowler/jmes-property-wrapper
Browse files Browse the repository at this point in the history
Add JMESPropertyWrapper protocol
  • Loading branch information
adam-fowler authored Jul 11, 2021
2 parents 64366a8 + 9afac9e commit 4a166ea
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Sources/JMESPath/Variable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import Foundation
public typealias JMESArray = [Any]
public typealias JMESObject = [String: Any]

public protocol JMESPropertyWrapper {
var anyValue: Any { get }
}

/// Internal representation of a variable
public enum JMESVariable {
case null
Expand Down Expand Up @@ -60,11 +64,15 @@ public enum JMESVariable {
default:
var object: JMESObject = [:]
for child in mirror.children {
guard let label = child.label else {
guard var label = child.label else {
self = .null
return
}
let unwrapValue = Self.unwrap(child.value) ?? NSNull()
var unwrapValue = Self.unwrap(child.value) ?? NSNull()
if let wrapper = unwrapValue as? JMESPropertyWrapper, label.first == "_" {
label = String(label.dropFirst())
unwrapValue = Self.unwrap(wrapper.anyValue) ?? NSNull()
}
object[label] = unwrapValue
}
self = .object(object)
Expand Down
21 changes: 21 additions & 0 deletions Tests/JMESPathTests/MirrorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,25 @@ final class MirrorTests: XCTestCase {
let test = TestObject(d: ["test": "one", "test2": "two", "test3": "three"])
self.testInterpreter("test2", data: test, result: "two")
}

func testPropertyWrapper() {
@propertyWrapper struct Wrap<T>: JMESPropertyWrapper {
var value: T
var customMirror: Mirror { return Mirror(reflecting: self.value) }

init(wrappedValue: T) {
self.value = wrappedValue
}
var wrappedValue: T {
get { return value }
set { value = newValue }
}
var anyValue: Any { return value }
}
struct TestObject {
@Wrap var test: String
}
let test = TestObject(test: "testText")
self.testInterpreter("test", data: test, result: "testText")
}
}

0 comments on commit 4a166ea

Please sign in to comment.