Skip to content
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

Fixes KorTE's JVM ObjectMapper to not redirect to the field if the getter returned null #1407

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,21 @@ open class JvmObjectMapper2 : ObjectMapper2() {
override suspend fun set(instance: Any, key: Any?, value: Any?) {
if (instance is DynamicType<*>) return instance.dynamicShape.setProp(instance, key, value)
val prop = instance::class.classInfo.propByName[key] ?: return
if (prop.setter != null) {
prop.setter?.invoke(instance, value)
} else {
prop.field?.set(instance, value)
when {
prop.setter != null -> prop.setter.invoke(instance, value)
prop.field != null -> prop.field.set(instance, value)
else -> Unit
}
}

override suspend fun get(instance: Any, key: Any?): Any? {
if (instance is DynamicType<*>) return instance.dynamicShape.getProp(instance, key)
val prop = instance::class.classInfo.propByName[key] ?: return null
return prop.getter?.invoke(instance) ?: prop.field?.get(instance)
return when {
prop.getter != null -> prop.getter.invoke(instance)
prop.field != null -> prop.field.get(instance)
else -> null
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions korte/src/jvmTest/kotlin/com/soywiz/korte/TemplateJvmTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,21 @@ class TemplateJvmTest {
Template("{% for row in rows %}{% if row.date >= startDate && row.date < startDate.plusDays(days) %}{{ row.title }},{% endif %}{% endfor %}")("startDate" to startDate, "days" to days, "rows" to rows)
)
}

@Suppress("unused")
class Getter {
@JvmField var a: Int? = 10
@JvmField var b: Int? = 10
var c: Int? = 10
fun getA(): Int? = null
fun getB(): Int? = 20
}

@Test
fun testGetter() = suspendTest {
assertEquals(
",20,10",
Template("{{ data.a }},{{ data.b }},{{ data.c }}")("data" to Getter())
)
}
}