jlens-core 0.18.0

JSONata language engine: parser, evaluator, function library and auditing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! Evaluation logging and auditing (see ADR 0004).
//!
//! The evaluator reports what it does to a [`Tracer`]: it enters and leaves nodes, compares
//! operands, short-circuits `and`/`or`, takes conditional branches, binds variables and filters
//! sequences. Tracers turn these events into something useful:
//!
//! * [`AuditCollector`] builds an [`AuditTree`], which records how the result was reached with
//!   business labels from [annotations](crate::annotation). [`explain`] summarizes it.
//! * [`JsonlTracer`] writes one JSON object per event, for offline analysis.
//! * `TracingTracer` (feature `tracing`) forwards events to the `tracing` crate.
//!
//! ```
//! use jlens_core::{Bindings, Expression, Options};
//! use jlens_core::audit::{AuditOptions, explain};
//! use serde_json::json;
//!
//! let expr = Expression::compile(
//!     r#"/** @label "Big order" */ total > 100 and /** @label "Known customer" */ customer != null"#,
//! ).unwrap();
//! let input = json!({"total": 250, "customer": null});
//! let (result, audit) =
//!     expr.evaluate_audited(Some(&input), &Bindings::new(), &Options::default(), &AuditOptions::decisions());
//! assert_eq!(result.unwrap(), Some(json!(false)));
//! let text = explain(&audit).to_string();
//! assert!(text.contains("✘ Known customer"));
//! ```

mod collector;
mod explain;
mod jsonl;
#[cfg(feature = "tracing")]
mod tracing_tracer;

pub use collector::AuditCollector;
pub use explain::{Explanation, ExplanationLine, explain};
pub use jsonl::JsonlTracer;
#[cfg(feature = "tracing")]
pub use tracing_tracer::TracingTracer;

use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;

use crate::ast::{Node, NodeId, Span};
use crate::error::Error;
use crate::value::{Inspect, MAX_NESTING, Value, nest};

/// Receives evaluation events. Every method has an empty default implementation.
///
/// Events nest: between `enter(node)` and the matching `exit(node)`, all events concern the
/// evaluation of `node` and of its descendants. The same node is entered many times when it is
/// evaluated for several items or inside a function called repeatedly.
pub trait Tracer {
    /// The evaluation starts.
    fn begin(&mut self, info: &EvalInfo) {
        let _ = info;
    }
    /// Evaluation of `node` against `input` starts.
    fn enter(&mut self, node: &Node, input: ValueRef<'_>) {
        let _ = (node, input);
    }
    /// Evaluation of `node` ends with a value or an error.
    fn exit(&mut self, node: &Node, outcome: Outcome<'_>) {
        let _ = (node, outcome);
    }
    /// A comparison (`=`, `!=`, `<`, `<=`, `>`, `>=`, `in`) is about to compare these operands.
    fn operands(&mut self, node: &Node, lhs: ValueRef<'_>, rhs: ValueRef<'_>) {
        let _ = (node, lhs, rhs);
    }
    /// The right side of `and`/`or` was not evaluated because the left side decided.
    fn short_circuit(&mut self, node: &Node) {
        let _ = node;
    }
    /// A conditional took a branch.
    fn branch(&mut self, node: &Node, taken: Branch) {
        let _ = (node, taken);
    }
    /// `$name := value` bound a variable.
    fn bind(&mut self, node: &Node, name: &str, value: ValueRef<'_>) {
        let _ = (node, name, value);
    }
    /// A predicate filter starts testing the items of a sequence.
    fn filter_begin(&mut self, predicate: &Node) {
        let _ = predicate;
    }
    /// A predicate filter tested one item.
    fn filter_item(&mut self, predicate: &Node, index: usize, item: ValueRef<'_>, passed: bool) {
        let _ = (predicate, index, item, passed);
    }
    /// A predicate filter finished.
    fn filter_end(&mut self, predicate: &Node) {
        let _ = predicate;
    }
    /// The evaluation ended.
    fn end(&mut self, outcome: Outcome<'_>) {
        let _ = outcome;
    }
}

/// Facts about an evaluation, given to [`Tracer::begin`].
#[derive(Debug, Clone)]
pub struct EvalInfo<'a> {
    /// Source text of the expression.
    pub source: &'a str,
    /// The evaluation's timestamp (`$millis()`), in milliseconds since the epoch.
    pub timestamp: f64,
}

/// A read-only view of a run-time value.
#[derive(Clone, Copy)]
pub struct ValueRef<'a>(pub(crate) &'a dyn Inspect);

impl ValueRef<'_> {
    /// The value as JSON (`None` for undefined and functions, and for a value nested more than
    /// 1,000 levels deep).
    pub fn to_json(&self) -> Option<JsonValue> {
        self.0.to_json()
    }

    /// The value as JSON, truncated according to `limits`.
    pub fn capture(&self, limits: &CaptureLimits) -> Option<JsonValue> {
        self.0.capture(limits)
    }

    /// Whether the value is undefined.
    pub fn is_undefined(&self) -> bool {
        self.0.signature_symbol() == 'm'
    }

    /// The value if it is a boolean.
    pub fn as_bool(&self) -> Option<bool> {
        self.0.as_bool()
    }

    /// The JSONata type name (`string`, `number`, `boolean`, `null`, `array`, `object`,
    /// `function`, or `undefined`).
    pub fn type_name(&self) -> &'static str {
        match self.0.signature_symbol() {
            'm' => "undefined",
            'l' => "null",
            'b' => "boolean",
            'n' => "number",
            's' => "string",
            'a' => "array",
            'o' => "object",
            _ => "function",
        }
    }
}

impl std::fmt::Debug for ValueRef<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

/// How the evaluation of a node ended.
#[derive(Debug, Clone, Copy)]
pub enum Outcome<'a> {
    /// It produced a value (possibly undefined).
    Value(ValueRef<'a>),
    /// It raised an error.
    Error(&'a Error),
}

/// The branch taken by a conditional.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Branch {
    /// The condition held.
    Then,
    /// The condition did not hold and there is an `else` part.
    Else,
    /// The condition did not hold and there is no `else` part.
    None,
}

/// How much an [`AuditCollector`] records.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AuditLevel {
    /// Nothing; the tree only has a header.
    Off,
    /// Only nodes that decide something: comparisons, `and`/`or`, conditionals, variable
    /// bindings, nodes producing booleans, filters, and annotated nodes.
    #[default]
    Decisions,
    /// Every evaluated node, with its input.
    Full,
}

/// Bounds on what an audit captures, so that large inputs produce bounded audits.
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CaptureLimits {
    /// Items kept from an array (and fields from an object) in captured values.
    pub max_items: usize,
    /// Characters kept from a string in captured values.
    pub max_string: usize,
    /// Nesting depth kept in captured values.
    pub max_depth: usize,
    /// Rejected items kept as samples per filter.
    pub max_rejected: usize,
    /// Nodes recorded in the tree; further nodes are dropped and `truncated` is set.
    pub max_nodes: usize,
    /// Characters of source text kept per node.
    pub max_source: usize,
}

impl Default for CaptureLimits {
    fn default() -> Self {
        CaptureLimits {
            max_items: 10,
            max_string: 200,
            max_depth: 4,
            max_rejected: 5,
            max_nodes: 10_000,
            max_source: 200,
        }
    }
}

/// Settings for [`crate::Expression::evaluate_audited`].
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct AuditOptions {
    /// What to record.
    pub level: AuditLevel,
    /// Bounds on captured values and tree size.
    pub limits: CaptureLimits,
}

impl AuditOptions {
    /// Records decisions only (the default).
    pub fn decisions() -> Self {
        AuditOptions::default()
    }

    /// Records every evaluated node.
    pub fn full() -> Self {
        AuditOptions {
            level: AuditLevel::Full,
            ..AuditOptions::default()
        }
    }
}

/// The audit of one evaluation.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuditTree {
    /// What was evaluated, when, and how it ended.
    pub header: AuditHeader,
    /// The root of the recorded tree (absent with [`AuditLevel::Off`]).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub root: Option<AuditNode>,
    /// Number of recorded nodes.
    pub node_count: usize,
    /// Whether nodes were dropped because of [`CaptureLimits::max_nodes`].
    pub truncated: bool,
}

/// The header of an [`AuditTree`].
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuditHeader {
    /// `jlens-core` and its version.
    pub engine: String,
    /// FNV-1a 64-bit hash of the expression source, in hexadecimal.
    pub expression_hash: String,
    /// FNV-1a 64-bit hash of the input document's JSON text, in hexadecimal.
    pub input_hash: String,
    /// The evaluation timestamp (`$now()`), in ISO 8601.
    pub timestamp: String,
    /// Wall-clock duration of the evaluation, in milliseconds.
    pub duration_ms: f64,
    /// The level the audit was recorded at.
    pub level: AuditLevel,
    /// The result of the evaluation (absent when undefined or on error).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<JsonValue>,
    /// The error, if the evaluation failed.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<AuditError>,
}

/// An error recorded in an audit.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AuditError {
    /// The JSONata error code.
    pub code: String,
    /// The message.
    pub message: String,
}

impl From<&Error> for AuditError {
    fn from(e: &Error) -> Self {
        AuditError {
            code: e.code().to_owned(),
            message: e.message(),
        }
    }
}

/// The result kind of an audited node.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum OutcomeKind {
    /// It produced `true`.
    True,
    /// It produced `false`.
    False,
    /// It produced another value.
    Value,
    /// It produced undefined.
    Undefined,
    /// It raised an error.
    Error,
}

/// One evaluation of one node.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuditNode {
    /// The syntax-tree node (`Node::id`).
    pub node: NodeId,
    /// The node type (`binary`, `path`, `function`, ...).
    pub kind: String,
    /// The operator, function or variable name, when there is one.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub op: Option<String>,
    /// Byte range of the node in the expression source.
    pub span: Span,
    /// The node's source text (possibly shortened).
    pub source: String,
    /// `@label` of the node's annotation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    /// `@message` of the node's annotation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    /// `@id` of the node's annotation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub check_id: Option<String>,
    /// The context value (recorded with [`AuditLevel::Full`] only).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub input: Option<JsonValue>,
    /// The value produced.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub output: Option<JsonValue>,
    /// How the evaluation ended.
    pub outcome: OutcomeKind,
    /// The error, when `outcome` is `error`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub error: Option<AuditError>,
    /// The compared values, for comparisons.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub operands: Option<[Option<JsonValue>; 2]>,
    /// For `and`/`or`: the right side was not evaluated.
    #[serde(skip_serializing_if = "std::ops::Not::not", default)]
    pub short_circuited: bool,
    /// For conditionals: the branch taken.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branch: Option<Branch>,
    /// For `$name := value`: the variable bound.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub binding: Option<String>,
    /// Predicate filters applied while evaluating this node.
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub filters: Vec<FilterAudit>,
    /// Recorded evaluations nested inside this one.
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub children: Vec<AuditNode>,
}

impl AuditNode {
    /// Whether the node evaluated to `true` (`Some(true)`), `false` (`Some(false)`), or
    /// something else (`None`).
    pub fn passed(&self) -> Option<bool> {
        match self.outcome {
            OutcomeKind::True => Some(true),
            OutcomeKind::False => Some(false),
            _ => None,
        }
    }
}

/// Statistics of a predicate filter (`items[price > 10]`).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FilterAudit {
    /// The predicate node.
    pub node: NodeId,
    /// The predicate's source text.
    pub source: String,
    /// `@label` of the predicate's annotation.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    /// Items tested.
    pub tested: usize,
    /// Items that passed.
    pub passed: usize,
    /// A sample of the items that did not pass.
    #[serde(skip_serializing_if = "Vec::is_empty", default)]
    pub rejected: Vec<JsonValue>,
}

/// Captures a value as JSON within `limits`. Truncated arrays end with a `"… N more"` string,
/// truncated objects with a `"…": "N more"` field, and strings with `…`.
pub(crate) fn capture(v: &Value<'_>, limits: &CaptureLimits, depth: usize) -> Option<JsonValue> {
    Some(match v {
        Value::Undefined => return None,
        Value::Function(_) => JsonValue::String("<function>".into()),
        Value::String(s) => {
            let count = s.chars().count();
            if count > limits.max_string {
                let mut t: String = s.chars().take(limits.max_string).collect();
                t.push('…');
                JsonValue::String(t)
            } else {
                JsonValue::String(s.to_string())
            }
        }
        Value::Array(_) | Value::Object(_) if depth >= limits.max_depth.min(MAX_NESTING) => {
            JsonValue::String("…".into())
        }
        Value::Array(a) => {
            let mut items: Vec<JsonValue> = a
                .items()
                .iter()
                .take(limits.max_items)
                .map(|i| {
                    nest(depth + 1, || capture(i, limits, depth + 1)).unwrap_or(JsonValue::Null)
                })
                .collect();
            if a.items().len() > limits.max_items {
                items.push(JsonValue::String(format!(
                    "… {} more",
                    a.items().len() - limits.max_items
                )));
            }
            JsonValue::Array(items)
        }
        Value::Object(o) => {
            let o = o.borrow();
            let mut map = serde_json::Map::new();
            for (k, v) in o.iter().take(limits.max_items) {
                if let Some(j) = nest(depth + 1, || capture(v, limits, depth + 1)) {
                    map.insert(k.to_string(), j);
                }
            }
            if o.len() > limits.max_items {
                map.insert(
                    "…".into(),
                    JsonValue::String(format!("{} more", o.len() - limits.max_items)),
                );
            }
            JsonValue::Object(map)
        }
        other => other.to_json()?,
    })
}

/// FNV-1a, 64 bits, as 16 hexadecimal digits.
pub(crate) fn fnv1a(text: &str) -> String {
    let mut hash: u64 = 0xcbf2_9ce4_8422_2325;
    for b in text.bytes() {
        hash ^= u64::from(b);
        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
    }
    format!("{hash:016x}")
}

/// A short description of a node for audits: its kind and operator/name.
pub(crate) fn describe(node: &Node) -> (&'static str, Option<String>) {
    use crate::ast::NodeKind;
    let op = match &node.kind {
        NodeKind::Binary { op, .. } => Some(op.symbol().to_owned()),
        NodeKind::Function { procedure, .. } | NodeKind::Partial { procedure, .. } => {
            match &procedure.kind {
                NodeKind::Variable { name } => Some(format!("${name}")),
                NodeKind::Path { steps, .. } => match &steps[0].kind {
                    NodeKind::Variable { name } => Some(format!("${name}")),
                    NodeKind::Name { name } => Some(name.clone()),
                    _ => None,
                },
                _ => None,
            }
        }
        NodeKind::Variable { name } => Some(format!("${name}")),
        NodeKind::Name { name } => Some(name.clone()),
        NodeKind::Bind { lhs, .. } => match &lhs.kind {
            NodeKind::Variable { name } => Some(format!("${name}")),
            _ => None,
        },
        _ => None,
    };
    (node.type_name(), op)
}

/// The source text of `span`, whitespace-normalized and shortened to `max` characters.
pub(crate) fn snippet(source: &str, span: Span, max: usize) -> String {
    let text = source.get(span.start..span.end).unwrap_or_default();
    // Drop comments (annotations show up as labels instead).
    let mut without_comments = String::with_capacity(text.len());
    let mut rest = text;
    while let Some(start) = rest.find("/*") {
        without_comments.push_str(&rest[..start]);
        match rest[start + 2..].find("*/") {
            Some(end) => rest = &rest[start + 2 + end + 2..],
            None => rest = "",
        }
    }
    without_comments.push_str(rest);
    let collapsed = without_comments
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ");
    if collapsed.chars().count() > max {
        let mut s: String = collapsed.chars().take(max).collect();
        s.push('…');
        s
    } else {
        collapsed
    }
}