Skip to main content

vector/transforms/lua/
mod.rs

1pub mod v1;
2pub mod v2;
3
4use vector_lib::{config::ComponentKey, configurable::configurable_component};
5
6use crate::{
7    config::{GenerateConfig, Input, OutputId, TransformConfig, TransformContext, TransformOutput},
8    schema,
9    transforms::Transform,
10};
11
12/// Marker type for the version one of the configuration for the `lua` transform.
13#[configurable_component]
14#[derive(Clone, Debug)]
15enum V1 {
16    /// Lua transform API version 1.
17    ///
18    /// This version is deprecated and will be removed in a future version.
19    #[configurable(metadata(deprecated))]
20    #[serde(rename = "1")]
21    V1,
22}
23
24/// Configuration for the version one of the `lua` transform.
25#[configurable_component]
26#[derive(Clone, Debug)]
27pub struct LuaConfigV1 {
28    /// Transform API version.
29    ///
30    /// Specifying this version ensures that backward compatibility is not broken.
31    version: Option<V1>,
32
33    #[serde(flatten)]
34    config: v1::LuaConfig,
35}
36
37/// Marker type for version two of the configuration for the `lua` transform.
38#[configurable_component]
39#[derive(Clone, Debug)]
40enum V2 {
41    /// Lua transform API version 2.
42    #[serde(rename = "2")]
43    V2,
44}
45
46/// Configuration for the version two of the `lua` transform.
47#[configurable_component]
48#[derive(Clone, Debug)]
49pub struct LuaConfigV2 {
50    /// Transform API version.
51    ///
52    /// Specifying this version ensures that backward compatibility is not broken.
53    version: V2,
54
55    #[serde(flatten)]
56    config: v2::LuaConfig,
57}
58
59/// Configuration for the `lua` transform.
60#[configurable_component(transform(
61    "lua",
62    "Modify event data using the Lua programming language."
63))]
64#[derive(Clone, Debug)]
65#[serde(untagged)]
66pub enum LuaConfig {
67    /// Configuration for version one.
68    V1(LuaConfigV1),
69
70    /// Configuration for version two.
71    V2(LuaConfigV2),
72}
73
74impl GenerateConfig for LuaConfig {
75    fn generate_config() -> toml::Value {
76        toml::from_str(
77            r#"version = "2"
78            hooks.process = """#,
79        )
80        .unwrap()
81    }
82}
83
84#[async_trait::async_trait]
85#[typetag::serde(name = "lua")]
86impl TransformConfig for LuaConfig {
87    async fn build(&self, context: &TransformContext) -> crate::Result<Transform> {
88        let key = context
89            .key
90            .as_ref()
91            .map_or_else(|| ComponentKey::from("lua"), Clone::clone);
92        match self {
93            LuaConfig::V1(v1) => v1.config.build(),
94            LuaConfig::V2(v2) => v2.config.build(key),
95        }
96    }
97
98    fn input(&self) -> Input {
99        match self {
100            LuaConfig::V1(v1) => v1.config.input(),
101            LuaConfig::V2(v2) => v2.config.input(),
102        }
103    }
104
105    fn outputs(
106        &self,
107        _: &TransformContext,
108        input_definitions: &[(OutputId, schema::Definition)],
109    ) -> Vec<TransformOutput> {
110        match self {
111            LuaConfig::V1(v1) => v1.config.outputs(input_definitions),
112            LuaConfig::V2(v2) => v2.config.outputs(input_definitions),
113        }
114    }
115}
116
117#[cfg(test)]
118mod test {
119    #[test]
120    fn generate_config() {
121        crate::test_util::test_generate_config::<super::LuaConfig>();
122    }
123}