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
use std::collections::HashMap;
use vrl::value::{ObjectMap, Value};

#[derive(Debug, Clone)]
struct TestEnrichmentTable;

impl enrichment::Table for TestEnrichmentTable {
    fn find_table_row<'a>(
        &self,
        _case: enrichment::Case,
        _condition: &'a [enrichment::Condition<'a>],
        _select: Option<&[String]>,
        _index: Option<enrichment::IndexHandle>,
    ) -> Result<ObjectMap, String> {
        let mut result = ObjectMap::new();
        result.insert("id".into(), Value::from(1));
        result.insert("firstname".into(), Value::from("Bob"));
        result.insert("surname".into(), Value::from("Smith"));

        Ok(result)
    }

    fn find_table_rows<'a>(
        &self,
        _case: enrichment::Case,
        _condition: &'a [enrichment::Condition<'a>],
        _select: Option<&[String]>,
        _index: Option<enrichment::IndexHandle>,
    ) -> Result<Vec<ObjectMap>, String> {
        let mut result1 = ObjectMap::new();
        result1.insert("id".into(), Value::from(1));
        result1.insert("firstname".into(), Value::from("Bob"));
        result1.insert("surname".into(), Value::from("Smith"));

        let mut result2 = ObjectMap::new();
        result2.insert("id".into(), Value::from(2));
        result2.insert("firstname".into(), Value::from("Fred"));
        result2.insert("surname".into(), Value::from("Smith"));

        Ok(vec![result1, result2])
    }

    fn add_index(
        &mut self,
        _case: enrichment::Case,
        _fields: &[&str],
    ) -> Result<enrichment::IndexHandle, String> {
        Ok(enrichment::IndexHandle(1))
    }

    fn index_fields(&self) -> Vec<(enrichment::Case, Vec<String>)> {
        Vec::new()
    }

    /// Returns true if the underlying data has changed and the table needs reloading.
    fn needs_reload(&self) -> bool {
        false
    }
}

pub(crate) fn test_enrichment_table() -> enrichment::TableRegistry {
    let registry = enrichment::TableRegistry::default();
    let mut tables: HashMap<String, Box<dyn enrichment::Table + Send + Sync>> = HashMap::new();
    tables.insert("test".into(), Box::new(TestEnrichmentTable));
    registry.load(tables);

    registry
}