vector/transforms/lua/
mod.rs1pub mod v1;
2pub mod v2;
3
4use vector_lib::{
5    config::{ComponentKey, LogNamespace},
6    configurable::configurable_component,
7};
8
9use crate::{
10    config::{GenerateConfig, Input, OutputId, TransformConfig, TransformContext, TransformOutput},
11    schema,
12    transforms::Transform,
13};
14
15#[configurable_component]
17#[derive(Clone, Debug)]
18enum V1 {
19    #[configurable(metadata(deprecated))]
27    #[serde(rename = "1")]
28    V1,
29}
30
31#[configurable_component]
33#[derive(Clone, Debug)]
34pub struct LuaConfigV1 {
35    version: Option<V1>,
39
40    #[serde(flatten)]
41    config: v1::LuaConfig,
42}
43
44#[configurable_component]
46#[derive(Clone, Debug)]
47enum V2 {
48    #[serde(rename = "2")]
50    V2,
51}
52
53#[configurable_component]
55#[derive(Clone, Debug)]
56pub struct LuaConfigV2 {
57    version: V2,
61
62    #[serde(flatten)]
63    config: v2::LuaConfig,
64}
65
66#[configurable_component(transform(
68    "lua",
69    "Modify event data using the Lua programming language."
70))]
71#[derive(Clone, Debug)]
72#[serde(untagged)]
73pub enum LuaConfig {
74    V1(LuaConfigV1),
76
77    V2(LuaConfigV2),
79}
80
81impl GenerateConfig for LuaConfig {
82    fn generate_config() -> toml::Value {
83        toml::from_str(
84            r#"version = "2"
85            hooks.process = """#,
86        )
87        .unwrap()
88    }
89}
90
91#[async_trait::async_trait]
92#[typetag::serde(name = "lua")]
93impl TransformConfig for LuaConfig {
94    async fn build(&self, context: &TransformContext) -> crate::Result<Transform> {
95        let key = context
96            .key
97            .as_ref()
98            .map_or_else(|| ComponentKey::from("lua"), Clone::clone);
99        match self {
100            LuaConfig::V1(v1) => v1.config.build(),
101            LuaConfig::V2(v2) => v2.config.build(key),
102        }
103    }
104
105    fn input(&self) -> Input {
106        match self {
107            LuaConfig::V1(v1) => v1.config.input(),
108            LuaConfig::V2(v2) => v2.config.input(),
109        }
110    }
111
112    fn outputs(
113        &self,
114        _: vector_lib::enrichment::TableRegistry,
115        input_definitions: &[(OutputId, schema::Definition)],
116        _: LogNamespace,
117    ) -> Vec<TransformOutput> {
118        match self {
119            LuaConfig::V1(v1) => v1.config.outputs(input_definitions),
120            LuaConfig::V2(v2) => v2.config.outputs(input_definitions),
121        }
122    }
123}
124
125#[cfg(test)]
126mod test {
127    #[test]
128    fn generate_config() {
129        crate::test_util::test_generate_config::<super::LuaConfig>();
130    }
131}