From fff002a0bf2b7f3462756a703882f60c9c83e2ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Garc=C3=ADa?= Date: Wed, 10 Apr 2024 11:16:50 +0200 Subject: [PATCH] fix: use JsMap wrapper (JS component) (#52) --- build.gradle.kts | 6 +++++- .../kotlin/org.hisp.dhis.lib.expression.js/Entry.kt | 5 ----- .../ExpressionDataJs.kt | 12 +++++++----- .../org.hisp.dhis.lib.expression.js/ExpressionJs.kt | 11 ++++++----- 4 files changed, 18 insertions(+), 16 deletions(-) delete mode 100644 src/jsMain/kotlin/org.hisp.dhis.lib.expression.js/Entry.kt diff --git a/build.gradle.kts b/build.gradle.kts index b219a97..94880c7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -75,7 +75,11 @@ kotlin { } val jvmMain by getting val jvmTest by getting - val jsMain by getting + val jsMain by getting { + dependencies { + api("org.jetbrains.kotlin-wrappers:kotlin-js:1.0.0-pre.722") + } + } val jsTest by getting val nativeMain by getting val nativeTest by getting diff --git a/src/jsMain/kotlin/org.hisp.dhis.lib.expression.js/Entry.kt b/src/jsMain/kotlin/org.hisp.dhis.lib.expression.js/Entry.kt deleted file mode 100644 index 022c238..0000000 --- a/src/jsMain/kotlin/org.hisp.dhis.lib.expression.js/Entry.kt +++ /dev/null @@ -1,5 +0,0 @@ -package org.hisp.dhis.lib.expression.js - -@OptIn(ExperimentalJsExport::class) -@JsExport -data class Entry(val key: K, val value: V) \ No newline at end of file diff --git a/src/jsMain/kotlin/org.hisp.dhis.lib.expression.js/ExpressionDataJs.kt b/src/jsMain/kotlin/org.hisp.dhis.lib.expression.js/ExpressionDataJs.kt index 5c9149e..befb9c2 100644 --- a/src/jsMain/kotlin/org.hisp.dhis.lib.expression.js/ExpressionDataJs.kt +++ b/src/jsMain/kotlin/org.hisp.dhis.lib.expression.js/ExpressionDataJs.kt @@ -1,11 +1,13 @@ package org.hisp.dhis.lib.expression.js +import js.collections.JsMap + @OptIn(ExperimentalJsExport::class) @JsExport data class ExpressionDataJs( - val programRuleVariableValues: Array> = emptyArray(), - val programVariableValues: Array> = emptyArray(), - val supplementaryValues: Array>> = emptyArray(), - val dataItemValues: Array> = emptyArray(), - val namedValues: Array> = emptyArray() + val programRuleVariableValues: JsMap = JsMap(), + val programVariableValues: JsMap = JsMap(), + val supplementaryValues: JsMap> = JsMap(), + val dataItemValues: JsMap = JsMap(), + val namedValues: JsMap = JsMap() ) \ No newline at end of file diff --git a/src/jsMain/kotlin/org.hisp.dhis.lib.expression.js/ExpressionJs.kt b/src/jsMain/kotlin/org.hisp.dhis.lib.expression.js/ExpressionJs.kt index f57be08..12c82b8 100644 --- a/src/jsMain/kotlin/org.hisp.dhis.lib.expression.js/ExpressionJs.kt +++ b/src/jsMain/kotlin/org.hisp.dhis.lib.expression.js/ExpressionJs.kt @@ -1,5 +1,6 @@ package org.hisp.dhis.lib.expression.js +import js.collections.JsMap import kotlinx.datetime.LocalDate import org.hisp.dhis.lib.expression.Expression import org.hisp.dhis.lib.expression.ast.AggregationType @@ -41,11 +42,11 @@ class ExpressionJs(expression: String, mode: String) { return expr.collectUIDs().map(::toIdJS).toTypedArray() } - fun describe(displayNames: Array>): String { + fun describe(displayNames: JsMap): String { return expr.describe(toMap(displayNames, { it }, { it })) } - fun validate(displayNamesKeys: Array>) { + fun validate(displayNamesKeys: JsMap) { expr.validate(toMap(displayNamesKeys, { it }, { e -> ValueType.valueOf(e) })) } @@ -53,7 +54,7 @@ class ExpressionJs(expression: String, mode: String) { return expr.collectDataItemForRegenerate().map(::toDataItemJS).toTypedArray() } - fun regenerate(dataItemValues: Array>): String { + fun regenerate(dataItemValues: JsMap): String { return expr.regenerate(toMap(dataItemValues, ::toDataItemJava) { it }) } @@ -68,9 +69,9 @@ class ExpressionJs(expression: String, mode: String) { companion object { val MODES = Expression.Mode.entries.map { it.name }.toTypedArray() - internal fun toMap(map: Array>, key: (Kf) -> K, value: (Vf) -> V): Map { + internal fun toMap(map: JsMap, key: (Kf) -> K, value: (Vf) -> V): Map { val res : MutableMap = mutableMapOf() - map.forEach { e -> res[key(e.key)] = value(e.value) } + map.forEach { v, k -> res[key(k)] = value(v) } return res; }