vector/transforms/lua/
mod.rs

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