Skip to content

Commit

Permalink
fix: use JsMap wrapper (JS component) (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgarciabnz authored Apr 10, 2024
1 parent ee1bbad commit 63f89f0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
6 changes: 5 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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
Expand Down
4 changes: 2 additions & 2 deletions src/jsMain/kotlin/org/hisp/dhis/rules/RuleActionJs.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.hisp.dhis.rules

import org.hisp.dhis.lib.expression.js.Entry
import js.collections.JsMap

@JsExport
@OptIn(ExperimentalJsExport::class)
data class RuleActionJs(
val data: String?,
val type: String,
val values: Array<Entry<String, String>> = emptyArray()
val values: JsMap<String, String> = JsMap()
)
6 changes: 3 additions & 3 deletions src/jsMain/kotlin/org/hisp/dhis/rules/RuleEngineContextJs.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.hisp.dhis.rules

import org.hisp.dhis.lib.expression.js.Entry
import js.collections.JsMap

@JsExport
@OptIn(ExperimentalJsExport::class)
data class RuleEngineContextJs(
val rules: Array<RuleJs>,
val ruleVariables: Array<RuleVariableJs>,
val supplementaryData: Array<Entry<String, Array<String>>> = emptyArray(),
val constantsValues: Array<Entry<String, String>> = emptyArray()
val supplementaryData: JsMap<String, Array<String>> = JsMap(),
val constantsValues: JsMap<String, String> = JsMap()
)
13 changes: 7 additions & 6 deletions src/jsMain/kotlin/org/hisp/dhis/rules/RuleEngineJs.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.hisp.dhis.rules

import js.array.tupleOf
import js.collections.JsMap
import kotlinx.datetime.Instant
import kotlinx.datetime.LocalDate
import org.hisp.dhis.lib.expression.js.Entry
import org.hisp.dhis.rules.api.DataItem
import org.hisp.dhis.rules.api.ItemValueType
import org.hisp.dhis.rules.api.RuleEngine
Expand All @@ -12,10 +13,10 @@ import org.hisp.dhis.rules.models.*
@JsExport
@OptIn(ExperimentalJsExport::class)
class RuleEngineJs {
fun validate(expression: String, dataItemStore: Array<Entry<String, DataItemJs>>): RuleValidationResult{
fun validate(expression: String, dataItemStore: JsMap<String, DataItemJs>): RuleValidationResult{
return RuleEngine.getInstance().validate(expression, toMap(dataItemStore, {it}, ::toDataItemJava))
}
fun validateDataFieldExpression(expression: String, dataItemStore: Array<Entry<String, DataItemJs>>): RuleValidationResult{
fun validateDataFieldExpression(expression: String, dataItemStore: JsMap<String, DataItemJs>): RuleValidationResult{
return RuleEngine.getInstance().validateDataFieldExpression(expression, toMap(dataItemStore, {it}, ::toDataItemJava))
}
fun evaluateAll(enrollmentTarget: RuleEnrollmentJs?, eventsTarget: Array<RuleEventJs>, executionContext: RuleEngineContextJs): Array<RuleEffectsJs>{
Expand All @@ -38,9 +39,9 @@ class RuleEngineJs {
.map(::toRuleEffectJs).toTypedArray()
}

private fun <Kf, Vf, K, V> toMap(map: Array<Entry<Kf, Vf>>, key: (Kf) -> K, value: (Vf) -> V): Map<K, V> {
private fun <Kf, Vf, K, V> toMap(map: JsMap<Kf, Vf>, key: (Kf) -> K, value: (Vf) -> V): Map<K, V> {
val res : MutableMap<K, V> = mutableMapOf()
map.forEach { e -> res[key(e.key)] = value(e.value) }
map.forEach { v, k -> res[key(k)] = value(v) }
return res
}

Expand Down Expand Up @@ -122,7 +123,7 @@ class RuleEngineJs {
return RuleActionJs(
data = ruleAction.data,
type = ruleAction.type,
values = ruleAction.values.entries.map { e -> Entry(e.key, e.value) }.toTypedArray()
values = JsMap(ruleAction.values.entries.map { e -> tupleOf(e.key, e.value) }.toTypedArray())
)
}

Expand Down

0 comments on commit 63f89f0

Please sign in to comment.