diff --git a/Cargo.toml b/Cargo.toml index 66b5daa..e12bea0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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ž diff --git a/src/lib.rs b/src/lib.rs index 0a68071..9292f60 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -110,7 +110,7 @@ pub fn evaluate_with_context(definition: String, host: Arc) -> * @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") @@ -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 @@ -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);