vector_core/event/lua/
log.rs1use mlua::prelude::*;
2
3use super::super::{EventMetadata, LogEvent, Value};
4
5impl IntoLua for LogEvent {
6 #![allow(clippy::wrong_self_convention)] fn into_lua(self, lua: &Lua) -> LuaResult<LuaValue> {
8 let (value, _metadata) = self.into_parts();
9 value.into_lua(lua)
10 }
11}
12
13impl FromLua for LogEvent {
14 fn from_lua(lua_value: LuaValue, lua: &Lua) -> LuaResult<Self> {
15 let value = Value::from_lua(lua_value, lua)?;
16 Ok(LogEvent::from_parts(value, EventMetadata::default()))
17 }
18}
19
20#[cfg(test)]
21mod test {
22 use super::*;
23
24 #[test]
25 fn into_lua() {
26 let mut log = LogEvent::default();
27 log.insert("a", 1);
28 log.insert("nested.field", "2");
29 log.insert("nested.array[0]", "example value");
30 log.insert("nested.array[2]", "another value");
31
32 let assertions = vec![
33 "type(log) == 'table'",
34 "log.a == 1",
35 "type(log.nested) == 'table'",
36 "log.nested.field == '2'",
37 "#log.nested.array == 3",
38 "log.nested.array[1] == 'example value'",
39 "log.nested.array[2] == ''",
40 "log.nested.array[3] == 'another value'",
41 ];
42
43 let lua = Lua::new();
44 lua.globals().set("log", log.clone()).unwrap();
45 for assertion in assertions {
46 let result: bool = lua
47 .load(assertion)
48 .eval()
49 .unwrap_or_else(|_| panic!("Failed to verify assertion {assertion:?}"));
50 assert!(result, "{}", assertion);
51 }
52 }
53
54 #[test]
55 fn from_lua() {
56 let lua_event = r"
57 {
58 a = 1,
59 nested = {
60 field = '2',
61 array = {'example value', '', 'another value'}
62 }
63 }
64 ";
65
66 let event: LogEvent = Lua::new().load(lua_event).eval().unwrap();
67
68 assert_eq!(event["a"], Value::Integer(1));
69 assert_eq!(event["nested.field"], Value::Bytes("2".into()));
70 assert_eq!(
71 event["nested.array[0]"],
72 Value::Bytes("example value".into())
73 );
74 assert_eq!(event["nested.array[1]"], Value::Bytes("".into()));
75 assert_eq!(
76 event["nested.array[2]"],
77 Value::Bytes("another value".into())
78 );
79 }
80}