datadog_api_client/datadogV1/model/
model_logs_url_parser.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/// This processor extracts query parameters and other important parameters from a URL.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct LogsURLParser {
14    /// Whether or not the processor is enabled.
15    #[serde(rename = "is_enabled")]
16    pub is_enabled: Option<bool>,
17    /// Name of the processor.
18    #[serde(rename = "name")]
19    pub name: Option<String>,
20    /// Normalize the ending slashes or not.
21    #[serde(
22        rename = "normalize_ending_slashes",
23        default,
24        with = "::serde_with::rust::double_option"
25    )]
26    pub normalize_ending_slashes: Option<Option<bool>>,
27    /// Array of source attributes.
28    #[serde(rename = "sources")]
29    pub sources: Vec<String>,
30    /// Name of the parent attribute that contains all the extracted details from the `sources`.
31    #[serde(rename = "target")]
32    pub target: String,
33    /// Type of logs URL parser.
34    #[serde(rename = "type")]
35    pub type_: crate::datadogV1::model::LogsURLParserType,
36    #[serde(flatten)]
37    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
38    #[serde(skip)]
39    #[serde(default)]
40    pub(crate) _unparsed: bool,
41}
42
43impl LogsURLParser {
44    pub fn new(
45        sources: Vec<String>,
46        target: String,
47        type_: crate::datadogV1::model::LogsURLParserType,
48    ) -> LogsURLParser {
49        LogsURLParser {
50            is_enabled: None,
51            name: None,
52            normalize_ending_slashes: None,
53            sources,
54            target,
55            type_,
56            additional_properties: std::collections::BTreeMap::new(),
57            _unparsed: false,
58        }
59    }
60
61    pub fn is_enabled(mut self, value: bool) -> Self {
62        self.is_enabled = Some(value);
63        self
64    }
65
66    pub fn name(mut self, value: String) -> Self {
67        self.name = Some(value);
68        self
69    }
70
71    pub fn normalize_ending_slashes(mut self, value: Option<bool>) -> Self {
72        self.normalize_ending_slashes = Some(value);
73        self
74    }
75
76    pub fn additional_properties(
77        mut self,
78        value: std::collections::BTreeMap<String, serde_json::Value>,
79    ) -> Self {
80        self.additional_properties = value;
81        self
82    }
83}
84
85impl<'de> Deserialize<'de> for LogsURLParser {
86    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
87    where
88        D: Deserializer<'de>,
89    {
90        struct LogsURLParserVisitor;
91        impl<'a> Visitor<'a> for LogsURLParserVisitor {
92            type Value = LogsURLParser;
93
94            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
95                f.write_str("a mapping")
96            }
97
98            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
99            where
100                M: MapAccess<'a>,
101            {
102                let mut is_enabled: Option<bool> = None;
103                let mut name: Option<String> = None;
104                let mut normalize_ending_slashes: Option<Option<bool>> = None;
105                let mut sources: Option<Vec<String>> = None;
106                let mut target: Option<String> = None;
107                let mut type_: Option<crate::datadogV1::model::LogsURLParserType> = None;
108                let mut additional_properties: std::collections::BTreeMap<
109                    String,
110                    serde_json::Value,
111                > = std::collections::BTreeMap::new();
112                let mut _unparsed = false;
113
114                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
115                    match k.as_str() {
116                        "is_enabled" => {
117                            if v.is_null() {
118                                continue;
119                            }
120                            is_enabled = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
121                        }
122                        "name" => {
123                            if v.is_null() {
124                                continue;
125                            }
126                            name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
127                        }
128                        "normalize_ending_slashes" => {
129                            normalize_ending_slashes =
130                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
131                        }
132                        "sources" => {
133                            sources = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
134                        }
135                        "target" => {
136                            target = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
137                        }
138                        "type" => {
139                            type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
140                            if let Some(ref _type_) = type_ {
141                                match _type_ {
142                                    crate::datadogV1::model::LogsURLParserType::UnparsedObject(
143                                        _type_,
144                                    ) => {
145                                        _unparsed = true;
146                                    }
147                                    _ => {}
148                                }
149                            }
150                        }
151                        &_ => {
152                            if let Ok(value) = serde_json::from_value(v.clone()) {
153                                additional_properties.insert(k, value);
154                            }
155                        }
156                    }
157                }
158                let sources = sources.ok_or_else(|| M::Error::missing_field("sources"))?;
159                let target = target.ok_or_else(|| M::Error::missing_field("target"))?;
160                let type_ = type_.ok_or_else(|| M::Error::missing_field("type_"))?;
161
162                let content = LogsURLParser {
163                    is_enabled,
164                    name,
165                    normalize_ending_slashes,
166                    sources,
167                    target,
168                    type_,
169                    additional_properties,
170                    _unparsed,
171                };
172
173                Ok(content)
174            }
175        }
176
177        deserializer.deserialize_any(LogsURLParserVisitor)
178    }
179}