Skip to content

Instantly share code, notes, and snippets.

@wildthink
Last active May 17, 2018 09:59
Show Gist options
  • Save wildthink/853eebe653fabce9ca6e589b1e411f58 to your computer and use it in GitHub Desktop.
Save wildthink/853eebe653fabce9ca6e589b1e411f58 to your computer and use it in GitHub Desktop.
// swiftlint:disable variable_name
import Foundation
{% for type in types.based.DynamicType %}
extension {{ type.name }} {
public func waldo() -> Waldo { return _Waldo(this: self) }
class _Waldo: Waldo {
var this:{{ type.name }}
private static var type_map: [String:Any.Type] = [
{% for variable in type.variables|instance %}
"{{variable.name}}": {{variable.typeName}}.self,
{% endfor %}
]
init (this: {{ type.name }}) {
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 {
{% for variable in type.variables|instance|publicGet %}
case "{{variable.name}}": return this.{{variable.name}} as? T
{% endfor %}
default:
return value
}
}
public func set<T> (_ key: String, to value: T? = nil) {
switch key {
{% for variable in type.storedVariables|instance|publicSet %}
{% if variable.isMutable %}
case "{{variable.name}}":
{% if variable.isOptional %}
if this.{{variable.name}} is T?,
{% else %}
if this.{{variable.name}} is T,
{% endif %}
let tv = value as? {{variable.typeName}} {
this.{{variable.name}} = tv
}
{% endif %}
{% endfor %}
default:
return
}
}
}
}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment