mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
fix: Merge data to init document (#293)
Init document is the aggregated data documen that the user has specified using multiple `add_data` calls. Each query evaluation starts of by initializing the current data to the init document. Previously `add_data` was incorrectly added to the current document, causing the added data to be lost if the addition happened after query evaluation. With this fix, scenarios where data addition may be interspersed with query evaluation calls are supported. Also provide a get_data method to obtain the (init) data document. Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
committed by
GitHub
parent
52afcbe5c5
commit
ef549a6528
@@ -422,3 +422,32 @@ fn one_yaml() -> Result<()> {
|
||||
fn run(path: &str) {
|
||||
yaml_test(path).unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_data() -> Result<()> {
|
||||
let mut engine = Engine::new();
|
||||
|
||||
// Merge { "x" : 1, "y" : {} }
|
||||
engine.add_data(Value::from_json_str(r#"{ "x" : 1, "y" : {}}"#)?)?;
|
||||
|
||||
// Merge { "z" : 2 }
|
||||
engine.add_data(Value::from_json_str(r#"{ "z" : 2 }"#)?)?;
|
||||
|
||||
// Add a policy
|
||||
engine.add_policy("policy.rego".to_string(), "package a".to_string())?;
|
||||
|
||||
// Evaluate virtual data document. The virtual document includes all rules as well.
|
||||
let v_data = engine.eval_query("data".to_string(), false)?.result[0].expressions[0]
|
||||
.value
|
||||
.clone();
|
||||
// There must be an empty package.
|
||||
assert_eq!(v_data["a"], Value::new_object());
|
||||
|
||||
// Get the data document.
|
||||
let data = engine.get_data();
|
||||
|
||||
// There must NOT be any value of `a`.
|
||||
assert_eq!(data["a"], Value::Undefined);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user