datadog_api_client/datadogV2/model/
model_powerpack_template_variable.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/// Powerpack template variables.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct PowerpackTemplateVariable {
14    /// The list of values that the template variable drop-down is limited to.
15    #[serde(
16        rename = "available_values",
17        default,
18        with = "::serde_with::rust::double_option"
19    )]
20    pub available_values: Option<Option<Vec<String>>>,
21    /// One or many template variable default values within the saved view, which are unioned together using `OR` if more than one is specified.
22    #[serde(rename = "defaults")]
23    pub defaults: Option<Vec<String>>,
24    /// The name of the variable.
25    #[serde(rename = "name")]
26    pub name: String,
27    /// The tag prefix associated with the variable. Only tags with this prefix appear in the variable drop-down.
28    #[serde(rename = "prefix", default, with = "::serde_with::rust::double_option")]
29    pub prefix: Option<Option<String>>,
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 PowerpackTemplateVariable {
38    pub fn new(name: String) -> PowerpackTemplateVariable {
39        PowerpackTemplateVariable {
40            available_values: None,
41            defaults: None,
42            name,
43            prefix: None,
44            additional_properties: std::collections::BTreeMap::new(),
45            _unparsed: false,
46        }
47    }
48
49    pub fn available_values(mut self, value: Option<Vec<String>>) -> Self {
50        self.available_values = Some(value);
51        self
52    }
53
54    pub fn defaults(mut self, value: Vec<String>) -> Self {
55        self.defaults = Some(value);
56        self
57    }
58
59    pub fn prefix(mut self, value: Option<String>) -> Self {
60        self.prefix = Some(value);
61        self
62    }
63
64    pub fn additional_properties(
65        mut self,
66        value: std::collections::BTreeMap<String, serde_json::Value>,
67    ) -> Self {
68        self.additional_properties = value;
69        self
70    }
71}
72
73impl<'de> Deserialize<'de> for PowerpackTemplateVariable {
74    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
75    where
76        D: Deserializer<'de>,
77    {
78        struct PowerpackTemplateVariableVisitor;
79        impl<'a> Visitor<'a> for PowerpackTemplateVariableVisitor {
80            type Value = PowerpackTemplateVariable;
81
82            fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result {
83                f.write_str("a mapping")
84            }
85
86            fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error>
87            where
88                M: MapAccess<'a>,
89            {
90                let mut available_values: Option<Option<Vec<String>>> = None;
91                let mut defaults: Option<Vec<String>> = None;
92                let mut name: Option<String> = None;
93                let mut prefix: Option<Option<String>> = None;
94                let mut additional_properties: std::collections::BTreeMap<
95                    String,
96                    serde_json::Value,
97                > = std::collections::BTreeMap::new();
98                let mut _unparsed = false;
99
100                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
101                    match k.as_str() {
102                        "available_values" => {
103                            available_values =
104                                Some(serde_json::from_value(v).map_err(M::Error::custom)?);
105                        }
106                        "defaults" => {
107                            if v.is_null() {
108                                continue;
109                            }
110                            defaults = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
111                        }
112                        "name" => {
113                            name = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
114                        }
115                        "prefix" => {
116                            prefix = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
117                        }
118                        &_ => {
119                            if let Ok(value) = serde_json::from_value(v.clone()) {
120                                additional_properties.insert(k, value);
121                            }
122                        }
123                    }
124                }
125                let name = name.ok_or_else(|| M::Error::missing_field("name"))?;
126
127                let content = PowerpackTemplateVariable {
128                    available_values,
129                    defaults,
130                    name,
131                    prefix,
132                    additional_properties,
133                    _unparsed,
134                };
135
136                Ok(content)
137            }
138        }
139
140        deserializer.deserialize_any(PowerpackTemplateVariableVisitor)
141    }
142}