Skip to content

Instantly share code, notes, and snippets.

@aainaj
Last active December 15, 2021 21:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aainaj/b4d2048effc5c1e18a4a783ee73849f8 to your computer and use it in GitHub Desktop.
Save aainaj/b4d2048effc5c1e18a4a783ee73849f8 to your computer and use it in GitHub Desktop.
KeyPathEditable Complete Source code including consumption
enum KeyPathError: Error {
case unableToCast(String)
}
protocol KeyPathEditable {
func update<KeyPathType>(path: PartialKeyPath<Self>, to value: KeyPathType) throws -> Self
}
extension KeyPathEditable {
func update<KeyPathType>(path: PartialKeyPath<Self>, to value: KeyPathType) throws -> Self {
guard let writableKeyPath = path as? WritableKeyPath<Self, KeyPathType> else {
throw KeyPathError.unableToCast("because of passed \(value) value data type")
}
var copy = self
copy[keyPath: writableKeyPath] = value
return copy
}
}
struct User: KeyPathEditable {
typealias Root = User
private(set) var name: String
private(set) var age: Int
private(set) var property: Property
}
struct Property: KeyPathEditable {
private(set) var areaName: String
private(set) var size: String
private(set) var name: String
}
let property = Property(areaName: "Sector 14", size: "2400 sq ft", name: "Mark villa")
let updatedProperty = try? property.update(path: \Property.areaName, to: "Bangalore")
print(updatedProperty?.areaName ?? property.areaName)
let user = User(name: "Me", age: 16, property: Property(areaName: "Sector 14", size: "2400 sq ft", name: "Mark villa"))
do {
let latestData = try user.update(path: \User.property, to: Property(areaName: "Sector 4", size: "1200 sq ft", name: "Mark villa"))
print(latestData.property)
} catch let error {
print(error)
}
let latestUserData = try? user.update(path: \User.property.name, to: "Euphoria")
print(latestUserData?.property.name ?? user.property.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment