-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KorTE: Accessing Map's declared properties #1413
Comments
Is not this line doing the trick?
|
Unfortunately, this line actually causes the problem. It doesn't allow to access dateTime property, as it tries to find key 'dateTime' in the map. Here is modified version that can clarify my point suspend fun accessAny(instance: Any?, key: Any?, mapper: ObjectMapper2): Any? {
val keyStr = DynamicContext { key.toDynamicString() }
return when {
instance == null -> null
instance is Dynamic2Gettable -> instance.dynamic2Get(key)
mapper.hasProperty(instance, keyStr) -> {
//println("Access dynamic property : $keyStr")
mapper.get(instance, key)
}
mapper.hasMethod(instance, keyStr) -> {
//println("Access dynamic method : $keyStr")
mapper.invokeAsync(instance::class as KClass<Any>, instance as Any?, keyStr, listOf())
}
instance is Map<*, *> -> instance[key]
instance is Iterable<*> -> instance.toList()[Dynamic2.toInt(key)]
else -> {
//println("Access dynamic null : '$keyStr'")
null
}
}
} More 'Kotlin' way would be to access |
But then methods available in the Map interface/implementation could be called instead. Could you consider doing something else like?: class TemplateTimeStamp(val map = LinkedHashMap<String, Any?>()) {
val time: String? by map.withDefault { null }
val date: String? by map.withDefault { null } |
I get your point, but in real implementation things are a little bit more complicated (include custom delegated properties, nested maps, participate in serialization etc.) Also, approach you suggest will not allow to access Map's keys directly. I think another solution could be moving accessAny processing to Mapper so this behavior could be overridden. BTW, thank you for great library and support |
Is it possible to access Maps' declared properties from templates ?
Imagine this class as sample
How can I get dateTime value from template?
Ref. code:
korge/korte/src/commonMain/kotlin/com/soywiz/korte/dynamic/Dynamic2.kt
Lines 156 to 178 in 122992a
The text was updated successfully, but these errors were encountered: