mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
tracing::trace builtin
Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
This commit is contained in:
committed by
Anand Krishnamoorthi
parent
81f6199642
commit
112623c762
+2
-1
@@ -9,6 +9,7 @@ mod debugging;
|
||||
pub mod numbers;
|
||||
pub mod sets;
|
||||
mod strings;
|
||||
mod tracing;
|
||||
pub mod types;
|
||||
mod utils;
|
||||
|
||||
@@ -56,7 +57,7 @@ lazy_static! {
|
||||
//rego::register(&mut m);
|
||||
//opa::register(&mut m);
|
||||
debugging::register(&mut m);
|
||||
//tracing::register(&mut m);
|
||||
tracing::register(&mut m);
|
||||
|
||||
m
|
||||
};
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT License.
|
||||
|
||||
use crate::ast::Expr;
|
||||
use crate::builtins;
|
||||
use crate::builtins::utils::{ensure_args_count, ensure_string};
|
||||
use crate::lexer::Span;
|
||||
use crate::value::Value;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) {
|
||||
m.insert("trace", trace);
|
||||
}
|
||||
|
||||
// Symbol analyzer must ensure that vars used by trace are defined before
|
||||
// the trace statement. Scheduler must ensure the above constraint.
|
||||
fn trace(span: &Span, params: &[Expr], args: &[Value]) -> Result<Value> {
|
||||
let name = "trace";
|
||||
ensure_args_count(span, name, params, args, 1)?;
|
||||
let msg = ensure_string(name, ¶ms[0], &args[0])?;
|
||||
|
||||
// Unlike rego, trace returns a string instead of bool.
|
||||
// The interpreter accumulates the traces.
|
||||
// TODO: Stateful bultins can pass in a state that would allow capturing
|
||||
// the traces in the state.
|
||||
Ok(Value::String(msg))
|
||||
}
|
||||
+33
-2
@@ -31,6 +31,7 @@ pub struct Interpreter<'source> {
|
||||
active_rules: Vec<&'source Rule<'source>>,
|
||||
builtins_cache: BTreeMap<(&'static str, Vec<Value>), Value>,
|
||||
no_rules_lookup: bool,
|
||||
traces: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -66,6 +67,7 @@ impl<'source> Interpreter<'source> {
|
||||
active_rules: vec![],
|
||||
builtins_cache: BTreeMap::new(),
|
||||
no_rules_lookup: false,
|
||||
traces: None,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1204,6 +1206,14 @@ impl<'source> Interpreter<'source> {
|
||||
}
|
||||
|
||||
let v = builtin(span, ¶ms[..], &args[..])?;
|
||||
|
||||
// Handle trace function.
|
||||
// TODO: with modifier.
|
||||
if let (Some(traces), Value::String(msg)) = (&mut self.traces, &v) {
|
||||
traces.push(msg.clone());
|
||||
return Ok(Value::Bool(true));
|
||||
};
|
||||
|
||||
if let Some(name) = cache {
|
||||
self.builtins_cache.insert((name, args), v.clone());
|
||||
}
|
||||
@@ -1883,7 +1893,19 @@ impl<'source> Interpreter<'source> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn eval(&mut self, data: &Option<Value>, input: &Option<Value>) -> Result<Value> {
|
||||
pub fn eval(
|
||||
&mut self,
|
||||
data: &Option<Value>,
|
||||
input: &Option<Value>,
|
||||
enable_tracing: bool,
|
||||
) -> Result<Value> {
|
||||
self.traces = match enable_tracing {
|
||||
true => Some(vec![]),
|
||||
false => None,
|
||||
};
|
||||
|
||||
self.builtins_cache.clear();
|
||||
|
||||
if let Some(input) = input {
|
||||
self.input = input.clone();
|
||||
|
||||
@@ -1924,7 +1946,16 @@ impl<'source> Interpreter<'source> {
|
||||
Ok(self.data.clone())
|
||||
}
|
||||
|
||||
pub fn eval_query_snippet(&mut self, snippet: &'source Expr<'source>) -> Result<Value> {
|
||||
pub fn eval_query_snippet(
|
||||
&mut self,
|
||||
snippet: &'source Expr<'source>,
|
||||
enable_tracing: bool,
|
||||
) -> Result<Value> {
|
||||
self.traces = match enable_tracing {
|
||||
true => Some(vec![]),
|
||||
false => None,
|
||||
};
|
||||
|
||||
// Create a new scope for evaluating the expression.
|
||||
self.scopes.push(Scope::new());
|
||||
let prev_module = self.set_current_module(self.modules.last().copied())?;
|
||||
|
||||
Reference in New Issue
Block a user