pub struct Expression { /* private fields */ }Expand description
A compiled JSONata expression.
Compile once with Expression::compile and evaluate many times. A compiled expression is
immutable and can be shared between threads.
Implementations§
Source§impl Expression
impl Expression
Sourcepub fn evaluate(
&self,
input: Option<&JsonValue>,
bindings: &Bindings,
) -> Result<Option<JsonValue>>
pub fn evaluate( &self, input: Option<&JsonValue>, bindings: &Bindings, ) -> Result<Option<JsonValue>>
Evaluates the expression against input with default Options.
None as input means undefined (no input document). A result of Ok(None) means
the expression evaluated to undefined.
Sourcepub fn evaluate_with(
&self,
input: Option<&JsonValue>,
bindings: &Bindings,
options: &Options,
) -> Result<Option<JsonValue>>
pub fn evaluate_with( &self, input: Option<&JsonValue>, bindings: &Bindings, options: &Options, ) -> Result<Option<JsonValue>>
Evaluates the expression with explicit Options (limits and guardrails).
Sourcepub fn evaluate_source<'a>(
&self,
input: &dyn Source<'a>,
bindings: &'a Bindings,
options: &Options,
) -> Result<Option<JsonValue>>
pub fn evaluate_source<'a>( &self, input: &dyn Source<'a>, bindings: &'a Bindings, options: &Options, ) -> Result<Option<JsonValue>>
Evaluates the expression against an input read from a Source, one
level at a time, as the evaluation reaches it. Use it for an input that is not a
serde_json::Value, without converting it first.
input writes the input document as the only value of its level, or nothing for
undefined. A level that the source fails to read stops the evaluation with U1004.
Sourcepub fn evaluate_document(
&self,
document: &Document,
bindings: &Bindings,
options: &Options,
) -> Result<Option<JsonValue>>
pub fn evaluate_document( &self, document: &Document, bindings: &Bindings, options: &Options, ) -> Result<Option<JsonValue>>
Evaluates the expression against a Document converted beforehand. Use it to
evaluate several expressions, or the same one several times, against one large document
without converting it each time.
Sourcepub fn evaluate_traced(
&self,
input: Option<&JsonValue>,
bindings: &Bindings,
options: &Options,
tracer: &mut dyn Tracer,
) -> Result<Option<JsonValue>>
pub fn evaluate_traced( &self, input: Option<&JsonValue>, bindings: &Bindings, options: &Options, tracer: &mut dyn Tracer, ) -> Result<Option<JsonValue>>
Evaluates the expression, reporting every evaluation event to tracer.
See the audit module for the events and the tracers jlens provides.
Sourcepub fn evaluate_audited(
&self,
input: Option<&JsonValue>,
bindings: &Bindings,
options: &Options,
audit: &AuditOptions,
) -> (Result<Option<JsonValue>>, AuditTree)
pub fn evaluate_audited( &self, input: Option<&JsonValue>, bindings: &Bindings, options: &Options, audit: &AuditOptions, ) -> (Result<Option<JsonValue>>, AuditTree)
Evaluates the expression and records an audit::AuditTree of how the result was
reached. The audit is returned even when the evaluation fails.
use jlens_core::{Bindings, Expression, Options};
use jlens_core::audit::{AuditOptions, OutcomeKind};
let expr = Expression::compile("$x > 10").unwrap();
let bindings = Bindings::new().var("x", 3);
let (result, audit) =
expr.evaluate_audited(None, &bindings, &Options::default(), &AuditOptions::decisions());
assert_eq!(result.unwrap(), Some(serde_json::json!(false)));
let root = audit.root.unwrap();
assert_eq!(root.outcome, OutcomeKind::False);
assert_eq!(root.operands, Some([Some(3.into()), Some(10.into())]));