Skip to content

Commit 323f190

Browse files
authored
Renamed keypath to JSONKeyPath prevent collision. (#406)
1 parent 2f12973 commit 323f190

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

Sources/Segment/Timeline.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ extension DestinationPlugin {
268268

269269
internal func isDestinationEnabled(event: RawEvent) -> Bool {
270270
var customerDisabled = false
271-
if let disabled: Bool = event.integrations?.value(forKeyPath: KeyPath(self.key)), disabled == false {
271+
if let disabled: Bool = event.integrations?.value(forKeyPath: JSONKeyPath(self.key)), disabled == false {
272272
customerDisabled = true
273273
}
274274

Sources/Segment/Utilities/JSON.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ extension JSON {
438438
}
439439

440440
/// Directly access or set a value within the JSON object using a key path.
441-
public subscript<T: Codable>(keyPath keyPath: KeyPath) -> T? {
441+
public subscript<T: Codable>(keyPath keyPath: JSONKeyPath) -> T? {
442442
get {
443443
var result: T? = nil
444444
switch self {
@@ -494,14 +494,14 @@ extension JSON {
494494
/// - forKeyPath: The keypath within the object to retrieve. eg: `context.device.ip`
495495
///
496496
/// - Returns: The value as typed, or nil.
497-
public func value<T: Codable>(forKeyPath keyPath: KeyPath) -> T? {
497+
public func value<T: Codable>(forKeyPath keyPath: JSONKeyPath) -> T? {
498498
return self[keyPath: keyPath]
499499
}
500500

501501
/// Directly access a value within the JSON object using a key path.
502502
/// - Parameters:
503503
/// - forKeyPath: The keypath within the object to set. eg: `context.device.ip`
504-
public mutating func setValue<T: Codable>(_ value: T?, forKeyPath keyPath: KeyPath) {
504+
public mutating func setValue<T: Codable>(_ value: T?, forKeyPath keyPath: JSONKeyPath) {
505505
self[keyPath: keyPath] = value
506506
}
507507

Sources/Segment/Utilities/KeyPath.swift renamed to Sources/Segment/Utilities/JSONKeyPath.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,32 @@
88
import Foundation
99

1010
protocol 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

1515
public 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

7474
extension 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

127127
struct 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

168168
struct 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
}

Tests/Segment-Tests/KeyPath_Tests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class KeyPath_Tests: XCTestCase {
133133
"anonymousId": "123456"
134134
]
135135
for key in keys {
136-
dict[key] = mapping[keyPath: KeyPath(key), reference: event1]
136+
dict[key] = mapping[keyPath: JSONKeyPath(key), reference: event1]
137137
}
138138
XCTAssertTrue(dict["device_id"] as? String == "ABCDEF")
139139

@@ -148,7 +148,7 @@ class KeyPath_Tests: XCTestCase {
148148
]
149149
dict = [String: Any]()
150150
for key in keys {
151-
dict[key] = mapping[keyPath: KeyPath(key), reference: event2]
151+
dict[key] = mapping[keyPath: JSONKeyPath(key), reference: event2]
152152
}
153153
XCTAssertTrue(dict["device_id"] as? String == "123456")
154154
}
@@ -167,7 +167,7 @@ class KeyPath_Tests: XCTestCase {
167167
"anonymousId": "123456"
168168
]
169169
for key in keys {
170-
dict[key] = mapping[keyPath: KeyPath(key), reference: event1]
170+
dict[key] = mapping[keyPath: JSONKeyPath(key), reference: event1]
171171
}
172172
XCTAssertTrue(dict["blank_no"] as? String == "ABCDEF")
173173

@@ -177,7 +177,7 @@ class KeyPath_Tests: XCTestCase {
177177
]
178178
dict = [String: Any]()
179179
for key in keys {
180-
dict[key] = mapping[keyPath: KeyPath(key), reference: event2]
180+
dict[key] = mapping[keyPath: JSONKeyPath(key), reference: event2]
181181
}
182182
XCTAssertTrue(dict["blank_yes"] as? String == "yep")
183183
}
@@ -195,7 +195,7 @@ class KeyPath_Tests: XCTestCase {
195195
]
196196
]
197197
for key in keys {
198-
dict[key] = mapping[keyPath: KeyPath(key), reference: event1]
198+
dict[key] = mapping[keyPath: JSONKeyPath(key), reference: event1]
199199
}
200200
let traits = dict["user_properties"] as? [String: Any]
201201
XCTAssertTrue(traits?["hoot"] as? String == "nanny")

0 commit comments

Comments
 (0)