vector_vrl_tests/
test_enrichment.rs

1use std::collections::HashMap;
2
3use vrl::value::{ObjectMap, Value};
4
5#[derive(Debug, Clone)]
6struct TestEnrichmentTable;
7
8impl enrichment::Table for TestEnrichmentTable {
9    fn find_table_row<'a>(
10        &self,
11        _case: enrichment::Case,
12        _condition: &'a [enrichment::Condition<'a>],
13        _select: Option<&[String]>,
14        _wildcard: Option<&Value>,
15        _index: Option<enrichment::IndexHandle>,
16    ) -> Result<ObjectMap, String> {
17        let mut result = ObjectMap::new();
18        result.insert("id".into(), Value::from(1));
19        result.insert("firstname".into(), Value::from("Bob"));
20        result.insert("surname".into(), Value::from("Smith"));
21
22        Ok(result)
23    }
24
25    fn find_table_rows<'a>(
26        &self,
27        _case: enrichment::Case,
28        _condition: &'a [enrichment::Condition<'a>],
29        _select: Option<&[String]>,
30        _wildcard: Option<&Value>,
31        _index: Option<enrichment::IndexHandle>,
32    ) -> Result<Vec<ObjectMap>, String> {
33        let mut result1 = ObjectMap::new();
34        result1.insert("id".into(), Value::from(1));
35        result1.insert("firstname".into(), Value::from("Bob"));
36        result1.insert("surname".into(), Value::from("Smith"));
37
38        let mut result2 = ObjectMap::new();
39        result2.insert("id".into(), Value::from(2));
40        result2.insert("firstname".into(), Value::from("Fred"));
41        result2.insert("surname".into(), Value::from("Smith"));
42
43        Ok(vec![result1, result2])
44    }
45
46    fn add_index(
47        &mut self,
48        _case: enrichment::Case,
49        _fields: &[&str],
50    ) -> Result<enrichment::IndexHandle, String> {
51        Ok(enrichment::IndexHandle(1))
52    }
53
54    fn index_fields(&self) -> Vec<(enrichment::Case, Vec<String>)> {
55        Vec::new()
56    }
57
58    /// Returns true if the underlying data has changed and the table needs reloading.
59    fn needs_reload(&self) -> bool {
60        false
61    }
62}
63
64pub(crate) fn test_enrichment_table() -> enrichment::TableRegistry {
65    let registry = enrichment::TableRegistry::default();
66    let mut tables: HashMap<String, Box<dyn enrichment::Table + Send + Sync>> = HashMap::new();
67    tables.insert("test".into(), Box::new(TestEnrichmentTable));
68    registry.load(tables);
69
70    registry
71}