-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add jexl to remote-settings component
- Loading branch information
Showing
10 changed files
with
326 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use crate::RemoteSettingsContext; | ||
use firefox_versioning::compare::version_compare; | ||
use jexl_eval::Evaluator; | ||
use serde_json::{json, Value}; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub(crate) enum ParseError { | ||
#[error("Evaluation error: {0}")] | ||
EvaluationError(String), | ||
#[error("Invalid result type")] | ||
InvalidResultType, | ||
} | ||
|
||
/// The JEXL filter is getting instatiated when a new `RemoteSettingsClient` is being created. | ||
pub struct JexlFilter { | ||
/// a JEXL `Evaluator` to run transforms and evaluations on. | ||
evaluator: Evaluator<'static>, | ||
/// The transformed `RemoteSettingsContext` as a `serde_json::Value` | ||
context: Value, | ||
} | ||
|
||
impl JexlFilter { | ||
/// Creating a new `JEXL` filter. If no `context` is set, all future `records` are being | ||
/// evaluated as `true` by default. | ||
pub(crate) fn new(context: Option<RemoteSettingsContext>) -> Self { | ||
let env_context = match context { | ||
Some(ctx) => { | ||
serde_json::to_value(ctx).expect("Failed to serialize RemoteSettingsContext") | ||
} | ||
None => json!({}), | ||
}; | ||
|
||
Self { | ||
evaluator: Evaluator::new() | ||
// We want to add more transforms later on. We started with `versionCompare`. | ||
// https://remote-settings.readthedocs.io/en/latest/target-filters.html#transforms | ||
// The goal is to get on pare with the desktop. | ||
.with_transform("versionCompare", |args| Ok(version_compare(args)?)), | ||
context: env_context, | ||
} | ||
} | ||
|
||
/// Evaluates the given filter expression in the provided context. | ||
/// Returns `Ok(true)` if the expression evaluates to true, `Ok(false)` otherwise. | ||
pub(crate) fn evaluate(&self, filter_expr: &str) -> Result<bool, ParseError> { | ||
if filter_expr.trim().is_empty() { | ||
return Ok(true); | ||
} | ||
|
||
let result = self | ||
.evaluator | ||
.eval_in_context(filter_expr, &self.context) | ||
.map_err(|e| { | ||
ParseError::EvaluationError(format!("Failed to evaluate '{}': {}", filter_expr, e)) | ||
})?; | ||
|
||
result.as_bool().ok_or(ParseError::InvalidResultType) | ||
} | ||
} |
Oops, something went wrong.