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