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

fix: use JsMap wrapper (JS component) #125

Merged
merged 1 commit into from
Apr 10, 2024
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
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) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks backwards, I*d expect k,v, is this indeed defined like this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is v, k because it is defined like that in JS Map class and the wrapper must mimic the syntax (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach#syntax).
Actually, if I change the order it doesn't compile, it is type-safe

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
values = JsMap(ruleAction.values.entries.map { e -> tupleOf(e.key, e.value) }.toTypedArray())
values = ruleAction.values

Shouldn't work like this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are different types. The type of RuleAction.values is Map, the type of RuleActionJs.values is JsMap.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course 😬

)
}

Expand Down