Skip to content

Commit

Permalink
Merge pull request #7 from superwall/develop
Browse files Browse the repository at this point in the history
Fix device, bump version
  • Loading branch information
ianrumac authored Oct 14, 2024
2 parents daa8d67 + 5fba2c6 commit f03f4b0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cel-eval"
version = "0.1.6"
version = "0.1.7"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.htmlž
Expand Down
106 changes: 70 additions & 36 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn evaluate_with_context(definition: String, host: Arc<dyn HostContext>) ->
* @return The AST of the expression, serialized as JSON
*/
pub fn parse_to_ast(expression: String) -> String {
let ast : JSONExpression = parse(expression.as_str()).expect(
let ast: JSONExpression = parse(expression.as_str()).expect(
format!("Failed to parse expression: {}", expression).as_str()
).into();
serde_json::to_string(&ast).expect("Failed to serialize AST into JSON")
Expand Down Expand Up @@ -262,6 +262,16 @@ fn execute_with(
)
.unwrap();

// Add the map to the `device` object
ctx.add_variable(
"device",
Value::Map(Map {
map: Arc::new(device_host_properties),
}),
)
.unwrap();


let binding = device.clone();
// Combine the device and computed properties
let host_properties = binding
Expand Down Expand Up @@ -540,51 +550,75 @@ mod tests {
}

#[tokio::test]
async fn test_execution_with_platform_reference() {
async fn test_execution_with_platform_computed_reference() {
let days_since = PassableValue::UInt(7);
let days_since = serde_json::to_string(&days_since).unwrap();
let ctx = Arc::new(TestContext {
map: [("daysSinceEvent".to_string(), days_since)]
map: [("minutesSince".to_string(), days_since)]
.iter()
.cloned()
.collect(),
});
let res = evaluate_with_context(
r#"
{
"variables": {
"map": {
"user": {
"type": "map",
"value": {
"should_display": {
"type": "bool",
"value": true
},
"some_value": {
"type": "uint",
"value": 7
}
}
}
}
},
"computed" : {
"daysSinceEvent": [{
"type": "string",
"value": "event_name"
}]
},
"device" : {
"timeSinceEvent": [{
"type": "string",
"value": "event_name"
}]
},
"expression": "computed.daysSinceEvent(\"test\") == user.some_value"
{
"variables": {
"map": {}
},
"expression": "device.minutesSince('app_launch') == computed.minutesSince('app_install')",
"computed": {
"daysSince": [
{
"type": "string",
"value": "event_name"
}
],
"minutesSince": [
{
"type": "string",
"value": "event_name"
}
],
"hoursSince": [
{
"type": "string",
"value": "event_name"
}
],
"monthsSince": [
{
"type": "string",
"value": "event_name"
}
]
},
"device": {
"daysSince": [
{
"type": "string",
"value": "event_name"
}
],
"minutesSince": [
{
"type": "string",
"value": "event_name"
}
],
"hoursSince": [
{
"type": "string",
"value": "event_name"
}
],
"monthsSince": [
{
"type": "string",
"value": "event_name"
}
]
}
"#
.to_string(),
}"#.to_string(),
ctx,
);
println!("{}", res);
Expand Down

0 comments on commit f03f4b0

Please sign in to comment.