datadog_api_client/datadogV1/model/
model_logs_array_processor.rs

1// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License.
2// This product includes software developed at Datadog (https://www.datadoghq.com/).
3// Copyright 2019-Present Datadog, Inc.
4use serde::de::{Error, MapAccess, Visitor};
5use serde::{Deserialize, Deserializer, Serialize};
6use serde_with::skip_serializing_none;
7use std::fmt::{self, Formatter};
8
9/// A processor for extracting, aggregating, or transforming values from JSON arrays within your logs.
10/// Supported operations are:
11/// - Select value from matching element
12/// - Compute array length
13/// - Append a value to an array
14#[non_exhaustive]
15#[skip_serializing_none]
16#[derive(Clone, Debug, PartialEq, Serialize)]
17pub struct LogsArrayProcessor {
18    /// Whether or not the processor is enabled.
19    #[serde(rename = "is_enabled")]
20    pub is_enabled: Option<bool>,
21    /// Name of the processor.
22    #[serde(rename = "name")]
23    pub name: Option<String>,
24    /// Configuration of the array processor operation to perform.
25    #[serde(rename = "operation")]
26    pub operation: crate::datadogV1::model::LogsArrayProcessorOperation,
27    /// Type of logs array processor.
28    #[serde(rename = "type")]
29    pub type_: crate::datadogV1::model::LogsArrayProcessorType,
30    #[serde(flatten)]
31    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
32    #[serde(skip)]
33    #[serde(default)]
34    pub(crate) _unparsed: bool,
35}
36
37impl LogsArrayProcessor {
38    pub fn new(
39        operation: crate::datadogV1::model::LogsArrayProcessorOperation,
40        type_: crate::datadogV1::model::LogsArrayProcessorType,
41    ) -> LogsArrayProcessor {
42        LogsArrayProcessor {
43            is_enabled: None,
44            name: None,
45            operation,
46            type_,
47            additional_properties: std::collections::BTreeMap::new(),
48            _unparsed: false,
49        }
50    }
51
52    pub fn is_enabled(mut self, value: bool) -> Self {
53        self.is_enabled = Some(value);
54        self
55    }
56
57    pub fn name(mut self, value: String) -> Self {
58        self.name = Some(value);
59        self
60    }
61
62    pub fn additional_properties(
63        mut self,
64        value: std::collections::BTreeMap<String, serde_json::Value>,
65    ) -> Self {
66        self.additional_properties = value;
67        self
68    }
69}
70
71impl<'de> Deserialize<'de> for LogsArrayProcessor {
72    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
73    where
74        D: Deserializer<'de>,
75    {
76        struct LogsArrayProcessorVisitor;
77        impl<'a> Visitor<'a> for LogsArrayProcessorVisitor {
78            type Value = LogsArrayProcessor;
79
80            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
81                f.write_str("a mapping")
82            }
83
84            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
85            where
86                M: MapAccess<'a>,
87            {
88                let mut is_enabled: Option<bool> = None;
89                let mut name: Option<String> = None;
90                let mut operation: Option<crate::datadogV1::model::LogsArrayProcessorOperation> =
91                    None;
92                let mut type_: Option<crate::datadogV1::model::LogsArrayProcessorType> = None;
93                let mut additional_properties: std::collections::BTreeMap<
94                    String,
95                    serde_json::Value,
96                > = std::collections::BTreeMap::new();
97                let mut _unparsed = false;
98
99                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
100                    match k.as_str() {
101                        "is_enabled" => {
102                            if v.is_null() {
103                                continue;
104                            }
105                            is_enabled = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
106                        }
107                        "name" => {
108                            if v.is_null() {
109                                continue;
110                            }
111                            name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
112                        }
113                        "operation" => {
114                            operation = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
115                            if let Some(ref _operation) = operation {
116                                match _operation {
117                                    crate::datadogV1::model::LogsArrayProcessorOperation::UnparsedObject(_operation) => {
118                                        _unparsed = true;
119                                    },
120                                    _ => {}
121                                }
122                            }
123                        }
124                        "type" => {
125                            type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
126                            if let Some(ref _type_) = type_ {
127                                match _type_ {
128                                    crate::datadogV1::model::LogsArrayProcessorType::UnparsedObject(_type_) => {
129                                        _unparsed = true;
130                                    },
131                                    _ => {}
132                                }
133                            }
134                        }
135                        &_ => {
136                            if let Ok(value) = serde_json::from_value(v.clone()) {
137                                additional_properties.insert(k, value);
138                            }
139                        }
140                    }
141                }
142                let operation = operation.ok_or_else(|| M::Error::missing_field("operation"))?;
143                let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
144
145                let content = LogsArrayProcessor {
146                    is_enabled,
147                    name,
148                    operation,
149                    type_,
150                    additional_properties,
151                    _unparsed,
152                };
153
154                Ok(content)
155            }
156        }
157
158        deserializer.deserialize_any(LogsArrayProcessorVisitor)
159    }
160}