From 86de475391ba9529c4db75f07caee538fe7b2bf9 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 5 Aug 2017 13:31:09 -0400 Subject: [PATCH] Add method for passing objects with generic type params --- src/main/kotlin/net/shadowfacts/ekt/EKT.kt | 12 ++++++++++-- src/test/kotlin/net/shadowfacts/ekt/Test.kt | 4 +--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/net/shadowfacts/ekt/EKT.kt b/src/main/kotlin/net/shadowfacts/ekt/EKT.kt index cd22154..28f5b1f 100644 --- a/src/main/kotlin/net/shadowfacts/ekt/EKT.kt +++ b/src/main/kotlin/net/shadowfacts/ekt/EKT.kt @@ -78,12 +78,20 @@ _result.toString() // Hack to allow data to be accessed by name from template instead of via bindings map val unwrapBindings = data.keys.map { - val type = data[it]!!::class.qualifiedName - "val $it = bindings[\"$it\"] as $type;" + val value = data[it]!! + if (value is Value) { + val type = value.type + "val $it = (bindings[\"$it\"] as net.shadowfacts.ekt.EKT.Value).value as $type" + } else { + val type = value::class.qualifiedName + "val $it = bindings[\"$it\"] as $type" + } }.joinToString("\n") engine.eval(unwrapBindings) return engine.eval(script) } + data class Value(val value: Any, val type: String) + } \ No newline at end of file diff --git a/src/test/kotlin/net/shadowfacts/ekt/Test.kt b/src/test/kotlin/net/shadowfacts/ekt/Test.kt index 65ede03..a8bd0f0 100644 --- a/src/test/kotlin/net/shadowfacts/ekt/Test.kt +++ b/src/test/kotlin/net/shadowfacts/ekt/Test.kt @@ -7,9 +7,7 @@ import java.io.File */ fun main(args: Array) { val res = EKT.render(File("template.ekt"), mapOf( - "title" to "EKT Test", - "body" to "some test content", - "value" to 42 + "list" to EKT.Value(listOf(1, 2, 3), "List") ), dumpGeneratedScript = File("script.kts")) File("result.txt").apply {