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
use mlua::prelude::*;

use super::super::{Event, LogEvent, Metric};
use super::metric::LuaMetric;

pub struct LuaEvent {
    pub event: Event,
    pub metric_multi_value_tags: bool,
}

impl IntoLua for LuaEvent {
    #![allow(clippy::wrong_self_convention)] // this trait is defined by mlua
    fn into_lua(self, lua: &Lua) -> LuaResult<LuaValue> {
        let table = lua.create_table()?;
        match self.event {
            Event::Log(log) => table.raw_set("log", log.into_lua(lua)?)?,
            Event::Metric(metric) => table.raw_set(
                "metric",
                LuaMetric {
                    metric,
                    multi_value_tags: self.metric_multi_value_tags,
                }
                .into_lua(lua)?,
            )?,
            Event::Trace(_) => {
                return Err(LuaError::ToLuaConversionError {
                    from: String::from("Event"),
                    to: "table",
                    message: Some("Trace are not supported".to_string()),
                })
            }
        }
        Ok(LuaValue::Table(table))
    }
}

impl FromLua for Event {
    fn from_lua(value: LuaValue, lua: &Lua) -> LuaResult<Self> {
        let LuaValue::Table(table) = &value else {
            return Err(LuaError::FromLuaConversionError {
                from: value.type_name(),
                to: String::from("Event"),
                message: Some("Event should be a Lua table".to_string()),
            });
        };
        match (table.raw_get("log")?, table.raw_get("metric")?) {
            (LuaValue::Table(log), LuaValue::Nil) => {
                Ok(Event::Log(LogEvent::from_lua(LuaValue::Table(log), lua)?))
            }
            (LuaValue::Nil, LuaValue::Table(metric)) => Ok(Event::Metric(Metric::from_lua(
                LuaValue::Table(metric),
                lua,
            )?)),
            _ => Err(LuaError::FromLuaConversionError {
                from: value.type_name(),
                to: String::from("Event"),
                message: Some(
                    "Event should contain either \"log\" or \"metric\" key at the top level"
                        .to_string(),
                ),
            }),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::event::{
        metric::{MetricKind, MetricValue},
        Metric, Value,
    };

    fn assert_event(event: Event, assertions: Vec<&'static str>) {
        let lua = Lua::new();
        lua.globals()
            .set(
                "event",
                LuaEvent {
                    event,
                    metric_multi_value_tags: false,
                },
            )
            .unwrap();
        for assertion in assertions {
            assert!(
                lua.load(assertion).eval::<bool>().expect(assertion),
                "{}",
                assertion
            );
        }
    }

    #[test]
    fn into_lua_log() {
        let mut event = LogEvent::default();
        event.insert("field", "value");

        let assertions = vec![
            "type(event) == 'table'",
            "event.metric == nil",
            "type(event.log) == 'table'",
            "event.log.field == 'value'",
        ];

        assert_event(event.into(), assertions);
    }

    #[test]
    fn into_lua_metric() {
        let event = Event::Metric(Metric::new(
            "example counter",
            MetricKind::Absolute,
            MetricValue::Counter {
                value: 0.577_215_66,
            },
        ));

        let assertions = vec![
            "type(event) == 'table'",
            "event.log == nil",
            "type(event.metric) == 'table'",
            "event.metric.name == 'example counter'",
            "event.metric.counter.value == 0.57721566",
        ];

        assert_event(event, assertions);
    }

    #[test]
    fn from_lua_log() {
        let lua_event = r#"
        {
            log = {
                field = "example",
                nested = {
                    field = "another example"
                }
            }
        }"#;

        let event = Lua::new().load(lua_event).eval::<Event>().unwrap();
        let log = event.as_log();
        assert_eq!(log["field"], Value::Bytes("example".into()));
        assert_eq!(log["nested.field"], Value::Bytes("another example".into()));
    }

    #[test]
    fn from_lua_metric() {
        let lua_event = r#"
        {
            metric = {
                name = "example counter",
                counter = {
                    value = 0.57721566
                }
            }
        }"#;
        let expected = Event::Metric(Metric::new(
            "example counter",
            MetricKind::Absolute,
            MetricValue::Counter {
                value: 0.577_215_66,
            },
        ));

        let event = Lua::new().load(lua_event).eval::<Event>().unwrap();
        vector_common::assert_event_data_eq!(event, expected);
    }

    #[test]
    // the panic message a) is platform dependent and b) can change if any code is added before this function.
    #[allow(clippy::should_panic_without_expect)]
    #[should_panic]
    fn from_lua_missing_log_and_metric() {
        let lua_event = r"{
            some_field: {}
        }";
        Lua::new().load(lua_event).eval::<Event>().unwrap();
    }
}