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

Add JEXL evaluator access from Swift and Kotlin #4813

Merged
merged 8 commits into from
Feb 3, 2022

Conversation

jhugman
Copy link
Contributor

@jhugman jhugman commented Feb 1, 2022

Fixes EXP-2158.

This PR refactors the targeting infrastructure to allow JEXL to be available for external applications.

This is relatively specialised: it allows evaluation against the targeting attributes Nimbus uses, and allows a JSON object/Codable to be add extra variables.

Pull Request checklist

  • Quality: This PR builds and tests run cleanly
    • Note:
      • For changes that need extra cross-platform testing, consider adding [ci full] to the PR title.
      • If this pull request includes a breaking change, consider cutting a new release after merging.
  • Tests: This PR includes thorough tests or an explanation of why it does not
  • Changelog: This PR includes a changelog entry in CHANGES_UNRELEASED.md or an explanation of why it does not need one
    • Any breaking changes to Swift or Kotlin binding APIs are noted explicitly
  • Dependencies: This PR follows our dependency management guidelines
    • Any new dependencies are accompanied by a summary of the due dilligence applied in selecting them.

@codecov-commenter
Copy link

Codecov Report

Merging #4813 (a587631) into main (2e88010) will increase coverage by 0.14%.
The diff coverage is 66.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4813      +/-   ##
==========================================
+ Coverage   81.49%   81.64%   +0.14%     
==========================================
  Files          49       50       +1     
  Lines        5653     5660       +7     
==========================================
+ Hits         4607     4621      +14     
+ Misses       1046     1039       -7     
Impacted Files Coverage Δ
components/nimbus/src/matcher.rs 94.44% <ø> (+47.22%) ⬆️
components/nimbus/src/lib.rs 85.97% <57.14%> (-0.88%) ⬇️
components/nimbus/src/evaluator.rs 96.44% <75.00%> (-0.84%) ⬇️
components/nimbus/tests/test_message_helpers.rs 100.00% <100.00%> (ø)
components/nimbus/src/error.rs 20.58% <0.00%> (+8.82%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2e88010...a587631. Read the comment docs.

@jhugman jhugman force-pushed the jhugman/add-jexl-helper branch from a587631 to 23fa587 Compare February 1, 2022 19:23
@@ -34,7 +34,7 @@ import org.robolectric.RobolectricTestRunner
import java.util.concurrent.Executors

@RunWith(RobolectricTestRunner::class)
class NimbusTest {
class NimbusTests {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aside from the rename, I think I accidentally ran a re-format on this file.

@@ -292,7 +363,8 @@ class NimbusTest {
"id": "test-experiment",
"last_modified": 1602197324372
}]}
""".trimIndent())
""".trimIndent()
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nerp. No extra changes.

Copy link
Contributor

@tarikeshaq tarikeshaq left a comment

Choose a reason for hiding this comment

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

Awesome! Looks good to me - it would be awesome if we can add a couple more tests cases in the kotlin/swift code, but otherwise no need to block on my review!

I am absolutely in love with exposing the ability to run targeting by the consumers 🚀 This should allow us to improve our end-to-end testing story as well 🕺

}

@Test
fun `jexl can be run against the json context`() {
Copy link
Contributor

Choose a reason for hiding this comment

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

should we add more complex jexl statements (at the very least something with "ors" and "and")?

match json.as_object() {
Some(obj) => Ok(obj.clone()),
_ => Err(uniffi::deps::anyhow::anyhow!(
"Unexpected JSON-non-object in the bagging area"
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm can we add tests for a non-object json? to make sure the exception is handled correctly by this - I think the invalid json case is covered by the bindings side custom types right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added tests, but we try hard to make sure strings that don't code for objects enter the system.

Comment on lines -14 to -33
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct Matcher {
pub app_name: String,
pub app_id: String,
pub channel: String,
pub app_display_version: Option<String>,
pub app_min_version: Option<String>,
pub app_max_version: Option<String>,
pub app_build: Option<String>,
pub app_min_build: Option<String>,
pub app_max_build: Option<String>,
pub architecture: Option<String>,
pub device_manufacturer: Option<String>,
pub device_model: Option<String>,
pub locale: Option<String>,
pub os: Option<String>,
pub os_version: Option<String>,
pub android_sdk_version: Option<String>,
pub debug_tags: Vec<String>,
}
Copy link
Contributor

Choose a reason for hiding this comment

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

😍

#[macro_use]
use nimbus::error::Result;

#[cfg(feature = "rkv-safe-mode")]
Copy link
Contributor

Choose a reason for hiding this comment

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

out of curiosity, why is this necessary? I know it's in the other test files, but does it have any implication on targeting tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it's because we actually use a DB in testing. I don't know definitively, except it doesn't work without it. It's cargo cult by this time.

messageHelper.evalJexl("test_value_from_json == 42")
}

assertTrue(
Copy link
Contributor

Choose a reason for hiding this comment

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

😍 🎉 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants