Skip to content

Instantly share code, notes, and snippets.

@wildthink
Created May 17, 2018 10:01
Show Gist options
  • Save wildthink/8635222bae4edf76a53fd5ee6164bdda to your computer and use it in GitHub Desktop.
Save wildthink/8635222bae4edf76a53fd5ee6164bdda to your computer and use it in GitHub Desktop.
extension Person {
public fun waldo() -> Waldo { return _Waldo(this: self) }
class _Waldo: Waldo {
var this:Person
private static var type_map: [String:Any.Type] = [
"name": String.self,
"birthday": Date.self,
"age": Int.self,
]
init (this: Person) {
self.this = this
}
public func type (for key: String) -> Any.Type? {
return _Waldo.type_map[key]
}
public func get<T> (for key: String, default value: T? = nil) -> T? {
switch key {
case "name": return this.name as? T
case "birthday": return this.birthday as? T
case "age": return this.age as? T
default:
return value
}
}
public func set<T> (_ key: String, to value: T? = nil) {
switch key {
case "name":
if this.name is T,
let tv = value as? String {
this.name = tv
}
case "birthday":
if this.birthday is T,
let tv = value as? Date {
this.birthday = tv
}
default:
return
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment