88import Foundation
99
1010protocol KeyPathHandler {
11- func isHandled( _ keyPath: KeyPath , forInput: Any ? ) -> Bool
12- func value( keyPath: KeyPath , input: Any ? , reference: Any ? ) -> Any ?
11+ func isHandled( _ keyPath: JSONKeyPath , forInput: Any ? ) -> Bool
12+ func value( keyPath: JSONKeyPath , input: Any ? , reference: Any ? ) -> Any ?
1313}
1414
1515public struct BasicHandler : KeyPathHandler {
16- func value( keyPath: KeyPath , input: Any ? , reference: Any ? ) -> Any ? {
16+ func value( keyPath: JSONKeyPath , input: Any ? , reference: Any ? ) -> Any ? {
1717 guard let input = input as? [ String : Any ] else { return nil }
1818 var result : Any ? = nil
1919 if keyPath. remaining. isEmpty {
2020 result = input [ keyPath. current]
2121 } else {
2222 if let nestedDict = input [ keyPath. current] as? [ String : Any ] {
23- result = nestedDict [ keyPath: KeyPath ( keyPath. remainingPath) ]
23+ result = nestedDict [ keyPath: JSONKeyPath ( keyPath. remainingPath) ]
2424 } else {
2525 result = nil
2626 }
2727 }
2828 return result
2929 }
3030
31- func isHandled( _ keyPath: KeyPath , forInput: Any ? ) -> Bool {
31+ func isHandled( _ keyPath: JSONKeyPath , forInput: Any ? ) -> Bool {
3232 return true
3333 }
3434}
3535
36- public struct KeyPath {
36+ public struct JSONKeyPath {
3737 var current : String
3838 var remaining : [ String ]
3939
@@ -47,7 +47,7 @@ public struct KeyPath {
4747
4848 internal static var handlers : [ KeyPathHandler ] = [ PathHandler ( ) , IfHandler ( ) , BasicHandler ( ) ]
4949 static func register( _ handler: KeyPathHandler ) { handlers. insert ( handler, at: 0 ) }
50- static func handlerFor( keyPath: KeyPath , input: Any ? ) -> KeyPathHandler ? {
50+ static func handlerFor( keyPath: JSONKeyPath , input: Any ? ) -> KeyPathHandler ? {
5151 guard let input = input as? [ String : Any ] else { return nil }
5252 for item in handlers {
5353 if item. isHandled ( keyPath, forInput: input [ keyPath. current] ) {
@@ -59,7 +59,7 @@ public struct KeyPath {
5959}
6060
6161
62- extension KeyPath : ExpressibleByStringLiteral {
62+ extension JSONKeyPath : ExpressibleByStringLiteral {
6363 public init ( stringLiteral value: String ) {
6464 self . init ( value)
6565 }
@@ -72,13 +72,13 @@ extension KeyPath: ExpressibleByStringLiteral {
7272}
7373
7474extension Dictionary where Key: StringProtocol , Value: Any {
75- internal func value( keyPath: KeyPath , reference: Any ? ) -> Any ? {
76- let handler = KeyPath . handlerFor ( keyPath: keyPath, input: self )
75+ internal func value( keyPath: JSONKeyPath , reference: Any ? ) -> Any ? {
76+ let handler = JSONKeyPath . handlerFor ( keyPath: keyPath, input: self )
7777 let result = handler? . value ( keyPath: keyPath, input: self , reference: reference)
7878 return result
7979 }
8080
81- internal mutating func setValue( _ value: Any ? , keyPath: KeyPath ) {
81+ internal mutating func setValue( _ value: Any ? , keyPath: JSONKeyPath ) {
8282 guard let key = keyPath. current as? Key else { return }
8383
8484 if keyPath. remaining. isEmpty {
@@ -89,28 +89,28 @@ extension Dictionary where Key: StringProtocol, Value: Any {
8989 }
9090 } else {
9191 if var nestedDict = self [ key] as? [ String : Any ] {
92- nestedDict [ keyPath: KeyPath ( keyPath. remainingPath) ] = value
92+ nestedDict [ keyPath: JSONKeyPath ( keyPath. remainingPath) ] = value
9393 self [ key] = ( nestedDict as! Value )
9494 } else {
9595 // this nested key doesn't exist but we're not at the end of the chain, need to create it
9696 var nestedDict = [ String: Any] ( )
97- nestedDict [ keyPath: KeyPath ( keyPath. remainingPath) ] = value
97+ nestedDict [ keyPath: JSONKeyPath ( keyPath. remainingPath) ] = value
9898 self [ key] = ( nestedDict as! Value )
9999 }
100100 }
101101 }
102102
103- public subscript( keyPath keyPath: KeyPath ) -> Any ? {
103+ public subscript( keyPath keyPath: JSONKeyPath ) -> Any ? {
104104 get { return value ( keyPath: keyPath, reference: nil ) }
105105 set { setValue ( newValue as Any , keyPath: keyPath) }
106106 }
107107
108- public subscript( keyPath keyPath: KeyPath , reference reference: Any ? ) -> Any ? {
108+ public subscript( keyPath keyPath: JSONKeyPath , reference reference: Any ? ) -> Any ? {
109109 get { return value ( keyPath: keyPath, reference: reference) }
110110 set { setValue ( newValue as Any , keyPath: keyPath) }
111111 }
112112
113- public func exists( keyPath: KeyPath , reference: Any ? = nil ) -> Bool {
113+ public func exists( keyPath: JSONKeyPath , reference: Any ? = nil ) -> Bool {
114114 return ( value ( keyPath: keyPath, reference: reference) != nil )
115115 }
116116}
@@ -125,13 +125,13 @@ extension String {
125125
126126
127127struct IfHandler : KeyPathHandler {
128- func isHandled( _ keyPath: KeyPath , forInput: Any ? ) -> Bool {
128+ func isHandled( _ keyPath: JSONKeyPath , forInput: Any ? ) -> Bool {
129129 guard let input = forInput as? [ String : Any ] else { return false }
130130 if input [ " @if " ] != nil { return true }
131131 return false
132132 }
133133
134- func value( keyPath: KeyPath , input: Any ? , reference: Any ? ) -> Any ? {
134+ func value( keyPath: JSONKeyPath , input: Any ? , reference: Any ? ) -> Any ? {
135135 guard let input = input as? [ String : Any ] else { return nil }
136136 let current = input [ keyPath. current] as? [ String : Any ]
137137 let conditional = current ? [ " @if " ] as? [ String : Any ]
@@ -166,22 +166,22 @@ struct IfHandler: KeyPathHandler {
166166}
167167
168168struct PathHandler : KeyPathHandler {
169- func isHandled( _ keyPath: KeyPath , forInput: Any ? ) -> Bool {
169+ func isHandled( _ keyPath: JSONKeyPath , forInput: Any ? ) -> Bool {
170170 guard let input = forInput as? [ String : Any ] else { return false }
171171 if input [ " @path " ] != nil {
172172 return true
173173 }
174174 return false
175175 }
176176
177- func value( keyPath: KeyPath , input: Any ? , reference: Any ? ) -> Any ? {
177+ func value( keyPath: JSONKeyPath , input: Any ? , reference: Any ? ) -> Any ? {
178178 guard let input = input as? [ String : Any ] else { return nil }
179179 let current = input [ keyPath. current] as? [ String : Any ]
180180 let path = ( current ? [ " @path " ] as? String ) ? . strippedReference
181181
182182 var result : Any ? = nil
183183 if let path = path, let reference = reference as? [ String : Any ] {
184- result = reference [ keyPath: KeyPath ( path) ]
184+ result = reference [ keyPath: JSONKeyPath ( path) ]
185185 }
186186 return result
187187 }
0 commit comments