vector/transforms/lua/
mod.rs

1pub 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/// Marker type for the version one of the configuration for the `lua` transform.
16#[configurable_component]
17#[derive(Clone, Debug)]
18enum V1 {
19    /// Lua transform API version 1.
20    ///
21    /// This version is deprecated and will be removed in a future version.
22    // TODO: The `deprecated` attribute flag is not used/can't be used for enum values like this
23    // because we don't emit the full schema for the enum value, we just gather its description. We
24    // might need to consider actually using the flag as a marker to say "append our boilerplate
25    // deprecation warning to the description of the field/enum value/etc".
26    #[configurable(metadata(deprecated))]
27    #[serde(rename = "1")]
28    V1,
29}
30
31/// Configuration for the version one of the `lua` transform.
32#[configurable_component]
33#[derive(Clone, Debug)]
34pub struct LuaConfigV1 {
35    /// Transform API version.
36    ///
37    /// Specifying this version ensures that backward compatibility is not broken.
38    version: Option<V1>,
39
40    #[serde(flatten)]
41    config: v1::LuaConfig,
42}
43
44/// Marker type for version two of the configuration for the `lua` transform.
45#[configurable_component]
46#[derive(Clone, Debug)]
47enum V2 {
48    /// Lua transform API version 2.
49    #[serde(rename = "2")]
50    V2,
51}
52
53/// Configuration for the version two of the `lua` transform.
54#[configurable_component]
55#[derive(Clone, Debug)]
56pub struct LuaConfigV2 {
57    /// Transform API version.
58    ///
59    /// Specifying this version ensures that backward compatibility is not broken.
60    version: V2,
61
62    #[serde(flatten)]
63    config: v2::LuaConfig,
64}
65
66/// Configuration for the `lua` transform.
67#[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    /// Configuration for version one.
75    V1(LuaConfigV1),
76
77    /// Configuration for version two.
78    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}