-
Notifications
You must be signed in to change notification settings - Fork 16
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() | ||
) |
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() | ||
) |
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 | ||||||
|
@@ -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>{ | ||||||
|
@@ -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 | ||||||
} | ||||||
|
||||||
|
@@ -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()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Shouldn't work like this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They are different types. The type of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Of course 😬 |
||||||
) | ||||||
} | ||||||
|
||||||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI it is defined in this class https://github.com/JetBrains/kotlin-wrappers/blob/master/kotlin-js/src/jsMain/kotlin/js/collections/JsMap.kt