|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +extension String { |
| 14 | + /// Key paths can contain a `.`, but it must be escaped with a backslash `\.`. This function splits up a keypath, honoring the ability to escape a `.`. |
| 15 | + internal func escapedKeyPathSplit() -> [String] { |
| 16 | + let escapesReplaced = self.replacing("\\.", with: "A_DOT_WAS_HERE") |
| 17 | + let split = escapesReplaced.split(separator: ".", omittingEmptySubsequences: false) |
| 18 | + return split.map { $0.replacingOccurrences(of: "A_DOT_WAS_HERE", with: ".") } |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +extension [String] { |
| 23 | + /// Re-create an escaped string, if any of the components contain a `.`. |
| 24 | + internal func escapedKeyPathJoin() -> String { |
| 25 | + let comps = self.map { $0.replacingOccurrences(of: ".", with: "\\.") } |
| 26 | + let joined = comps.joined(separator: ".") |
| 27 | + return joined |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +// MARK: - Get Value at Key Path |
| 32 | + |
| 33 | +func value(atKeyPath: String, in propertyList: Any) -> Any? { |
| 34 | + let comps = atKeyPath.escapedKeyPathSplit() |
| 35 | + return _value(atKeyPath: comps, in: propertyList, remainingKeyPath: comps[comps.startIndex..<comps.endIndex]) |
| 36 | +} |
| 37 | + |
| 38 | +func _value(atKeyPath: [String], in propertyList: Any, remainingKeyPath: ArraySlice<String>) -> Any? { |
| 39 | + if remainingKeyPath.isEmpty { |
| 40 | + // We're there |
| 41 | + return propertyList |
| 42 | + } |
| 43 | + |
| 44 | + guard let key = remainingKeyPath.first, !key.isEmpty else { |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + if let dictionary = propertyList as? [String: Any] { |
| 49 | + if let dictionaryValue = dictionary[key] { |
| 50 | + return _value(atKeyPath: atKeyPath, in: dictionaryValue, remainingKeyPath: remainingKeyPath.dropFirst()) |
| 51 | + } else { |
| 52 | + return nil |
| 53 | + } |
| 54 | + } else if let array = propertyList as? [Any] { |
| 55 | + if let lastInt = Int(key), (array.startIndex..<array.endIndex).contains(lastInt) { |
| 56 | + return _value(atKeyPath: atKeyPath, in: array[lastInt], remainingKeyPath: remainingKeyPath.dropFirst()) |
| 57 | + } else { |
| 58 | + return nil |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +// MARK: - Remove Value At Key Path |
| 66 | + |
| 67 | +func removeValue(atKeyPath: String, in propertyList: Any) throws -> Any? { |
| 68 | + let comps = atKeyPath.escapedKeyPathSplit() |
| 69 | + return try _removeValue(atKeyPath: comps, in: propertyList, remainingKeyPath: comps[comps.startIndex..<comps.endIndex]) |
| 70 | +} |
| 71 | + |
| 72 | +func _removeValue(atKeyPath: [String], in propertyList: Any, remainingKeyPath: ArraySlice<String>) throws -> Any? { |
| 73 | + if remainingKeyPath.isEmpty { |
| 74 | + // We're there |
| 75 | + return nil |
| 76 | + } |
| 77 | + |
| 78 | + guard let key = remainingKeyPath.first, !key.isEmpty else { |
| 79 | + throw PLUContextError.argument("No value to remove at key path \(atKeyPath.escapedKeyPathJoin())") |
| 80 | + } |
| 81 | + |
| 82 | + if let dictionary = propertyList as? [String: Any] { |
| 83 | + guard let existing = dictionary[String(key)] else { |
| 84 | + throw PLUContextError.argument("No value to remove at key path \(atKeyPath.escapedKeyPathJoin())") |
| 85 | + } |
| 86 | + |
| 87 | + var new = dictionary |
| 88 | + if let removed = try _removeValue(atKeyPath: atKeyPath, in: existing, remainingKeyPath: remainingKeyPath.dropFirst()) { |
| 89 | + new[key] = removed |
| 90 | + } else { |
| 91 | + new.removeValue(forKey: key) |
| 92 | + } |
| 93 | + return new |
| 94 | + } else if let array = propertyList as? [Any] { |
| 95 | + guard let intKey = Int(key), (array.startIndex..<array.endIndex).contains(intKey) else { |
| 96 | + throw PLUContextError.argument("No value to remove at key path \(atKeyPath.escapedKeyPathJoin())") |
| 97 | + } |
| 98 | + |
| 99 | + let existing = array[intKey] |
| 100 | + |
| 101 | + var new = array |
| 102 | + if let removed = try _removeValue(atKeyPath: atKeyPath, in: existing, remainingKeyPath: remainingKeyPath.dropFirst()) { |
| 103 | + new[intKey] = removed |
| 104 | + } else { |
| 105 | + new.remove(at: intKey) |
| 106 | + } |
| 107 | + return new |
| 108 | + } else { |
| 109 | + // Cannot descend further into the property list, but we have keys remaining in the path |
| 110 | + throw PLUContextError.argument("No value to remove at key path \(atKeyPath.escapedKeyPathJoin())") |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// MARK: - Insert or Replace Value At Key Path |
| 115 | + |
| 116 | +func insertValue(_ value: Any, atKeyPath: String, in propertyList: Any, replacing: Bool, appending: Bool) throws -> Any { |
| 117 | + let comps = atKeyPath.escapedKeyPathSplit() |
| 118 | + return try _insertValue(value, atKeyPath: comps, in: propertyList, remainingKeyPath: comps[comps.startIndex..<comps.endIndex], replacing: replacing, appending: appending) |
| 119 | +} |
| 120 | + |
| 121 | +func _insertValue(_ value: Any, atKeyPath: [String], in propertyList: Any, remainingKeyPath: ArraySlice<String>, replacing: Bool, appending: Bool) throws -> Any { |
| 122 | + // Are we recursing further, or is this the place where we are inserting? |
| 123 | + guard let key = remainingKeyPath.first else { |
| 124 | + throw PLUContextError.argument("Key path not found \(atKeyPath.escapedKeyPathJoin())") |
| 125 | + } |
| 126 | + |
| 127 | + if let dictionary = propertyList as? [String : Any] { |
| 128 | + let existingValue = dictionary[key] |
| 129 | + if remainingKeyPath.count > 1 { |
| 130 | + // Descend |
| 131 | + if let existingValue { |
| 132 | + var new = dictionary |
| 133 | + new[key] = try _insertValue(value, atKeyPath: atKeyPath, in: existingValue, remainingKeyPath: remainingKeyPath.dropFirst(), replacing: replacing, appending: appending) |
| 134 | + return new |
| 135 | + } else { |
| 136 | + throw PLUContextError.argument("Key path not found \(atKeyPath.escapedKeyPathJoin())") |
| 137 | + } |
| 138 | + } else { |
| 139 | + // Insert |
| 140 | + if replacing { |
| 141 | + // Just slam it in |
| 142 | + var new = dictionary |
| 143 | + new[key] = value |
| 144 | + return new |
| 145 | + } else if let existingValue { |
| 146 | + if appending { |
| 147 | + if var existingValueArray = existingValue as? [Any] { |
| 148 | + existingValueArray.append(value) |
| 149 | + var new = dictionary |
| 150 | + new[key] = existingValueArray |
| 151 | + return new |
| 152 | + } else { |
| 153 | + throw PLUContextError.argument("Appending to a non-array at key path \(atKeyPath.escapedKeyPathJoin())") |
| 154 | + } |
| 155 | + } else { |
| 156 | + // Not replacing, already exists, not appending to an array |
| 157 | + throw PLUContextError.argument("Value already exists at key path \(atKeyPath.escapedKeyPathJoin())") |
| 158 | + } |
| 159 | + } else { |
| 160 | + // Still just slam it in |
| 161 | + var new = dictionary |
| 162 | + new[key] = value |
| 163 | + return new |
| 164 | + } |
| 165 | + } |
| 166 | + } else if let array = propertyList as? [Any] { |
| 167 | + guard let intKey = Int(key) else { |
| 168 | + throw PLUContextError.argument("Unable to index into array with key path \(atKeyPath.escapedKeyPathJoin())") |
| 169 | + } |
| 170 | + |
| 171 | + let containsKey = array.indices.contains(intKey) |
| 172 | + |
| 173 | + if remainingKeyPath.count > 1 { |
| 174 | + // Descend |
| 175 | + if containsKey { |
| 176 | + var new = array |
| 177 | + new[intKey] = try _insertValue(value, atKeyPath: atKeyPath, in: array[intKey], remainingKeyPath: remainingKeyPath.dropFirst(), replacing: replacing, appending: appending) |
| 178 | + return new |
| 179 | + } else { |
| 180 | + throw PLUContextError.argument("Index \(intKey) out of bounds in array at key path \(atKeyPath.escapedKeyPathJoin())") |
| 181 | + } |
| 182 | + } else { |
| 183 | + if appending { |
| 184 | + // Append to the array in this array, at this index |
| 185 | + guard let valueAtKey = array[intKey] as? [Any] else { |
| 186 | + throw PLUContextError.argument("Attempt to append value to non-array at key path \(atKeyPath.escapedKeyPathJoin())") |
| 187 | + } |
| 188 | + var new = array |
| 189 | + new[intKey] = valueAtKey + [value] |
| 190 | + return new |
| 191 | + } else if containsKey { |
| 192 | + var new = array |
| 193 | + new.insert(value, at: intKey) |
| 194 | + return new |
| 195 | + } else if intKey == array.count { |
| 196 | + // note: the value of the integer can be out of bounds for the array (== the endIndex). We treat that as an append. |
| 197 | + var new = array |
| 198 | + new.append(value) |
| 199 | + return new |
| 200 | + } else { |
| 201 | + throw PLUContextError.argument("Index \(intKey) out of bounds in array at key path \(atKeyPath.escapedKeyPathJoin())") |
| 202 | + } |
| 203 | + } |
| 204 | + } else { |
| 205 | + throw PLUContextError.argument("Unable to insert value at key path \(atKeyPath.escapedKeyPathJoin())") |
| 206 | + } |
| 207 | +} |
0 commit comments