datadog_api_client/datadogV2/model/
model_relation_attributes.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/// Relation attributes.
10#[non_exhaustive]
11#[skip_serializing_none]
12#[derive(Clone, Debug, PartialEq, Serialize)]
13pub struct RelationAttributes {
14    /// Relation entity reference.
15    #[serde(rename = "from")]
16    pub from: Option<crate::datadogV2::model::RelationEntity>,
17    /// Relation entity reference.
18    #[serde(rename = "to")]
19    pub to: Option<crate::datadogV2::model::RelationEntity>,
20    /// Supported relation types.
21    #[serde(rename = "type")]
22    pub type_: Option<crate::datadogV2::model::RelationType>,
23    #[serde(flatten)]
24    pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>,
25    #[serde(skip)]
26    #[serde(default)]
27    pub(crate) _unparsed: bool,
28}
29
30impl RelationAttributes {
31    pub fn new() -> RelationAttributes {
32        RelationAttributes {
33            from: None,
34            to: None,
35            type_: None,
36            additional_properties: std::collections::BTreeMap::new(),
37            _unparsed: false,
38        }
39    }
40
41    pub fn from(mut self, value: crate::datadogV2::model::RelationEntity) -> Self {
42        self.from = Some(value);
43        self
44    }
45
46    pub fn to(mut self, value: crate::datadogV2::model::RelationEntity) -> Self {
47        self.to = Some(value);
48        self
49    }
50
51    pub fn type_(mut self, value: crate::datadogV2::model::RelationType) -> Self {
52        self.type_ = Some(value);
53        self
54    }
55
56    pub fn additional_properties(
57        mut self,
58        value: std::collections::BTreeMap<String, serde_json::Value>,
59    ) -> Self {
60        self.additional_properties = value;
61        self
62    }
63}
64
65impl Default for RelationAttributes {
66    fn default() -> Self {
67        Self::new()
68    }
69}
70
71impl<'de> Deserialize<'de> for RelationAttributes {
72    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
73    where
74        D: Deserializer<'de>,
75    {
76        struct RelationAttributesVisitor;
77        impl<'a> Visitor<'a> for RelationAttributesVisitor {
78            type Value = RelationAttributes;
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 from: Option<crate::datadogV2::model::RelationEntity> = None;
89                let mut to: Option<crate::datadogV2::model::RelationEntity> = None;
90                let mut type_: Option<crate::datadogV2::model::RelationType> = None;
91                let mut additional_properties: std::collections::BTreeMap<
92                    String,
93                    serde_json::Value,
94                > = std::collections::BTreeMap::new();
95                let mut _unparsed = false;
96
97                while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? {
98                    match k.as_str() {
99                        "from" => {
100                            if v.is_null() {
101                                continue;
102                            }
103                            from = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
104                        }
105                        "to" => {
106                            if v.is_null() {
107                                continue;
108                            }
109                            to = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
110                        }
111                        "type" => {
112                            if v.is_null() {
113                                continue;
114                            }
115                            type_ = Some(serde_json::from_value(v).map_err(M::Error::custom)?);
116                            if let Some(ref _type_) = type_ {
117                                match _type_ {
118                                    crate::datadogV2::model::RelationType::UnparsedObject(
119                                        _type_,
120                                    ) => {
121                                        _unparsed = true;
122                                    }
123                                    _ => {}
124                                }
125                            }
126                        }
127                        &_ => {
128                            if let Ok(value) = serde_json::from_value(v.clone()) {
129                                additional_properties.insert(k, value);
130                            }
131                        }
132                    }
133                }
134
135                let content = RelationAttributes {
136                    from,
137                    to,
138                    type_,
139                    additional_properties,
140                    _unparsed,
141                };
142
143                Ok(content)
144            }
145        }
146
147        deserializer.deserialize_any(RelationAttributesVisitor)
148    }
149}