-
Notifications
You must be signed in to change notification settings - Fork 5
feat: Add AndroidCustomIntent evaluation mode #86
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
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 |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package org.hisp.dhis.lib.expression.spi | ||
|
|
||
| import kotlin.js.JsExport | ||
|
|
||
| @JsExport | ||
| enum class AndroidCustomIntentVariable { | ||
|
Member
Author
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. I decided to create an enum just to be able to validate the expression.
Collaborator
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. Using an enum sure makes FYI: When I make this into the context key-value the key would be a wrapper on this enum or the enum itself if it can implement the interface. For the value we should talk about if you rather want a wrapper on
Member
Author
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. Makes sense. This variable name can be changed later if we find that is messing things up. The only client will probably be the Android app, unless for a while. Anyway, wouldn't be possible to create another node that uses |
||
| orgunit_code, | ||
| orgunit_id, | ||
| orgunit_path, | ||
| user_id, | ||
| user_username | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package org.hisp.dhis.lib.expression | ||
|
|
||
| import org.hisp.dhis.lib.expression.spi.ExpressionData | ||
| import org.hisp.dhis.lib.expression.spi.ParseException | ||
| import kotlin.test.Test | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertFalse | ||
| import kotlin.test.assertTrue | ||
|
|
||
| /** | ||
| * Tests that the Android Custom Intent mode | ||
| */ | ||
| internal class AndroidCustomIntentExpressionTest { | ||
|
|
||
| @Test | ||
| fun testLiteralValues() { | ||
| assertEquals("faJk7SeA5e", evaluate("'faJk7SeA5e'")) | ||
| assertEquals(5.0, evaluate("5")) | ||
| assertEquals(5.5, evaluate("5.5")) | ||
| assertEquals(true, evaluate("true")) | ||
| } | ||
|
|
||
| @Test | ||
| fun testSecondLevelOrgunit() { | ||
| val data = ExpressionData().copy( | ||
| programVariableValues = mapOf( | ||
| "orgunit_path" to "/ImspTQPwCqd/O6uvpzGd5pu/YuQRtpLP10I/g8upMTyEZGZ" | ||
| ) | ||
| ) | ||
|
|
||
| assertEquals("O6uvpzGd5pu", evaluate("d2:split(VAR{orgunit_path}, '/', 2)", data)) | ||
| } | ||
|
|
||
| @Test | ||
| fun testConcatenateUserOrgunit() { | ||
| val data = ExpressionData().copy( | ||
| programVariableValues = mapOf( | ||
| "user_username" to "admin", | ||
| "orgunit_code" to "OUC32" | ||
| ) | ||
| ) | ||
|
|
||
| assertEquals( | ||
| "admin_OUC32", | ||
| evaluate("d2:concatenate(VAR{user_username}, '_', VAR{orgunit_code})", data) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun testValidateExpression() { | ||
| validate("VAR{orgunit_code}", valid = true) | ||
| validate("VAR{invalid_var}", valid = false) | ||
| } | ||
|
|
||
| companion object { | ||
| private fun evaluate(expression: String, data: ExpressionData = ExpressionData()): Any? { | ||
| return Expression(expression, ExpressionMode.ANDROID_CUSTOM_INTENT_EXPRESSION).evaluate(data) | ||
| } | ||
|
|
||
| private fun validate(expression: String, valid: Boolean) { | ||
| try { | ||
| Expression(expression, ExpressionMode.ANDROID_CUSTOM_INTENT_EXPRESSION).validate(mapOf()) | ||
| assertTrue(valid) | ||
| } catch (e: ParseException) { | ||
| assertFalse(valid) | ||
| } | ||
| } | ||
| } | ||
| } |
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.
There are many things in the public API that must be internal, such as this enum. I didn't want to make it internal just to be consistent with the rest of the modes. @jbee should we create a task to clean this up? I can push it.
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.
Yes, new task. I think I first should also do the PR with the unified context map. Then we can do the cleanup.